Problem
As of Spring 5.2 AnnotationUtils.findAnnotation
is not searching for nested annotations properly.
Sample:
@Deprecated
@Retention(RetentionPolicy.RUNTIME)
@Target(value = { ElementType.TYPE })
public @interface CustomAnnotation {
}
@CustomAnnotation
public class CustomType {
}
In Spring 5.1.x the following code returns proper result:
Deprecated annotation = AnnotationUtils.findAnnotation(CustomType.class, Deprecated.class);
but not in Spring 5.2.x.
Reproduce
- Checkout: https://github.com/alexey-anufriev/spring-framework-annotation-utils-bug
- Run
mvn clean test
to reproduce.
Comment From: dmitrijza
Correction: It's working properly, but only for annotations that doesn't located in the JDK packages, seems it's because of additional checks in AnnotationUtils
with AnnotationFilter
, that has appeared later (and AnnotationFilter.PLAIN
for packages under java.*), in the Spring 5.2.x. and now working perfectly for AnnotationUtils.isInJavaLangAnnotationPackage(...)
.
Short: AnnotationUtils
in Spring 5.2.x doesn't look for annotations in java.lang
or java.lang.annotation
. That's why you got null
when you try to find java.lang.Deprecated
in the class.
Comment From: alexey-anufriev
@sq179, thank you for the explanation, I have also tried to dig the source code and figured out the same reason, but still, it was not reported as a backward-incompatible change, so I would like to understand if this is: - regression and will be fixed; - backward-incompatible change and I have to apply custom solution here.
Comment From: nielsm5
I doubt they will fix it, see issue #23929 where they defined/limited the scope.
Comment From: alexey-anufriev
@nielsm5, good point, thank you.
@jhoeller, shall this one be also closed as the referenced one?
Comment From: snicoll
Yes. Duplicate #23929