@@ -66,14 +66,15 @@ 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 <AnnotationCacheKey , Annotation > findAnnotationCache =
72
71
new ConcurrentReferenceHashMap <AnnotationCacheKey , Annotation >(256 );
73
72
74
73
private static final Map <Class <?>, Boolean > annotatedInterfaceCache =
75
74
new ConcurrentReferenceHashMap <Class <?>, Boolean >(256 );
76
75
76
+ private static transient Log logger ;
77
+
77
78
78
79
/**
79
80
* Get a single {@link Annotation} of {@code annotationType} from the supplied
@@ -93,10 +94,7 @@ public static <T extends Annotation> T getAnnotation(Annotation ann, Class<T> an
93
94
}
94
95
catch (Exception ex ) {
95
96
// Assuming nested Class values not resolvable within annotation attributes...
96
- // We're probably hitting a non-present optional arrangement - let's back out.
97
- if (logger .isInfoEnabled ()) {
98
- logger .info ("Failed to introspect annotations on [" + ann .annotationType () + "]: " + ex );
99
- }
97
+ logIntrospectionFailure (ann .annotationType (), ex );
100
98
return null ;
101
99
}
102
100
}
@@ -125,10 +123,7 @@ public static <T extends Annotation> T getAnnotation(AnnotatedElement annotatedE
125
123
}
126
124
catch (Exception ex ) {
127
125
// Assuming nested Class values not resolvable within annotation attributes...
128
- // We're probably hitting a non-present optional arrangement - let's back out.
129
- if (logger .isInfoEnabled ()) {
130
- logger .info ("Failed to introspect annotations on [" + annotatedElement + "]: " + ex );
131
- }
126
+ logIntrospectionFailure (annotatedElement , ex );
132
127
return null ;
133
128
}
134
129
}
@@ -146,10 +141,7 @@ public static Annotation[] getAnnotations(Method method) {
146
141
}
147
142
catch (Exception ex ) {
148
143
// Assuming nested Class values not resolvable within annotation attributes...
149
- // We're probably hitting a non-present optional arrangement - let's back out.
150
- if (logger .isInfoEnabled ()) {
151
- logger .info ("Failed to introspect annotations on [" + method + "]: " + ex );
152
- }
144
+ logIntrospectionFailure (method , ex );
153
145
return null ;
154
146
}
155
147
}
@@ -208,10 +200,7 @@ public static <A extends Annotation> Set<A> getRepeatableAnnotation(AnnotatedEle
208
200
}
209
201
catch (Exception ex ) {
210
202
// Assuming nested Class values not resolvable within annotation attributes...
211
- // We're probably hitting a non-present optional arrangement - let's back out.
212
- if (logger .isInfoEnabled ()) {
213
- logger .info ("Failed to introspect annotations on [" + annotatedElement + "]: " + ex );
214
- }
203
+ logIntrospectionFailure (annotatedElement , ex );
215
204
}
216
205
return Collections .emptySet ();
217
206
}
@@ -293,10 +282,7 @@ private static boolean isInterfaceWithAnnotatedMethods(Class<?> iface) {
293
282
}
294
283
catch (Exception ex ) {
295
284
// Assuming nested Class values not resolvable within annotation attributes...
296
- // We're probably hitting a non-present optional arrangement - let's back out.
297
- if (logger .isInfoEnabled ()) {
298
- logger .info ("Failed to introspect annotations on [" + ifcMethod + "]: " + ex );
299
- }
285
+ logIntrospectionFailure (ifcMethod , ex );
300
286
}
301
287
}
302
288
annotatedInterfaceCache .put (iface , found );
@@ -347,35 +333,39 @@ public static <A extends Annotation> A findAnnotation(Class<?> clazz, Class<A> a
347
333
* @param visited the set of annotations that have already been visited
348
334
* @return the annotation if found, or {@code null} if not found
349
335
*/
336
+ @ SuppressWarnings ("unchecked" )
350
337
private static <A extends Annotation > A findAnnotation (Class <?> clazz , Class <A > annotationType , Set <Annotation > visited ) {
351
338
Assert .notNull (clazz , "Class must not be null" );
352
- if (isAnnotationDeclaredLocally (annotationType , clazz )) {
353
- try {
354
- return clazz .getAnnotation (annotationType );
339
+
340
+ try {
341
+ Annotation [] anns = clazz .getDeclaredAnnotations ();
342
+ for (Annotation ann : anns ) {
343
+ if (ann .annotationType ().equals (annotationType )) {
344
+ return (A ) ann ;
345
+ }
355
346
}
356
- catch (Exception ex ) {
357
- // Assuming nested Class values not resolvable within annotation attributes...
358
- // We're probably hitting a non-present optional arrangement - let's back out.
359
- if (logger .isInfoEnabled ()) {
360
- logger .info ("Failed to introspect annotations on [" + clazz + "]: " + ex );
347
+ for (Annotation ann : anns ) {
348
+ if (!isInJavaLangAnnotationPackage (ann ) && visited .add (ann )) {
349
+ A annotation = findAnnotation (ann .annotationType (), annotationType , visited );
350
+ if (annotation != null ) {
351
+ return annotation ;
352
+ }
361
353
}
362
- return null ;
363
354
}
364
355
}
356
+ catch (Exception ex ) {
357
+ // Assuming nested Class values not resolvable within annotation attributes...
358
+ // We're probably hitting a non-present optional arrangement - let's back out.
359
+ return null ;
360
+ }
361
+
365
362
for (Class <?> ifc : clazz .getInterfaces ()) {
366
363
A annotation = findAnnotation (ifc , annotationType , visited );
367
364
if (annotation != null ) {
368
365
return annotation ;
369
366
}
370
367
}
371
- for (Annotation ann : clazz .getDeclaredAnnotations ()) {
372
- if (!isInJavaLangAnnotationPackage (ann ) && visited .add (ann )) {
373
- A annotation = findAnnotation (ann .annotationType (), annotationType , visited );
374
- if (annotation != null ) {
375
- return annotation ;
376
- }
377
- }
378
- }
368
+
379
369
Class <?> superclass = clazz .getSuperclass ();
380
370
if (superclass == null || superclass .equals (Object .class )) {
381
371
return null ;
@@ -473,19 +463,16 @@ public static boolean isAnnotationDeclaredLocally(Class<? extends Annotation> an
473
463
Assert .notNull (clazz , "Class must not be null" );
474
464
boolean declaredLocally = false ;
475
465
try {
476
- for (Annotation annotation : clazz .getDeclaredAnnotations ()) {
477
- if (annotation .annotationType ().equals (annotationType )) {
466
+ for (Annotation ann : clazz .getDeclaredAnnotations ()) {
467
+ if (ann .annotationType ().equals (annotationType )) {
478
468
declaredLocally = true ;
479
469
break ;
480
470
}
481
471
}
482
472
}
483
473
catch (Exception ex ) {
484
474
// Assuming nested Class values not resolvable within annotation attributes...
485
- // We're probably hitting a non-present optional arrangement - let's back out.
486
- if (logger .isInfoEnabled ()) {
487
- logger .info ("Failed to introspect annotations on [" + clazz + "]: " + ex );
488
- }
475
+ logIntrospectionFailure (clazz , ex );
489
476
}
490
477
return declaredLocally ;
491
478
}
@@ -710,6 +697,18 @@ public static Object getDefaultValue(Class<? extends Annotation> annotationType,
710
697
}
711
698
712
699
700
+ private static void logIntrospectionFailure (AnnotatedElement annotatedElement , Exception ex ) {
701
+ Log loggerToUse = logger ;
702
+ if (loggerToUse == null ) {
703
+ loggerToUse = LogFactory .getLog (AnnotationUtils .class );
704
+ logger = loggerToUse ;
705
+ }
706
+ if (loggerToUse .isInfoEnabled ()) {
707
+ loggerToUse .info ("Failed to introspect annotations on [" + annotatedElement + "]: " + ex );
708
+ }
709
+ }
710
+
711
+
713
712
/**
714
713
* Cache key for the AnnotatedElement cache.
715
714
*/
@@ -767,15 +766,15 @@ public Set<A> getResult(AnnotatedElement element) {
767
766
@ SuppressWarnings ("unchecked" )
768
767
private void process (AnnotatedElement annotatedElement ) {
769
768
if (this .visited .add (annotatedElement )) {
770
- for (Annotation annotation : annotatedElement .getAnnotations ()) {
771
- if (ObjectUtils .nullSafeEquals (this .annotationType , annotation .annotationType ())) {
772
- this .result .add ((A ) annotation );
769
+ for (Annotation ann : annotatedElement .getAnnotations ()) {
770
+ if (ObjectUtils .nullSafeEquals (this .annotationType , ann .annotationType ())) {
771
+ this .result .add ((A ) ann );
773
772
}
774
- else if (ObjectUtils .nullSafeEquals (this .containerAnnotationType , annotation .annotationType ())) {
775
- this .result .addAll (getValue (annotation ));
773
+ else if (ObjectUtils .nullSafeEquals (this .containerAnnotationType , ann .annotationType ())) {
774
+ this .result .addAll (getValue (ann ));
776
775
}
777
- else if (!isInJavaLangAnnotationPackage (annotation )) {
778
- process (annotation .annotationType ());
776
+ else if (!isInJavaLangAnnotationPackage (ann )) {
777
+ process (ann .annotationType ());
779
778
}
780
779
}
781
780
}
0 commit comments