Skip to content

Commit dfc7972

Browse files
committed
AnnotationAttributesReadingVisitor defensively handles meta-annotation retrieval failure
Issue: SPR-12493 (cherry picked from commit 5018889)
1 parent 90ae073 commit dfc7972

File tree

2 files changed

+9
-4
lines changed

2 files changed

+9
-4
lines changed

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,8 @@ public static <T extends Annotation> T getAnnotation(AnnotatedElement annotatedE
127127
/**
128128
* Get all {@link Annotation Annotations} from the supplied Method, Constructor or Field.
129129
* @param annotatedElement the Method, Constructor or Field to retrieve annotations from
130-
* @return the annotations found
130+
* @return the annotations found, or {@code null} if not resolvable (e.g. because nested
131+
* Class values in annotation attributes failed to resolve at runtime)
131132
* @since 4.0.8
132133
*/
133134
public static Annotation[] getAnnotations(AnnotatedElement annotatedElement) {

spring-core/src/main/java/org/springframework/core/type/classreading/AnnotationAttributesReadingVisitor.java

+7-3
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import org.springframework.core.annotation.AnnotationAttributes;
2727
import org.springframework.core.annotation.AnnotationUtils;
2828
import org.springframework.util.MultiValueMap;
29+
import org.springframework.util.ObjectUtils;
2930

3031
/**
3132
* ASM visitor which looks for the annotations defined on a class or method, including
@@ -72,9 +73,12 @@ public void doVisitEnd(Class<?> annotationClass) {
7273
attributes.add(0, this.attributes);
7374
}
7475
Set<String> metaAnnotationTypeNames = new LinkedHashSet<String>();
75-
for (Annotation metaAnnotation : AnnotationUtils.getAnnotations(annotationClass)) {
76-
if (!AnnotationUtils.isInJavaLangAnnotationPackage(metaAnnotation)) {
77-
recursivelyCollectMetaAnnotations(metaAnnotationTypeNames, metaAnnotation);
76+
Annotation[] metaAnnotations = AnnotationUtils.getAnnotations(annotationClass);
77+
if (!ObjectUtils.isEmpty(metaAnnotations)) {
78+
for (Annotation metaAnnotation : metaAnnotations) {
79+
if (!AnnotationUtils.isInJavaLangAnnotationPackage(metaAnnotation)) {
80+
recursivelyCollectMetaAnnotations(metaAnnotationTypeNames, metaAnnotation);
81+
}
7882
}
7983
}
8084
if (this.metaAnnotationMap != null) {

0 commit comments

Comments
 (0)