Skip to content
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

Support repeated annotations in AnnotationContainer #1624

Open
ccarbonellbh opened this issue Feb 7, 2025 · 2 comments
Open

Support repeated annotations in AnnotationContainer #1624

ccarbonellbh opened this issue Feb 7, 2025 · 2 comments

Comments

@ccarbonellbh
Copy link

ccarbonellbh commented Feb 7, 2025

TypeDeclaration was updated to use AnnotationContainer (previously a List of Annotations), and the methods exposed only allow for adding a single instance of a given annotation. With the move to AnnotationContainer, I seem to no longer be able to generate code that looks like below:

@PropertySource(value = {"file:${properties.directory}/${properties.file}"}, ignoreResourceNotFound = true, factory = GenericPropertySourceFactory.class)
@PropertySource(value = {"file:${properties.directory}/qa-overrides.properties"}, ignoreResourceNotFound = true)
public class MyApplication extends SpringBootServletInitializer {
...

Is there a suggested way that this should now be handled? Should AnnotationContainer be modified to still support this workflow?

Thank you!

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Feb 7, 2025
@mhalbritter mhalbritter changed the title Cannot add multiple instances of the same annotation to TypeDeclaration Support repeated annotations in AnnotationContainer Feb 10, 2025
@mhalbritter mhalbritter added type: enhancement and removed status: waiting-for-triage An issue we've not yet triaged labels Feb 10, 2025
@mhalbritter
Copy link
Contributor

mhalbritter commented Feb 10, 2025

Hello,

that's true, AnnotationContainer doesn't support repeated annotations.

As a workaround, until support for this has been implemented, you can use this workaround:

JavaTypeDeclaration test = compilationUnit.createTypeDeclaration("Test");
Annotation annotation1 = Annotation.of(ClassName.of(PropertySource.class))
    .set("value", "classpath:1.properties")
    .build();
Annotation annotation2 = Annotation.of(ClassName.of(PropertySource.class))
    .set("value", "classpath:2.properties")
    .build();
test.annotations()
    .add(ClassName.of(PropertySources.class),
            (annotation) -> annotation.set("value", annotation1, annotation2));

This uses PropertySources, which is the repeatable annotation container for PropertySource:

@Repeatable(PropertySources.class)
public @interface PropertySource {
   // ...
}

The generated code looks like this:

package com.example;

import org.springframework.context.annotation.PropertySource;
import org.springframework.context.annotation.PropertySources;

@PropertySources({ @PropertySource("classpath:1.properties"), @PropertySource("classpath:2.properties") })
class Test {

}

While this isn't optimal code, it compiles and works.

@ccarbonellbh
Copy link
Author

@mhalbritter Thank you for the suggested workaround. I will use that approach until AnnotationContainer gets support for repeatable annotations!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants