Skip to content

Fix context-sensitive template replacement for standalone expression statements#7211

Merged
timtebeek merged 1 commit intomainfrom
tim/fix-template-statement-gen
Mar 31, 2026
Merged

Fix context-sensitive template replacement for standalone expression statements#7211
timtebeek merged 1 commit intomainfrom
tim/fix-template-statement-gen

Conversation

@timtebeek
Copy link
Copy Markdown
Member

Summary

  • Fixes JavaTemplate failing with "generated 0 statements" when replacing a standalone method invocation expression statement inside a non-void method using a context-sensitive template
  • Root cause: the semicolon terminating the expression was placed after the if(true) block close brace and return statement in the generated stub, instead of directly after the template content
  • Discovered via UseVisitWithParentCursor recipe failing on AbstractLogEnabledToSlf4j.java#L110

Test plan

  • Added replaceContextSensitiveMethodInvocationStandaloneStatement test in JavaTemplateTest8Test
  • All JavaTemplateTest8Test tests pass
  • All rewrite-java and rewrite-java-test module tests pass

…statements in non-void methods

When a JavaTemplate replaces a standalone method invocation expression statement
inside a non-void method, the generated template stub was missing a semicolon
after the expression. The semicolon was appended to the `after` buffer after
the `}\nreturn ...;` from the non-void method wrapping, placing it outside the
`if(true)` block instead of directly after the template content. This caused
`parseBlockStatements` to return 0 results.

Fix by using `insert(0, ';')` instead of `append(';')` so the semicolon is
placed before the closing brace of the `if(true)` block.
@github-project-automation github-project-automation Bot moved this from In Progress to Ready to Review in OpenRewrite Mar 31, 2026
@timtebeek
Copy link
Copy Markdown
Member Author

Test failure unrelated; merging:

MavenPomDownloaderTest > WithNativeHttpURLConnectionAndTLS > useHttpWhenHttpsFails() FAILED
    org.opentest4j.AssertionFailedError: 
    expected: MavenRepository(id=id, uri=http://localhost:37747/maven/, releases=null, snapshots=null, knownToExist=false, username=null, *** timeout=null, deriveMetadataIfMissing=null)
     but was: null
        at app//org.openrewrite.maven.internal.MavenPomDownloaderTest$WithNativeHttpURLConnectionAndTLS.useHttpWhenHttpsFails(MavenPomDownloaderTest.java:357)

@timtebeek timtebeek merged commit ded268e into main Mar 31, 2026
1 check failed
@timtebeek timtebeek deleted the tim/fix-template-statement-gen branch March 31, 2026 10:09
@github-project-automation github-project-automation Bot moved this from Ready to Review to Done in OpenRewrite Mar 31, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

2 participants