I find myself having to lookup annotations from an ordered set of different locations and use the information from the first location a particular annotation is present. The particular use case is to find some object mapping annotations on some property accessor methods first but falling back to the field backing a property.
I currently keep a List<MergedAnnotations>
with the accessor method derived instances first, the one created from the field last and stream over them, check for whether the annotation is present, if so use that and continue looking for it otherwise (see this). It would be cool if that external iteration could be hidden behind a MergedAnnotations
instance in the first place.
/cc @philwebb
Comment From: philwebb
I think it should be relatively easy to change the of(Element)
method to take an array, however, I think we should probably limit this to SearchStrategy.DIRECT
(at least for now).
There are some nasty complications with ordering if we start to try and support any of the more complex searching scenarios. For example, if you have the following:
@A
public abstract static class Animal {
@B
public abstract void run();
}
@C
public static class Cat {
@D
public void run() {
}
}
Currently if you asked for annotation on the method you'd get @D
then @B
. If you asked for the class you'd get @C
then @A
. The aggregate indexes are 0
for D and C and 1
for B and A. Actually, things are a little more complicated than that because we also provide the meta annotations.
If we support a var-args of
we might have MergedAnnotations.of(catClass, catRunMethod)
. The ordering could be @C
, @D
, @A
, @B
or it could be @C
, @A
, @D
, @B
. The first would be easy to implement but would give aggregate indexes of 0
, 1
, 0
, 1
which is a bit unusual. The second would require a major refactoring of AnnotationsScanner
(which I don't want to do).
Comment From: philwebb
@odrotbohm Would something like this meet your needs?
https://github.com/philwebb/spring-framework/commit/91f63ecb5443b9bbd70f07010d171b75094265b2#diff-eba2e417e8bfe63f7a164175972bad53R2101
Comment From: odrotbohm
Yes, that's pretty exactly what I need.
Comment From: philwebb
Closing in favor of PR #23344