Skip to content

Add support for repeatable annotations #1670

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

Open
wants to merge 27 commits into
base: main
Choose a base branch
from

Conversation

YangSiJun528
Copy link
Contributor

@YangSiJun528 YangSiJun528 commented Jul 9, 2025

Resolves issue #1624 by adding support for multiple annotations of the same type.

Key changes:

  • Allows AnnotationContainer to support multiple annotations
  • Fixes KotlinSourceCodeWriter#writeProperty to correctly write annotations
  • Adds tests for repeated annotations

Fixes gh-1624

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Jul 9, 2025
@mhalbritter
Copy link
Contributor

Hello! The build is failing because of formatting violations. You can check them locally by running ./mvnw verify.

@YangSiJun528
Copy link
Contributor Author

I made the fix and confirmed it works locally.

@mhalbritter
Copy link
Contributor

Hello,

thanks for the PR! I might have missed something here, but why did you create a second implementation for the annotations? Is there a reason we couldn't add the functionality for repeated annotations into the AnnotationContainer itself?

@mhalbritter mhalbritter added the status: waiting-for-feedback We need additional information before we can continue label Jul 14, 2025
@YangSiJun528

This comment was marked as resolved.

@spring-projects-issues spring-projects-issues added status: feedback-provided Feedback has been provided and removed status: waiting-for-feedback We need additional information before we can continue labels Jul 14, 2025
@YangSiJun528
Copy link
Contributor Author

YangSiJun528 commented Jul 15, 2025

After thinking more about your previous comment, I realized there might be a better approach that allows AnnotationContainer itself to support repeated annotations.
This way, it adds support for repeatable annotations without many code changes.

You can see it in this commit — I've confirmed that it doesn't break any existing tests:
YangSiJun528@488ce5a

Sorry if my earlier proposal ended up taking unnecessary time.

Do you think this approach would be better?
If you agree, I can update the PR with this version.

@mhalbritter
Copy link
Contributor

Yes, please update the PR. Thanks!

@YangSiJun528
Copy link
Contributor Author

I've updated the PR. @mhalbritter

* @return {@code true} if an annotation with the specified name is present, otherwise
* {@code false}
*/
public boolean has(String name) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't really like the concept of an annotation name. What do you think about deprecating the add methods, and add two new methods: addSingle and addRepeatable.

addSingle behaves essentially like add but if there is more than one annotation of the given class and a customizer is provided it fails. Also addSingle doesn't add a annotation if it's already there.

addRepeatable can't customize already existing annotations, it always adds a new one.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought AnnotationContainer needed a key to identify specific annotations when there are multiple ones of the same type.

But you're probably right that the annotation name concept adds unnecessary complexity.

So you're suggesting addSingle/addRepeatable would be cleaner because most use cases don't need to handle specific instances of repeatable annotations?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I find the existing add method somewhat confusing in that it either adds or modifies the existing one. That behavior is not applicable to repeatable annotations.

We could also think about adding:

  • addSingle (fails if there is already one of the same type)
  • addRepeatable
  • addSingleOrCustomize (essentially the existing add method - but fails if there are multiple annotations of that type registered already. Allows to customize the annotation if there is one already).

I have to play around a bit.

Copy link
Contributor

@mhalbritter mhalbritter Jul 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One more data point to split the functionality from add and customize: io.spring.initializr.generator.buildsystem.gradle.GradleConfigurationContainer has separate add and customize methods.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

However, io.spring.initializr.generator.buildsystem.gradle.GradlePluginContainer also combines add and customize. Hm.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, customize might be a fine name too.
From a ClassName perspective, customize and replace seem to have similar meanings.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good!
I think this is much cleaner than the previous implementation.

I found that single annotations have customize functionality, but repeatable annotations don't.
Do you think repeatable annotations should also have customize features like single annotations do?

If so, methods like customizeAllRepeatable, customizeRepeatableIf, or removeRepeatableIf might be helpful for more granular control over repeatable annotations.

What do you think? Should I look into adding these features?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for looking at it. Do you want to take this code into your PR?

I don't think customizing repeatable annotations make sense. We can add this functionality later, if the need arises.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, I'll update the PR based on the code you provided and mention you.

@mhalbritter mhalbritter added status: waiting-for-feedback We need additional information before we can continue and removed status: feedback-provided Feedback has been provided labels Jul 17, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: waiting-for-feedback We need additional information before we can continue status: waiting-for-triage An issue we've not yet triaged
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support repeated annotations in AnnotationContainer
3 participants