Skip to content

Commit 99e2770

Browse files
gsmetgunnarmorling
authored andcommitted
HV-1502 Remove an unnecessary intermediary map in AnnotationDef
1 parent 6345df8 commit 99e2770

File tree

3 files changed

+37
-37
lines changed

3 files changed

+37
-37
lines changed

engine/src/main/java/org/hibernate/validator/cfg/AnnotationDef.java

+8-23
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
import java.util.Map;
1515

1616
import org.hibernate.validator.internal.util.CollectionHelper;
17-
import org.hibernate.validator.internal.util.StringHelper;
1817
import org.hibernate.validator.internal.util.annotation.AnnotationDescriptor;
1918
import org.hibernate.validator.internal.util.logging.Log;
2019
import org.hibernate.validator.internal.util.logging.LoggerFactory;
@@ -32,6 +31,7 @@
3231
* @author Hardy Ferentschik
3332
* @author Gunnar Morling
3433
* @author Marko Bekhta
34+
* @author Guillaume Smet
3535
*/
3636
public abstract class AnnotationDef<C extends AnnotationDef<C, A>, A extends Annotation> {
3737

@@ -42,16 +42,9 @@ public abstract class AnnotationDef<C extends AnnotationDef<C, A>, A extends Ann
4242
// public getters as they would pollute the fluent definition API.
4343

4444
/**
45-
* The constraint annotation type of this definition.
45+
* The annotation descriptor builder.
4646
*/
47-
protected final Class<A> annotationType;
48-
49-
/**
50-
* A map with the annotation parameters of this definition. Contains only parameters
51-
* of non annotation types. Keys are property names of this definition's annotation
52-
* type, values are annotation parameter values of the appropriate types.
53-
*/
54-
protected final Map<String, Object> parameters;
47+
protected final AnnotationDescriptor.Builder<A> annotationDescriptorBuilder;
5548

5649
/**
5750
* A map with annotation parameters of this definition which are annotations
@@ -71,15 +64,13 @@ public abstract class AnnotationDef<C extends AnnotationDef<C, A>, A extends Ann
7164
private final Map<String, Class<?>> annotationsAsParametersTypes;
7265

7366
protected AnnotationDef(Class<A> annotationType) {
74-
this.annotationType = annotationType;
75-
this.parameters = new HashMap<>();
67+
this.annotationDescriptorBuilder = new AnnotationDescriptor.Builder<>( annotationType );
7668
this.annotationsAsParameters = new HashMap<>();
7769
this.annotationsAsParametersTypes = new HashMap<>();
7870
}
7971

8072
protected AnnotationDef(AnnotationDef<?, A> original) {
81-
this.annotationType = original.annotationType;
82-
this.parameters = original.parameters;
73+
this.annotationDescriptorBuilder = original.annotationDescriptorBuilder;
8374
this.annotationsAsParameters = original.annotationsAsParameters;
8475
this.annotationsAsParametersTypes = original.annotationsAsParametersTypes;
8576
}
@@ -90,7 +81,7 @@ private C getThis() {
9081
}
9182

9283
protected C addParameter(String key, Object value) {
93-
parameters.put( key, value );
84+
annotationDescriptorBuilder.setAttribute( key, value );
9485
return getThis();
9586
}
9687

@@ -105,16 +96,11 @@ protected C addAnnotationAsParameter(String key, AnnotationDef<?, ?> value) {
10596
return resultingList;
10697
}
10798
} );
108-
annotationsAsParametersTypes.putIfAbsent( key, value.annotationType );
99+
annotationsAsParametersTypes.putIfAbsent( key, value.annotationDescriptorBuilder.getType() );
109100
return getThis();
110101
}
111102

112103
protected AnnotationDescriptor<A> createAnnotationDescriptor() {
113-
AnnotationDescriptor.Builder<A> annotationDescriptorBuilder = new AnnotationDescriptor.Builder<>( annotationType );
114-
for ( Map.Entry<String, Object> parameter : parameters.entrySet() ) {
115-
annotationDescriptorBuilder.setAttribute( parameter.getKey(), parameter.getValue() );
116-
}
117-
118104
for ( Map.Entry<String, List<AnnotationDef<?, ?>>> annotationAsParameter : annotationsAsParameters.entrySet() ) {
119105
annotationDescriptorBuilder.setAttribute(
120106
annotationAsParameter.getKey(),
@@ -149,8 +135,7 @@ public String toString() {
149135
final StringBuilder sb = new StringBuilder();
150136
sb.append( this.getClass().getSimpleName() );
151137
sb.append( '{' );
152-
sb.append( "annotationType=" ).append( StringHelper.toShortString( annotationType ) );
153-
sb.append( ", parameters=" ).append( parameters );
138+
sb.append( annotationDescriptorBuilder );
154139
sb.append( '}' );
155140
return sb.toString();
156141
}

engine/src/main/java/org/hibernate/validator/cfg/ConstraintDef.java

-13
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@
1010

1111
import javax.validation.Payload;
1212

13-
import org.hibernate.validator.internal.util.StringHelper;
14-
1513
/**
1614
* Base class for all constraint definition types. Each sub type represents a
1715
* single constraint annotation type and allows to add this constraint to a bean
@@ -56,15 +54,4 @@ public C payload(Class<? extends Payload>... payload) {
5654
addParameter( "payload", payload );
5755
return getThis();
5856
}
59-
60-
@Override
61-
public String toString() {
62-
final StringBuilder sb = new StringBuilder();
63-
sb.append( this.getClass().getSimpleName() );
64-
sb.append( '{' );
65-
sb.append( "constraintType=" ).append( StringHelper.toShortString( annotationType ) );
66-
sb.append( ", parameters=" ).append( parameters );
67-
sb.append( '}' );
68-
return sb.toString();
69-
}
7057
}

engine/src/main/java/org/hibernate/validator/internal/util/annotation/AnnotationDescriptor.java

+29-1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import java.util.TreeSet;
2323

2424
import org.hibernate.validator.internal.util.CollectionHelper;
25+
import org.hibernate.validator.internal.util.StringHelper;
2526
import org.hibernate.validator.internal.util.logging.Log;
2627
import org.hibernate.validator.internal.util.logging.LoggerFactory;
2728
import org.hibernate.validator.internal.util.privilegedactions.GetAnnotationAttributes;
@@ -161,7 +162,7 @@ public int hashCode() {
161162
@Override
162163
public String toString() {
163164
StringBuilder result = new StringBuilder();
164-
result.append( '@' ).append( type.getName() ).append( '(' );
165+
result.append( '@' ).append( StringHelper.toShortString( type ) ).append( '(' );
165166
for ( String s : getRegisteredAttributesInAlphabeticalOrder() ) {
166167
result.append( s ).append( '=' ).append( attributes.get( s ) ).append( ", " );
167168
}
@@ -272,6 +273,10 @@ public boolean hasAttribute(String key) {
272273
return attributes.containsKey( key );
273274
}
274275

276+
public Class<S> getType() {
277+
return type;
278+
}
279+
275280
public AnnotationDescriptor<S> build() {
276281
return new AnnotationDescriptor<S>( type, getAnnotationAttributes() );
277282
}
@@ -307,6 +312,29 @@ else if ( m.getDefaultValue() != null ) {
307312
}
308313
return result;
309314
}
315+
316+
@Override
317+
public String toString() {
318+
StringBuilder result = new StringBuilder();
319+
result.append( '@' ).append( StringHelper.toShortString( type ) ).append( '(' );
320+
for ( String s : getRegisteredAttributesInAlphabeticalOrder() ) {
321+
result.append( s ).append( '=' ).append( attributes.get( s ) ).append( ", " );
322+
}
323+
// remove last separator:
324+
if ( attributes.size() > 0 ) {
325+
result.delete( result.length() - 2, result.length() );
326+
result.append( ")" );
327+
}
328+
else {
329+
result.delete( result.length() - 1, result.length() );
330+
}
331+
332+
return result.toString();
333+
}
334+
335+
private SortedSet<String> getRegisteredAttributesInAlphabeticalOrder() {
336+
return new TreeSet<String>( attributes.keySet() );
337+
}
310338
}
311339

312340
/**

0 commit comments

Comments
 (0)