Skip to content

Commit 63115ed

Browse files
committed
Do not use annotation detection shortcuts on Class
Issue: SPR-13440
1 parent c5b318a commit 63115ed

File tree

1 file changed

+14
-6
lines changed

1 file changed

+14
-6
lines changed

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

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -332,9 +332,13 @@ public static <A extends Annotation> A getMergedAnnotation(AnnotatedElement elem
332332
Assert.notNull(annotationType, "annotationType must not be null");
333333

334334
// Shortcut: directly present on the element, with no merging needed?
335-
A annotation = element.getAnnotation(annotationType);
336-
if (annotation != null) {
337-
return AnnotationUtils.synthesizeAnnotation(annotation, element);
335+
if (!(element instanceof Class)) {
336+
// Do not use this shortcut against a Class: Inherited annotations
337+
// would get preferred over locally declared composed annotations.
338+
A annotation = element.getAnnotation(annotationType);
339+
if (annotation != null) {
340+
return AnnotationUtils.synthesizeAnnotation(annotation, element);
341+
}
338342
}
339343

340344
// Exhaustive retrieval of merged annotation attributes...
@@ -478,9 +482,13 @@ public static <A extends Annotation> A findMergedAnnotation(AnnotatedElement ele
478482
Assert.notNull(annotationType, "annotationType must not be null");
479483

480484
// Shortcut: directly present on the element, with no merging needed?
481-
A annotation = element.getDeclaredAnnotation(annotationType);
482-
if (annotation != null) {
483-
return AnnotationUtils.synthesizeAnnotation(annotation, element);
485+
if (!(element instanceof Class)) {
486+
// Do not use this shortcut against a Class: Inherited annotations
487+
// would get preferred over locally declared composed annotations.
488+
A annotation = element.getAnnotation(annotationType);
489+
if (annotation != null) {
490+
return AnnotationUtils.synthesizeAnnotation(annotation, element);
491+
}
484492
}
485493

486494
// Exhaustive retrieval of merged annotation attributes...

0 commit comments

Comments
 (0)