@@ -79,7 +79,14 @@ public static <T extends Annotation> T getAnnotation(Annotation ann, Class<T> an
79
79
if (annotationType .isInstance (ann )) {
80
80
return (T ) ann ;
81
81
}
82
- return ann .annotationType ().getAnnotation (annotationType );
82
+ try {
83
+ return ann .annotationType ().getAnnotation (annotationType );
84
+ }
85
+ catch (Exception ex ) {
86
+ // Assuming nested Class values not resolvable within annotation attributes...
87
+ // We're probably hitting a non-present optional arrangement - let's back out.
88
+ return null ;
89
+ }
83
90
}
84
91
85
92
/**
@@ -92,16 +99,23 @@ public static <T extends Annotation> T getAnnotation(Annotation ann, Class<T> an
92
99
* @since 3.1
93
100
*/
94
101
public static <T extends Annotation > T getAnnotation (AnnotatedElement ae , Class <T > annotationType ) {
95
- T ann = ae .getAnnotation (annotationType );
96
- if (ann == null ) {
97
- for (Annotation metaAnn : ae .getAnnotations ()) {
98
- ann = metaAnn .annotationType ().getAnnotation (annotationType );
99
- if (ann != null ) {
100
- break ;
102
+ try {
103
+ T ann = ae .getAnnotation (annotationType );
104
+ if (ann == null ) {
105
+ for (Annotation metaAnn : ae .getAnnotations ()) {
106
+ ann = metaAnn .annotationType ().getAnnotation (annotationType );
107
+ if (ann != null ) {
108
+ break ;
109
+ }
101
110
}
102
111
}
112
+ return ann ;
113
+ }
114
+ catch (Exception ex ) {
115
+ // Assuming nested Class values not resolvable within annotation attributes...
116
+ // We're probably hitting a non-present optional arrangement - let's back out.
117
+ return null ;
103
118
}
104
- return ann ;
105
119
}
106
120
107
121
/**
@@ -112,7 +126,14 @@ public static <T extends Annotation> T getAnnotation(AnnotatedElement ae, Class<
112
126
* @see org.springframework.core.BridgeMethodResolver#findBridgedMethod(Method)
113
127
*/
114
128
public static Annotation [] getAnnotations (Method method ) {
115
- return BridgeMethodResolver .findBridgedMethod (method ).getAnnotations ();
129
+ try {
130
+ return BridgeMethodResolver .findBridgedMethod (method ).getAnnotations ();
131
+ }
132
+ catch (Exception ex ) {
133
+ // Assuming nested Class values not resolvable within annotation attributes...
134
+ // We're probably hitting a non-present optional arrangement - let's back out.
135
+ return null ;
136
+ }
116
137
}
117
138
118
139
/**
@@ -162,10 +183,16 @@ public static <A extends Annotation> Set<A> getRepeatableAnnotation(Method metho
162
183
public static <A extends Annotation > Set <A > getRepeatableAnnotation (AnnotatedElement annotatedElement ,
163
184
Class <? extends Annotation > containerAnnotationType , Class <A > annotationType ) {
164
185
165
- if (annotatedElement .getAnnotations ().length == 0 ) {
166
- return Collections .emptySet ();
186
+ try {
187
+ if (annotatedElement .getAnnotations ().length > 0 ) {
188
+ return new AnnotationCollector <A >(containerAnnotationType , annotationType ).getResult (annotatedElement );
189
+ }
167
190
}
168
- return new AnnotationCollector <A >(containerAnnotationType , annotationType ).getResult (annotatedElement );
191
+ catch (Exception ex ) {
192
+ // Assuming nested Class values not resolvable within annotation attributes...
193
+ // We're probably hitting a non-present optional arrangement - let's back out.
194
+ }
195
+ return Collections .emptySet ();
169
196
}
170
197
171
198
/**
@@ -230,9 +257,15 @@ private static boolean isInterfaceWithAnnotatedMethods(Class<?> iface) {
230
257
}
231
258
boolean found = false ;
232
259
for (Method ifcMethod : iface .getMethods ()) {
233
- if (ifcMethod .getAnnotations ().length > 0 ) {
234
- found = true ;
235
- break ;
260
+ try {
261
+ if (ifcMethod .getAnnotations ().length > 0 ) {
262
+ found = true ;
263
+ break ;
264
+ }
265
+ }
266
+ catch (Exception ex ) {
267
+ // Assuming nested Class values not resolvable within annotation attributes...
268
+ // We're probably hitting a non-present optional arrangement - let's back out.
236
269
}
237
270
}
238
271
annotatedInterfaceCache .put (iface , found );
@@ -278,7 +311,14 @@ public static <A extends Annotation> A findAnnotation(Class<?> clazz, Class<A> a
278
311
private static <A extends Annotation > A findAnnotation (Class <?> clazz , Class <A > annotationType , Set <Annotation > visited ) {
279
312
Assert .notNull (clazz , "Class must not be null" );
280
313
if (isAnnotationDeclaredLocally (annotationType , clazz )) {
281
- return clazz .getAnnotation (annotationType );
314
+ try {
315
+ return clazz .getAnnotation (annotationType );
316
+ }
317
+ catch (Exception ex ) {
318
+ // Assuming nested Class values not resolvable within annotation attributes...
319
+ // We're probably hitting a non-present optional arrangement - let's back out.
320
+ return null ;
321
+ }
282
322
}
283
323
for (Class <?> ifc : clazz .getInterfaces ()) {
284
324
A annotation = findAnnotation (ifc , annotationType , visited );
@@ -390,12 +430,18 @@ public static boolean isAnnotationDeclaredLocally(Class<? extends Annotation> an
390
430
Assert .notNull (annotationType , "Annotation type must not be null" );
391
431
Assert .notNull (clazz , "Class must not be null" );
392
432
boolean declaredLocally = false ;
393
- for (Annotation annotation : clazz .getDeclaredAnnotations ()) {
394
- if (annotation .annotationType ().equals (annotationType )) {
395
- declaredLocally = true ;
396
- break ;
433
+ try {
434
+ for (Annotation annotation : clazz .getDeclaredAnnotations ()) {
435
+ if (annotation .annotationType ().equals (annotationType )) {
436
+ declaredLocally = true ;
437
+ break ;
438
+ }
397
439
}
398
440
}
441
+ catch (Exception ex ) {
442
+ // Assuming nested Class values not resolvable within annotation attributes...
443
+ // We're probably hitting a non-present optional arrangement - let's back out.
444
+ }
399
445
return declaredLocally ;
400
446
}
401
447
@@ -632,7 +678,7 @@ private void process(AnnotatedElement annotatedElement) {
632
678
this .result .add ((A ) annotation );
633
679
}
634
680
else if (ObjectUtils .nullSafeEquals (this .containerAnnotationType , annotation .annotationType ())) {
635
- this .result .addAll (Arrays . asList ( getValue (annotation ) ));
681
+ this .result .addAll (getValue (annotation ));
636
682
}
637
683
else if (!isInJavaLangAnnotationPackage (annotation )) {
638
684
process (annotation .annotationType ());
@@ -642,15 +688,15 @@ else if (!isInJavaLangAnnotationPackage(annotation)) {
642
688
}
643
689
644
690
@ SuppressWarnings ("unchecked" )
645
- private A [] getValue (Annotation annotation ) {
691
+ private List < A > getValue (Annotation annotation ) {
646
692
try {
647
693
Method method = annotation .annotationType ().getDeclaredMethod ("value" );
648
694
ReflectionUtils .makeAccessible (method );
649
- return ( A []) method .invoke (annotation );
695
+ return Arrays . asList (( A []) method .invoke (annotation ) );
650
696
}
651
697
catch (Exception ex ) {
652
- throw new IllegalStateException ( " Unable to read value from repeating annotation container " +
653
- this . containerAnnotationType . getName (), ex );
698
+ // Unable to read value from repeating annotation container -> ignore it.
699
+ return Collections . emptyList ( );
654
700
}
655
701
}
656
702
}
0 commit comments