@@ -66,10 +66,11 @@ public abstract class AnnotationUtils {
66
66
/** The attribute name for annotations with a single element */
67
67
public static final String VALUE = "value" ;
68
68
69
- private static final Log logger = LogFactory .getLog (AnnotationUtils .class );
70
69
71
70
private static final Map <Class <?>, Boolean > annotatedInterfaceCache = new WeakHashMap <Class <?>, Boolean >();
72
71
72
+ private static transient Log logger ;
73
+
73
74
74
75
/**
75
76
* Get a single {@link Annotation} of {@code annotationType} from the supplied
@@ -89,10 +90,7 @@ public static <T extends Annotation> T getAnnotation(Annotation ann, Class<T> an
89
90
}
90
91
catch (Exception ex ) {
91
92
// Assuming nested Class values not resolvable within annotation attributes...
92
- // We're probably hitting a non-present optional arrangement - let's back out.
93
- if (logger .isInfoEnabled ()) {
94
- logger .info ("Failed to introspect annotations on [" + ann .annotationType () + "]: " + ex );
95
- }
93
+ logIntrospectionFailure (ann .annotationType (), ex );
96
94
return null ;
97
95
}
98
96
}
@@ -121,10 +119,7 @@ public static <T extends Annotation> T getAnnotation(AnnotatedElement annotatedE
121
119
}
122
120
catch (Exception ex ) {
123
121
// Assuming nested Class values not resolvable within annotation attributes...
124
- // We're probably hitting a non-present optional arrangement - let's back out.
125
- if (logger .isInfoEnabled ()) {
126
- logger .info ("Failed to introspect annotations on [" + annotatedElement + "]: " + ex );
127
- }
122
+ logIntrospectionFailure (annotatedElement , ex );
128
123
return null ;
129
124
}
130
125
}
@@ -142,10 +137,7 @@ public static Annotation[] getAnnotations(Method method) {
142
137
}
143
138
catch (Exception ex ) {
144
139
// Assuming nested Class values not resolvable within annotation attributes...
145
- // We're probably hitting a non-present optional arrangement - let's back out.
146
- if (logger .isInfoEnabled ()) {
147
- logger .info ("Failed to introspect annotations on [" + method + "]: " + ex );
148
- }
140
+ logIntrospectionFailure (method , ex );
149
141
return null ;
150
142
}
151
143
}
@@ -204,10 +196,7 @@ public static <A extends Annotation> Set<A> getRepeatableAnnotation(AnnotatedEle
204
196
}
205
197
catch (Exception ex ) {
206
198
// Assuming nested Class values not resolvable within annotation attributes...
207
- // We're probably hitting a non-present optional arrangement - let's back out.
208
- if (logger .isInfoEnabled ()) {
209
- logger .info ("Failed to introspect annotations on [" + annotatedElement + "]: " + ex );
210
- }
199
+ logIntrospectionFailure (annotatedElement , ex );
211
200
}
212
201
return Collections .emptySet ();
213
202
}
@@ -282,10 +271,7 @@ private static boolean isInterfaceWithAnnotatedMethods(Class<?> iface) {
282
271
}
283
272
catch (Exception ex ) {
284
273
// Assuming nested Class values not resolvable within annotation attributes...
285
- // We're probably hitting a non-present optional arrangement - let's back out.
286
- if (logger .isInfoEnabled ()) {
287
- logger .info ("Failed to introspect annotations on [" + ifcMethod + "]: " + ex );
288
- }
274
+ logIntrospectionFailure (ifcMethod , ex );
289
275
}
290
276
}
291
277
annotatedInterfaceCache .put (iface , found );
@@ -330,33 +316,41 @@ public static <A extends Annotation> A findAnnotation(Class<?> clazz, Class<A> a
330
316
*/
331
317
private static <A extends Annotation > A findAnnotation (Class <?> clazz , Class <A > annotationType , Set <Annotation > visited ) {
332
318
Assert .notNull (clazz , "Class must not be null" );
319
+
333
320
if (isAnnotationDeclaredLocally (annotationType , clazz )) {
334
321
try {
335
322
return clazz .getAnnotation (annotationType );
336
323
}
337
324
catch (Exception ex ) {
338
325
// Assuming nested Class values not resolvable within annotation attributes...
339
- // We're probably hitting a non-present optional arrangement - let's back out.
340
- if (logger .isInfoEnabled ()) {
341
- logger .info ("Failed to introspect annotations on [" + clazz + "]: " + ex );
342
- }
326
+ logIntrospectionFailure (clazz , ex );
343
327
return null ;
344
328
}
345
329
}
330
+
346
331
for (Class <?> ifc : clazz .getInterfaces ()) {
347
332
A annotation = findAnnotation (ifc , annotationType , visited );
348
333
if (annotation != null ) {
349
334
return annotation ;
350
335
}
351
336
}
352
- for (Annotation ann : clazz .getDeclaredAnnotations ()) {
353
- if (!isInJavaLangAnnotationPackage (ann ) && visited .add (ann )) {
354
- A annotation = findAnnotation (ann .annotationType (), annotationType , visited );
355
- if (annotation != null ) {
356
- return annotation ;
337
+
338
+ try {
339
+ for (Annotation ann : clazz .getDeclaredAnnotations ()) {
340
+ if (!isInJavaLangAnnotationPackage (ann ) && visited .add (ann )) {
341
+ A annotation = findAnnotation (ann .annotationType (), annotationType , visited );
342
+ if (annotation != null ) {
343
+ return annotation ;
344
+ }
357
345
}
358
346
}
359
347
}
348
+ catch (Exception ex ) {
349
+ // Assuming nested Class values not resolvable within annotation attributes...
350
+ logIntrospectionFailure (clazz , ex );
351
+ return null ;
352
+ }
353
+
360
354
Class <?> superclass = clazz .getSuperclass ();
361
355
if (superclass == null || superclass .equals (Object .class )) {
362
356
return null ;
@@ -454,19 +448,16 @@ public static boolean isAnnotationDeclaredLocally(Class<? extends Annotation> an
454
448
Assert .notNull (clazz , "Class must not be null" );
455
449
boolean declaredLocally = false ;
456
450
try {
457
- for (Annotation annotation : clazz .getDeclaredAnnotations ()) {
458
- if (annotation .annotationType ().equals (annotationType )) {
451
+ for (Annotation ann : clazz .getDeclaredAnnotations ()) {
452
+ if (ann .annotationType ().equals (annotationType )) {
459
453
declaredLocally = true ;
460
454
break ;
461
455
}
462
456
}
463
457
}
464
458
catch (Exception ex ) {
465
459
// Assuming nested Class values not resolvable within annotation attributes...
466
- // We're probably hitting a non-present optional arrangement - let's back out.
467
- if (logger .isInfoEnabled ()) {
468
- logger .info ("Failed to introspect annotations on [" + clazz + "]: " + ex );
469
- }
460
+ logIntrospectionFailure (clazz , ex );
470
461
}
471
462
return declaredLocally ;
472
463
}
@@ -691,6 +682,18 @@ public static Object getDefaultValue(Class<? extends Annotation> annotationType,
691
682
}
692
683
693
684
685
+ private static void logIntrospectionFailure (AnnotatedElement annotatedElement , Exception ex ) {
686
+ Log loggerToUse = logger ;
687
+ if (loggerToUse == null ) {
688
+ loggerToUse = LogFactory .getLog (AnnotationUtils .class );
689
+ logger = loggerToUse ;
690
+ }
691
+ if (loggerToUse .isInfoEnabled ()) {
692
+ loggerToUse .info ("Failed to introspect annotations on [" + annotatedElement + "]: " + ex );
693
+ }
694
+ }
695
+
696
+
694
697
private static class AnnotationCollector <A extends Annotation > {
695
698
696
699
private final Class <? extends Annotation > containerAnnotationType ;
@@ -714,15 +717,15 @@ public Set<A> getResult(AnnotatedElement element) {
714
717
@ SuppressWarnings ("unchecked" )
715
718
private void process (AnnotatedElement annotatedElement ) {
716
719
if (this .visited .add (annotatedElement )) {
717
- for (Annotation annotation : annotatedElement .getAnnotations ()) {
718
- if (ObjectUtils .nullSafeEquals (this .annotationType , annotation .annotationType ())) {
719
- this .result .add ((A ) annotation );
720
+ for (Annotation ann : annotatedElement .getAnnotations ()) {
721
+ if (ObjectUtils .nullSafeEquals (this .annotationType , ann .annotationType ())) {
722
+ this .result .add ((A ) ann );
720
723
}
721
- else if (ObjectUtils .nullSafeEquals (this .containerAnnotationType , annotation .annotationType ())) {
722
- this .result .addAll (getValue (annotation ));
724
+ else if (ObjectUtils .nullSafeEquals (this .containerAnnotationType , ann .annotationType ())) {
725
+ this .result .addAll (getValue (ann ));
723
726
}
724
- else if (!isInJavaLangAnnotationPackage (annotation )) {
725
- process (annotation .annotationType ());
727
+ else if (!isInJavaLangAnnotationPackage (ann )) {
728
+ process (ann .annotationType ());
726
729
}
727
730
}
728
731
}
0 commit comments