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
AnnotationTypeFilter not finding java.lang annotations as meta annotations [SPR-12633] #17234
in: coreIssues in core modules (aop, beans, core, context, expression)status: declinedA suggestion or change that we don't feel we should currently apply
After upgraded from Spring 4.0.2.RELEASE to 4.1.4.RELEASE, I noticed that the AnnotationTypeFilter class is no longer able to find meta annotations. Here's a snippet:
public @interface MetaAnnotation {
}
@MetaAnnotation
public @interface CustomAnnotation {
}
@CustomAnnotation
public class TestBean {
}
@Test
public void metaAnnotationNotWorking() throws Exception {
AnnotationTypeFilter typeFilter = new AnnotationTypeFilter( MetaAnnotation.class, true );
ClassPathScanningCandidateComponentProvider provider = new ClassPathScanningCandidateComponentProvider( false );
provider.addIncludeFilter( typeFilter );
Set<BeanDefinition> candidates = provider.findCandidateComponents( TestBean.class.getPackage().getName() );
assert !candidates.isEmpty();
}
That test passes for me once I add @Retention(RetentionPolicy.RUNTIME) (as required) and make sure that TestBean is declared as static if it is an inner class...
I'm not aware of any regression in that part of the code either. Could you please double-check why it fails for you?
Quite right re: @Retention. When I made the above pseudo-code, I neglected that.
I assumed this was a general issue with meta annotations, but after looking more closely at my application tests it appears as though it's an issue with meta annotations in java.lang.*. It's probably of dubious benefit to even care about this case, admittedly, but in my original tests I had used @Documented and @Component just as a simple way of testing some of my scanning logic. Here's my revised snippet:
// @Component is annotated with @Documented
@Component
public static class TestBean {
}
@Test
public void metaAnnotationNotWorking() throws Exception {
AnnotationTypeFilter typeFilter = new AnnotationTypeFilter( Documented.class, true );
ClassPathScanningCandidateComponentProvider provider = new ClassPathScanningCandidateComponentProvider( false );
provider.addIncludeFilter( typeFilter );
Set<BeanDefinition> candidates = provider.findCandidateComponents( getClass().getPackage().getName() );
assertTrue( candidates.isEmpty(), "Okay, it's working again!" );
}
That test fails for me under 4.0.2.RELEASE, meaning that it does find TestBean. Under 4.1.4.RELEASE, it passes.
When I step through the code with a debugger using the 4.1.4.RELEASE Spring core, I noticed that at line 106 of AnnotationMetadataReadingVisitor.java the variable metaAnnotationMap only contains a key for @Component, and the value is an empty set.
This special handling of java.lang annotations dates back to #16198, making ASM-based meta-annotation scanning more efficient. Since a practical use case for discovering components based on java.lang meta-annotations is rather unlikely, I'll consider this behavior as acceptable.
in: coreIssues in core modules (aop, beans, core, context, expression)status: declinedA suggestion or change that we don't feel we should currently apply
Activity
spring-projects-issues commentedon Jan 20, 2015
Juergen Hoeller commented
That test passes for me once I add
@Retention(RetentionPolicy.RUNTIME)(as required) and make sure thatTestBeanis declared as static if it is an inner class...I'm not aware of any regression in that part of the code either. Could you please double-check why it fails for you?
Juergen
spring-projects-issues commentedon Jan 20, 2015
John Bass commented
Quite right re:
@Retention. When I made the above pseudo-code, I neglected that.I assumed this was a general issue with meta annotations, but after looking more closely at my application tests it appears as though it's an issue with meta annotations in java.lang.*. It's probably of dubious benefit to even care about this case, admittedly, but in my original tests I had used
@Documentedand@Componentjust as a simple way of testing some of my scanning logic. Here's my revised snippet:That test fails for me under 4.0.2.RELEASE, meaning that it does find TestBean. Under 4.1.4.RELEASE, it passes.
When I step through the code with a debugger using the 4.1.4.RELEASE Spring core, I noticed that at line 106 of AnnotationMetadataReadingVisitor.java the variable metaAnnotationMap only contains a key for
@Component, and the value is an empty set.spring-projects-issues commentedon Jan 21, 2015
Juergen Hoeller commented
This special handling of
java.langannotations dates back to #16198, making ASM-based meta-annotation scanning more efficient. Since a practical use case for discovering components based onjava.langmeta-annotations is rather unlikely, I'll consider this behavior as acceptable.Juergen