Favor more locally declared composed annotations over inherited annotations in AnnotationUtils [SPR-11475] #16100
Closed
1 of 2 tasks
Labels
in: core
Issues in core modules (aop, beans, core, context, expression)
type: enhancement
A general enhancement
Milestone
Sam Brannen opened SPR-11475 and commented
Status Quo
The implementations of both
AnnotationUtils
andAnnotatedElementUtils
(and possiblyAbstractRecursiveAnnotationVisitor
as well) currently favor inherited annotations and inherited composed annotations over composed annotations that are declared closer to the starting class passed to thefindAnnotation()
andgetAnnotationAttributes()
methods.Given a class hierarchy with a depth of at least three, if the lowest level (e.g.,
Level3
) is not directly annotated butLevel2
(a direct superclass ofLevel3
) is directly annotated with@ComposedAnno
(which is meta-annotated with@Anno
) andLevel1
(a direct superclass of@Level2
) is directly annotated with either@Anno
or a composed annotation that is meta-annotated with@Anno
, if the@ComposedAnno
annotation is not declared as@Inherited
, then any attributes declared via@Anno
on@ComposedAnno
(present on classLevel2
) will be shadowed by those declared via@Anno
on classLevel1
.This behavior is very non-intuitive and would likely be considered a bug by any developers who encounter it.
Concrete Example
Given...
If we execute the following unit test, one would likely expect that "composed2" should be found, since the immediate superclass is annotated with
@Composed2
; however, with the current implementation "composed1" will be found since@Composed1
is declared as@Inherited
and therefore shadows@Composed2
. As such, the test fails on the last line.Proposal
Refactor the affected implementations of
AnnotationUtils
andAnnotatedElementUtils
so that more locally declared composed annotations are favored over inherited annotations and inherited composed annotations.This can likely be achieved by using the
getDeclaredAnnotation()
andgetDeclaredAnnotations()
methods injava.lang.Class
instead of thegetAnnotation()
andgetAnnotations()
which are currently being used in these utility classes.Note that
MetaAnnotationUtils
already usesgetDeclaredAnnotations()
.Deliverables
AnnotationUtils
to usegetDeclaredAnnotation()
andgetDeclaredAnnotations()
where appropriate.AnnotatedElementUtils
to usegetDeclaredAnnotation()
andgetDeclaredAnnotations()
where appropriate.Affects: 4.0 GA
Issue Links:
Referenced from: commits a2f1169, 0f5a27c, 1d30bf8, 0637864, 90b938a
The text was updated successfully, but these errors were encountered: