Skip to content

Fix AnnotationTemplateGenerator to handle nested annotations correctly#5713

Merged
knutwannheden merged 6 commits intomainfrom
issue-5712
Jul 7, 2025
Merged

Fix AnnotationTemplateGenerator to handle nested annotations correctly#5713
knutwannheden merged 6 commits intomainfrom
issue-5712

Conversation

@knutwannheden
Copy link
Copy Markdown
Contributor

@knutwannheden knutwannheden commented Jul 4, 2025

The AnnotationTemplateGenerator was failing with an IndexOutOfBoundsException when processing nested
annotations (e.g., repeatable annotations within annotation arrays). This occurred because the template
generator couldn't find a "typical annotation target" for annotations nested inside other annotations' array
values.

The fix adds proper handling for nested annotations by:

  1. Walking up the parent hierarchy to find the ultimate annotation target (method, variable, or class)
  2. Adding a fallback case that generates @interface $Placeholder {} when no typical target can be found
  3. Updating the template() method to handle nested annotation cases consistently

This ensures that JavaTemplate can successfully process and transform nested annotations without throwing
exceptions.

Added a test case that demonstrates the fix by transforming nested repeatable annotations' attributes using
JavaTemplate.

The `AnnotationTemplateGenerator` was failing with an `IndexOutOfBoundsException` when processing nested
annotations (e.g., repeatable annotations within annotation arrays). This occurred because the template
generator couldn't find a "typical annotation target" for annotations nested inside other annotations' array
values.

The fix adds proper handling for nested annotations by:
1. Walking up the parent hierarchy to find the ultimate annotation target (method, variable, or class)
2. Adding a fallback case that generates `@interface $Placeholder {}` when no typical target can be found
3. Updating both the `template()` and `cacheKey()` methods to handle nested annotation cases consistently

This ensures that `JavaTemplate` can successfully process and transform nested annotations without throwing
exceptions.

Added a test case that demonstrates the fix by transforming nested repeatable annotations' attributes using
`JavaTemplate`.

Fixes:
- #5712
@github-project-automation github-project-automation Bot moved this to In Progress in OpenRewrite Jul 4, 2025
@knutwannheden knutwannheden marked this pull request as draft July 4, 2025 02:22
Copy link
Copy Markdown
Member

@timtebeek timtebeek left a comment

Choose a reason for hiding this comment

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

Approved already; could merge as is, but wasn't sure given that you had marked this as a draft.

@github-project-automation github-project-automation Bot moved this from In Progress to Ready to Review in OpenRewrite Jul 4, 2025
@timtebeek timtebeek added the bug Something isn't working label Jul 4, 2025
@timtebeek timtebeek marked this pull request as ready for review July 4, 2025 18:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

AnnotationTemplateGenerator does not handle nested annotations correctly

2 participants