Skip to content

AnnotationTypeFilter not finding java.lang annotations as meta annotations [SPR-12633] #17234

Closed

Description

@spring-projects-issues

John Bass opened SPR-12633 and commented

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();
}

Affects: 4.1.4

Issue Links:

Activity

  1. spring-projects-issues commented on Jan 20, 2015

    @spring-projects-issues
    CollaboratorAuthor

    Juergen Hoeller commented

    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?

    Juergen

  2. spring-projects-issues commented on Jan 20, 2015

    @spring-projects-issues
    CollaboratorAuthor

    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 @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.

  3. spring-projects-issues commented on Jan 21, 2015

    @spring-projects-issues
    CollaboratorAuthor

    Juergen Hoeller commented

    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.

    Juergen

  4. added
    type: bug
    status: declined
    in: core
    on Jan 11, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

in: corestatus: declined

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions