Skip to content

Commit e16f21c

Browse files
committed
AnnotationUtils consistently logs introspection failures via lazily initialized logger
Issue: SPR-12325 Issue: SPR-12329
1 parent 7507560 commit e16f21c

File tree

1 file changed

+46
-43
lines changed

1 file changed

+46
-43
lines changed

spring-core/src/main/java/org/springframework/core/annotation/AnnotationUtils.java

+46-43
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,11 @@ public abstract class AnnotationUtils {
6666
/** The attribute name for annotations with a single element */
6767
public static final String VALUE = "value";
6868

69-
private static final Log logger = LogFactory.getLog(AnnotationUtils.class);
7069

7170
private static final Map<Class<?>, Boolean> annotatedInterfaceCache = new WeakHashMap<Class<?>, Boolean>();
7271

72+
private static transient Log logger;
73+
7374

7475
/**
7576
* 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
8990
}
9091
catch (Exception ex) {
9192
// 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);
9694
return null;
9795
}
9896
}
@@ -121,10 +119,7 @@ public static <T extends Annotation> T getAnnotation(AnnotatedElement annotatedE
121119
}
122120
catch (Exception ex) {
123121
// 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);
128123
return null;
129124
}
130125
}
@@ -142,10 +137,7 @@ public static Annotation[] getAnnotations(Method method) {
142137
}
143138
catch (Exception ex) {
144139
// 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);
149141
return null;
150142
}
151143
}
@@ -204,10 +196,7 @@ public static <A extends Annotation> Set<A> getRepeatableAnnotation(AnnotatedEle
204196
}
205197
catch (Exception ex) {
206198
// 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);
211200
}
212201
return Collections.emptySet();
213202
}
@@ -282,10 +271,7 @@ private static boolean isInterfaceWithAnnotatedMethods(Class<?> iface) {
282271
}
283272
catch (Exception ex) {
284273
// 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);
289275
}
290276
}
291277
annotatedInterfaceCache.put(iface, found);
@@ -330,33 +316,41 @@ public static <A extends Annotation> A findAnnotation(Class<?> clazz, Class<A> a
330316
*/
331317
private static <A extends Annotation> A findAnnotation(Class<?> clazz, Class<A> annotationType, Set<Annotation> visited) {
332318
Assert.notNull(clazz, "Class must not be null");
319+
333320
if (isAnnotationDeclaredLocally(annotationType, clazz)) {
334321
try {
335322
return clazz.getAnnotation(annotationType);
336323
}
337324
catch (Exception ex) {
338325
// 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);
343327
return null;
344328
}
345329
}
330+
346331
for (Class<?> ifc : clazz.getInterfaces()) {
347332
A annotation = findAnnotation(ifc, annotationType, visited);
348333
if (annotation != null) {
349334
return annotation;
350335
}
351336
}
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+
}
357345
}
358346
}
359347
}
348+
catch (Exception ex) {
349+
// Assuming nested Class values not resolvable within annotation attributes...
350+
logIntrospectionFailure(clazz, ex);
351+
return null;
352+
}
353+
360354
Class<?> superclass = clazz.getSuperclass();
361355
if (superclass == null || superclass.equals(Object.class)) {
362356
return null;
@@ -454,19 +448,16 @@ public static boolean isAnnotationDeclaredLocally(Class<? extends Annotation> an
454448
Assert.notNull(clazz, "Class must not be null");
455449
boolean declaredLocally = false;
456450
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)) {
459453
declaredLocally = true;
460454
break;
461455
}
462456
}
463457
}
464458
catch (Exception ex) {
465459
// 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);
470461
}
471462
return declaredLocally;
472463
}
@@ -691,6 +682,18 @@ public static Object getDefaultValue(Class<? extends Annotation> annotationType,
691682
}
692683

693684

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+
694697
private static class AnnotationCollector<A extends Annotation> {
695698

696699
private final Class<? extends Annotation> containerAnnotationType;
@@ -714,15 +717,15 @@ public Set<A> getResult(AnnotatedElement element) {
714717
@SuppressWarnings("unchecked")
715718
private void process(AnnotatedElement annotatedElement) {
716719
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);
720723
}
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));
723726
}
724-
else if (!isInJavaLangAnnotationPackage(annotation)) {
725-
process(annotation.annotationType());
727+
else if (!isInJavaLangAnnotationPackage(ann)) {
728+
process(ann.annotationType());
726729
}
727730
}
728731
}

0 commit comments

Comments
 (0)