Skip to content

AnnotationMetadata.getAnnotationAttributes inconsistency for empty array in ASM implementation [SPR-17347] #21881

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
spring-projects-issues opened this issue Oct 5, 2018 · 1 comment
Assignees
Labels
in: core Issues in core modules (aop, beans, core, context, expression) status: backported An issue that has been backported to maintenance branches type: bug A general bug
Milestone

Comments

@spring-projects-issues
Copy link
Collaborator

spring-projects-issues commented Oct 5, 2018

LutherWest opened SPR-17347 and commented

Different implementations of AnnotationMetadata provides different annotation attributes. Currently, it makes ImportBeanDefinitionRegistrar inconsistent due to provided AnnotationMetadata in registerBeanDefinitions(...) method.

 

For the test case below, StandardAnnotationMetadata will return an empty String array (as expected). But AnnotationMetadataReadingVisitor will return Hello world!.

 

Please, consider the following test case:

@SpringBootTest(classes = ClassAnnotationMetadataTest.class)
@ClassAnnotationMetadataTest.SimpleAnnotation(arrayProperty = {}) // force emptiness
@RunWith(SpringRunner.class)
public class ClassAnnotationMetadataTest {

    @Autowired
    private ResourceLoader resourceLoader;

    private MetadataReaderFactory metadataReaderFactory;

    @Before
    public void setup() {
        this.metadataReaderFactory = new CachingMetadataReaderFactory(resourceLoader);
    }

    @Test
    public void annotationMetadataResultInconsistency() throws IOException {
        AnnotationMetadata meta1 = new StandardAnnotationMetadata(ClassAnnotationMetadataTest.class);
        AnnotationMetadata meta2 = metadataReaderFactory.getMetadataReader(ClassAnnotationMetadataTest.class.getName())
                .getAnnotationMetadata();

        Assertions.assertThat(meta1)
                .isInstanceOf(StandardAnnotationMetadata.class)
                .extracting(meta -> meta.getAnnotationAttributes(SimpleAnnotation.class.getName()))
                .containsAnyOf(entry("arrayProperty", new String[]{ }));
        Assertions.assertThat(meta2)
                .isInstanceOf(AnnotationMetadataReadingVisitor.class)
                .extracting(meta -> meta.getAnnotationAttributes(SimpleAnnotation.class.getName()))
                .containsAnyOf(entry("arrayProperty", new String[]{ }));
    }

    @Retention(RetentionPolicy.RUNTIME)
    @Target(ElementType.TYPE)
    public @interface SimpleAnnotation {
        String[] arrayProperty() default "Hello world!";
    }
}

 

A bit more details, which I can found:

https://github.com/spring-projects/spring-framework/blob/master/spring-core/src/main/java/org/springframework/core/type/classreading/RecursiveAnnotationAttributesVisitor.java#L46

This line will trigger registration of default values for annotation.

For given test case, explicitly specified empty array will be treated as absence of value (associated AnnotationAttributes object won't contain 'arrayProperty' value) and default one will be picked up.

 

Finally

Is it expected behavior?


Affects: 5.0.7

Issue Links:

Referenced from: commits 5343076, 83909e6

Backported to: 5.0.10

@spring-projects-issues
Copy link
Collaborator Author

Juergen Hoeller commented

We consistently expose empty arrays even with ASM parsing now, checking the absence of a visitor callback for an attribute of array type and explicitly exposing an empty array in that case.

@spring-projects-issues spring-projects-issues added type: bug A general bug status: backported An issue that has been backported to maintenance branches in: core Issues in core modules (aop, beans, core, context, expression) labels Jan 11, 2019
@spring-projects-issues spring-projects-issues added this to the 5.1.1 milestone Jan 11, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: core Issues in core modules (aop, beans, core, context, expression) status: backported An issue that has been backported to maintenance branches type: bug A general bug
Projects
None yet
Development

No branches or pull requests

2 participants