You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Support repeatable annotations as composed annotations [SPR-13973] #18545
Calling AnnotationUtils.getRepeatableAnnotations(MetaMockBean.class,MockBean.class, MockBeans.class) will return a synthesized annotation. However, calling value() on the result returns Void.class rather than the expected ExampleService.class.
Basically, none of the methods in AnnotationUtils support merging of annotation attributes within an annotation hierarchy.
Only AnnotatedElementUtils supports merging of attributes (i.e., composed annotations).
Regarding the (original) subject of this issue, getRepeatableAnnotations does actually support @AliasFor, however only within a single annotation.
In other words, we do not currently have any search algorithms that support merging of annotation attributes within an annotation hierarchy for repeatable annotations. So... if that's something you'd like to have support for, we could create another issue or repurpose this one.
Sam Brannen, I was about to create such an issue myself, actually... since it seems to be the root cause of #18376. Definitely something to sort out for 4.3, I guess as a new variant of repeatable annotation lookup on AnnotatedElementUtils? Let's repurpose this issue for it.
Our main remaining use of getRepeatableAnnotations is for @JmsListener, @Scheduled and JMX ManagedNotification / ManagedOperationParameter lookup, as well as Sql lookup in the test context framework, all for very similar purposes. So requirement-wise, we just need a variant of the search algorithm that works for those.
That's probably the biggest gap that we have in our annotation composability story at this point, so worth some priority.
Juergen Hoeller, yes, this new functionality would have to reside in AnnotatedElementUtils, keeping in line with the documentation for AnnotationUtils:
Meta-annotation Support
... For support for meta-annotations with attribute overrides in composed annotations, use AnnotatedElementUtils instead.
Support repeatable annotations as composed annotations
Prior to this commit, AnnotationUtils supported searching for
repeatable annotations even if the repeatable annotation was declared
on a custom stereotype annotation. However, there was no support for
merging of attributes in composed repeatable annotations. In other
words, it was not possible for a custom annotation to override
attributes in a repeatable annotation.
This commit addresses this by introducing
findMergedRepeatableAnnotations() methods in AnnotatedElementUtils.
These new methods provide full support for explicit annotation
attribute overrides configured via @AliasFor (as well as
convention-based overrides) with "find semantics".
Based on your comments above, I went ahead and migrated the lookups for @JmsListener, @Scheduled, @ManagedNotification, @ManagedOperationParameter, and @Sql to use the new AnnotatedElementUtils.getMergedRepeatableAnnotations() method.
I have also added tests for all of these, except for @ManagedOperationParameter.
When I started writing a test for @ManagedOperationParameter I realized that it can only be declared on methods and therefore cannot be used to create a composed annotation to begin with, which leads to the following questions:
Do we want to allow @ManagedOperationParameter (and all related JMX annotations) to be used as meta-annotations?
If not, should we revert to the old algorithm in AnnotationUtils for @ManagedOperationParameter lookups?
Activity
spring-projects-issues commentedon Feb 23, 2016
Sam Brannen commented
Yes, I do believe this is the expected behavior.
Basically, none of the methods in
AnnotationUtilssupport merging of annotation attributes within an annotation hierarchy.Only
AnnotatedElementUtilssupports merging of attributes (i.e., composed annotations).Regarding the (original) subject of this issue,
getRepeatableAnnotationsdoes actually support@AliasFor, however only within a single annotation.In other words, we do not currently have any search algorithms that support merging of annotation attributes within an annotation hierarchy for repeatable annotations. So... if that's something you'd like to have support for, we could create another issue or repurpose this one.
spring-projects-issues commentedon Feb 23, 2016
Juergen Hoeller commented
Sam Brannen, I was about to create such an issue myself, actually... since it seems to be the root cause of #18376. Definitely something to sort out for 4.3, I guess as a new variant of repeatable annotation lookup on
AnnotatedElementUtils? Let's repurpose this issue for it.Juergen
spring-projects-issues commentedon Feb 23, 2016
Juergen Hoeller commented
Our main remaining use of
getRepeatableAnnotationsis for@JmsListener,@Scheduledand JMXManagedNotification/ManagedOperationParameterlookup, as well asSqllookup in the test context framework, all for very similar purposes. So requirement-wise, we just need a variant of the search algorithm that works for those.That's probably the biggest gap that we have in our annotation composability story at this point, so worth some priority.
spring-projects-issues commentedon Feb 25, 2016
Sam Brannen commented
Note that this issue is related to #18054 and #17490.
spring-projects-issues commentedon Feb 25, 2016
Sam Brannen commented
Juergen Hoeller, yes, this new functionality would have to reside in
AnnotatedElementUtils, keeping in line with the documentation forAnnotationUtils:spring-projects-issues commentedon Mar 23, 2016
Sam Brannen commented
Completed as described in GitHub commit 2535469:
spring-projects-issues commentedon Mar 23, 2016
Sam Brannen commented
Phil Webb, please give this a try in the next 4.3 snapshot and let me know if it meets your needs.
Cheers!
spring-projects-issues commentedon Mar 23, 2016
Sam Brannen commented
Reopening in order to introduce
getMergedRepeatableAnnotations(..)methods inAnnotatedElementUtilsthat search using get semantics.This is needed, for example, in
SqlScriptsTestExecutionListenerwhere find semantics for repeatable annotations are not applicable.spring-projects-issues commentedon Mar 25, 2016
Sam Brannen commented
Added support for "get" search semantics in 46e0484
spring-projects-issues commentedon Mar 26, 2016
Sam Brannen commented
Juergen Hoeller,
Based on your comments above, I went ahead and migrated the lookups for
@JmsListener,@Scheduled,@ManagedNotification,@ManagedOperationParameter, and@Sqlto use the newAnnotatedElementUtils.getMergedRepeatableAnnotations()method.I have also added tests for all of these, except for
@ManagedOperationParameter.When I started writing a test for
@ManagedOperationParameterI realized that it can only be declared on methods and therefore cannot be used to create a composed annotation to begin with, which leads to the following questions:@ManagedOperationParameter(and all related JMX annotations) to be used as meta-annotations?AnnotationUtilsfor@ManagedOperationParameterlookups?spring-projects-issues commentedon Mar 29, 2016
Sam Brannen commented
Team Decisions
@ManagedOperationParameter(and all related JMX annotations) to be used as meta-annotations?AnnotationUtilsfor@ManagedOperationParameterlookups?