68
68
public final class AnnotationElement {
69
69
private final Type type ;
70
70
private final List <Object > annotationValues ;
71
- private final List <String > annotationNames ;
72
71
private final boolean inBootClassLoader ;
73
72
74
73
// package private
75
74
AnnotationElement (Type type , List <Object > objects , boolean boot ) {
76
75
Objects .requireNonNull (type );
77
76
Objects .requireNonNull (objects );
78
77
this .type = type ;
79
- if (objects .size () != type .getFields ().size ()) {
78
+ List <ValueDescriptor > fields = type .getFields ();
79
+ int fieldCount = fields .size ();
80
+ if (objects .size () != fieldCount ) {
80
81
StringJoiner descriptors = new StringJoiner ("," , "[" , "]" );
81
82
for (ValueDescriptor v : type .getFields ()) {
82
83
descriptors .add (v .getName ());
@@ -88,25 +89,18 @@ public final class AnnotationElement {
88
89
throw new IllegalArgumentException ("Annotation " + descriptors + " for " + type .getName () + " doesn't match number of values " + values );
89
90
}
90
91
91
- List <String > n = new ArrayList <>();
92
- List <Object > v = new ArrayList <>();
93
- int index = 0 ;
94
- for (ValueDescriptor valueDescriptor : type .getFields ()) {
92
+ for (int index = 0 ; index < fieldCount ; index ++) {
95
93
Object object = objects .get (index );
96
94
if (object == null ) {
97
95
throw new IllegalArgumentException ("Annotation value can't be null" );
98
96
}
99
97
Class <?> valueType = object .getClass ();
100
- if (valueDescriptor .isArray ()) {
98
+ if (fields . get ( index ) .isArray ()) {
101
99
valueType = valueType .getComponentType ();
102
100
}
103
101
checkType (Utils .unboxType (valueType ));
104
- n .add (valueDescriptor .getName ());
105
- v .add (object );
106
- index ++;
107
102
}
108
- this .annotationValues = Utils .smallUnmodifiable (v );
109
- this .annotationNames = Utils .smallUnmodifiable (n );
103
+ this .annotationValues = Utils .smallUnmodifiable (objects );
110
104
this .inBootClassLoader = boot ;
111
105
}
112
106
@@ -162,9 +156,8 @@ public AnnotationElement(Class<? extends Annotation> annotationType, Map<String,
162
156
if (methods .length != map .size ()) {
163
157
throw new IllegalArgumentException ("Number of declared methods must match size of value map" );
164
158
}
165
- List <String > n = new ArrayList <>();
166
- List <Object > v = new ArrayList <>();
167
- Set <String > nameSet = new HashSet <>();
159
+ List <Object > v = new ArrayList <>(methods .length );
160
+ Set <String > nameSet = methods .length > 1 ? new HashSet <String >() : null ;
168
161
for (Method method : methods ) {
169
162
String fieldName = method .getName ();
170
163
Object object = map .get (fieldName );
@@ -198,18 +191,19 @@ public AnnotationElement(Class<? extends Annotation> annotationType, Map<String,
198
191
fieldType = Utils .unboxType (object .getClass ());
199
192
checkType (fieldType );
200
193
}
201
- if (nameSet .contains (fieldName )) {
202
- throw new IllegalArgumentException ("Value with name '" + fieldName + "' already exists" );
194
+ if (nameSet != null ) {
195
+ if (nameSet .contains (fieldName )) {
196
+ throw new IllegalArgumentException ("Value with name '" + fieldName + "' already exists" );
197
+ }
198
+ nameSet .add (fieldName );
203
199
}
204
200
if (isKnownJFRAnnotation (annotationType )) {
205
201
ValueDescriptor vd = new ValueDescriptor (fieldType , fieldName , Collections .emptyList (), true );
206
202
type .add (vd );
207
203
}
208
- n .add (fieldName );
209
204
v .add (object );
210
205
}
211
206
this .annotationValues = Utils .smallUnmodifiable (v );
212
- this .annotationNames = Utils .smallUnmodifiable (n );
213
207
this .inBootClassLoader = annotationType .getClassLoader () == null ;
214
208
}
215
209
@@ -314,12 +308,9 @@ public String getTypeName() {
314
308
*/
315
309
public Object getValue (String name ) {
316
310
Objects .requireNonNull (name );
317
- int index = 0 ;
318
- for (String n : annotationNames ) {
319
- if (name .equals (n )) {
320
- return annotationValues .get (index );
321
- }
322
- index ++;
311
+ int index = type .indexOf (name );
312
+ if (index != -1 ) {
313
+ return annotationValues .get (index );
323
314
}
324
315
StringJoiner valueNames = new StringJoiner ("," , "[" , "]" );
325
316
for (ValueDescriptor v : type .getFields ()) {
@@ -339,12 +330,7 @@ public Object getValue(String name) {
339
330
*/
340
331
public boolean hasValue (String name ) {
341
332
Objects .requireNonNull (name );
342
- for (String n : annotationNames ) {
343
- if (name .equals (n )) {
344
- return true ;
345
- }
346
- }
347
- return false ;
333
+ return type .indexOf (name ) != -1 ;
348
334
}
349
335
350
336
/**
0 commit comments