Skip to content

Conversation

@knutwannheden
Copy link
Contributor

@knutwannheden knutwannheden commented Jan 14, 2026

When using JavaTemplate to add annotations to method declarations inside anonymous inner classes with multiple constructor arguments, the template context generation would fail because it didn't properly handle the case where the parent of a J.Block is a J.NewClass (anonymous class).

This fix:

  1. Adds handling in contextTemplate for J.Block whose parent is J.NewClass
  2. Creates a new anonymousClassDeclaration method to generate proper context
  3. Adds a check in J.NewClass handling to avoid duplicate context when prior is the body of the anonymous class

…rite#6520)

When using JavaTemplate to add annotations to method declarations inside
anonymous inner classes with multiple constructor arguments, the template
context generation would fail because it didn't properly handle the case
where the parent of a J.Block is a J.NewClass (anonymous class).

This fix:
1. Adds handling in contextTemplate for J.Block whose parent is J.NewClass
2. Creates a new anonymousClassDeclaration method to generate proper context
3. Adds a check in J.NewClass handling to avoid duplicate context when prior
   is the body of the anonymous class
@timtebeek
Copy link
Member

Copying the failure here for easier reference:

JavaTemplateTest6Test > annotateMethodInAnonymousClassWithMultipleArgs() FAILED
    java.lang.AssertionError: Recipe was expected to make a change but made no changes.
        at org.openrewrite.test.LargeSourceSetCheckingExpectedCycles.afterCycle(LargeSourceSetCheckingExpectedCycles.java:119)
        at org.openrewrite.RecipeScheduler.runRecipeCycles(RecipeScheduler.java:97)
        at org.openrewrite.RecipeScheduler.scheduleRun(RecipeScheduler.java:42)
        at org.openrewrite.Recipe.run(Recipe.java:451)
        at org.openrewrite.test.RewriteTest.rewriteRun(RewriteTest.java:400)
        at org.openrewrite.test.RewriteTest.rewriteRun(RewriteTest.java:132)
        at org.openrewrite.java.JavaTemplateTest6Test.annotateMethodInAnonymousClassWithMultipleArgs(JavaTemplateTest6Test.java:507)

@knutwannheden knutwannheden changed the title Fix JavaTemplate parsing failure for anonymous inner classes (#6520) Fix JavaTemplate parsing failure for anonymous inner classes Jan 14, 2026
knutwannheden and others added 2 commits January 14, 2026 11:34
The original PR fix for BlockStatementTemplateGenerator handles anonymous
classes correctly, but the test case uses annotation coordinates which
go through AnnotationTemplateGenerator instead. This commit adds the
same handling to AnnotationTemplateGenerator:

- Handle J.Block with parent J.NewClass (anonymous class body)
- Handle J.NewClass when prior is the body to avoid duplicate context
- Handle J.NewClass when prior is an argument to wrap properly
- Add anonymousClassDeclaration helper method for anonymous class context
- Add referToSameElement helper for proper element comparison

Also includes a workaround for jsonrpc library API change where
HeaderDelimitedMessageHandler no longer accepts a formatter parameter.

Co-Authored-By: Claude Opus 4.5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: In Progress

Development

Successfully merging this pull request may close these issues.

AnnotateNullableMethods might crash with JavaTemplate parsing failure

3 participants