Skip to content

Commit 2c0c081

Browse files
committed
AnnotationUtils defensively catches and logs unexpected exceptions from retrieval attempts (proceeding like the annotation wasn't there)
Issue: SPR-11874
1 parent fd809cd commit 2c0c081

File tree

1 file changed

+30
-4
lines changed

1 file changed

+30
-4
lines changed

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

+30-4
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@
2828
import java.util.Set;
2929
import java.util.WeakHashMap;
3030

31+
import org.apache.commons.logging.Log;
32+
import org.apache.commons.logging.LogFactory;
33+
3134
import org.springframework.core.BridgeMethodResolver;
3235
import org.springframework.util.Assert;
3336
import org.springframework.util.ObjectUtils;
@@ -63,6 +66,8 @@ public abstract class AnnotationUtils {
6366
/** The attribute name for annotations with a single element */
6467
public static final String VALUE = "value";
6568

69+
private static final Log logger = LogFactory.getLog(AnnotationUtils.class);
70+
6671
private static final Map<Class<?>, Boolean> annotatedInterfaceCache = new WeakHashMap<Class<?>, Boolean>();
6772

6873

@@ -85,6 +90,9 @@ public static <T extends Annotation> T getAnnotation(Annotation ann, Class<T> an
8590
catch (Exception ex) {
8691
// Assuming nested Class values not resolvable within annotation attributes...
8792
// 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+
}
8896
return null;
8997
}
9098
}
@@ -93,16 +101,16 @@ public static <T extends Annotation> T getAnnotation(Annotation ann, Class<T> an
93101
* Get a single {@link Annotation} of {@code annotationType} from the supplied
94102
* Method, Constructor or Field. Meta-annotations will be searched if the annotation
95103
* is not declared locally on the supplied element.
96-
* @param ae the Method, Constructor or Field from which to get the annotation
104+
* @param annotatedElement the Method, Constructor or Field from which to get the annotation
97105
* @param annotationType the annotation type to look for, both locally and as a meta-annotation
98106
* @return the matching annotation, or {@code null} if none found
99107
* @since 3.1
100108
*/
101-
public static <T extends Annotation> T getAnnotation(AnnotatedElement ae, Class<T> annotationType) {
109+
public static <T extends Annotation> T getAnnotation(AnnotatedElement annotatedElement, Class<T> annotationType) {
102110
try {
103-
T ann = ae.getAnnotation(annotationType);
111+
T ann = annotatedElement.getAnnotation(annotationType);
104112
if (ann == null) {
105-
for (Annotation metaAnn : ae.getAnnotations()) {
113+
for (Annotation metaAnn : annotatedElement.getAnnotations()) {
106114
ann = metaAnn.annotationType().getAnnotation(annotationType);
107115
if (ann != null) {
108116
break;
@@ -114,6 +122,9 @@ public static <T extends Annotation> T getAnnotation(AnnotatedElement ae, Class<
114122
catch (Exception ex) {
115123
// Assuming nested Class values not resolvable within annotation attributes...
116124
// 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+
}
117128
return null;
118129
}
119130
}
@@ -132,6 +143,9 @@ public static Annotation[] getAnnotations(Method method) {
132143
catch (Exception ex) {
133144
// Assuming nested Class values not resolvable within annotation attributes...
134145
// 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+
}
135149
return null;
136150
}
137151
}
@@ -191,6 +205,9 @@ public static <A extends Annotation> Set<A> getRepeatableAnnotation(AnnotatedEle
191205
catch (Exception ex) {
192206
// Assuming nested Class values not resolvable within annotation attributes...
193207
// 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+
}
194211
}
195212
return Collections.emptySet();
196213
}
@@ -266,6 +283,9 @@ private static boolean isInterfaceWithAnnotatedMethods(Class<?> iface) {
266283
catch (Exception ex) {
267284
// Assuming nested Class values not resolvable within annotation attributes...
268285
// 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+
}
269289
}
270290
}
271291
annotatedInterfaceCache.put(iface, found);
@@ -317,6 +337,9 @@ private static <A extends Annotation> A findAnnotation(Class<?> clazz, Class<A>
317337
catch (Exception ex) {
318338
// Assuming nested Class values not resolvable within annotation attributes...
319339
// 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+
}
320343
return null;
321344
}
322345
}
@@ -441,6 +464,9 @@ public static boolean isAnnotationDeclaredLocally(Class<? extends Annotation> an
441464
catch (Exception ex) {
442465
// Assuming nested Class values not resolvable within annotation attributes...
443466
// 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+
}
444470
}
445471
return declaredLocally;
446472
}

0 commit comments

Comments
 (0)