{{ message }}
Repository navigation
UseTextBlocks: Added support for converting string concatenations with variables using .formatted(...) - #1301
Open
jbessels wants to merge 1 commit into
Conversation
…ions into formatted text blocks
Author
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

Finally!! After all these years, I finally got to scratch that itch! 😄
UseTextBlocks: Support converting string concatenations with variables using .formatted(...)
Tbh its AI generated. I checked out all relevant OpenRewrite repo's, fed the AI with existing recipe name and a very careful description. Antigravity with Vertex / Gemini was then tasked to create it. I ordered it to test it all and create edge case tests - tests first, implementation later. Also to check and follow the OpenRewrite naming convention, test convertion etc. The result is shown beneath.
I'm very curious if this is decent code or AI slop. I used AI cause not knowledgable enough to code this OpenRewrite stuff my self (limited free time), the honest truth.
Summary
Extends the
UseTextBlocksrecipe with an opt-informatVariablesconfiguration option. When enabled, multi-line string concatenations that contain variable references or expressions (e.g."SELECT * FROM\n" + table + " WHERE id = " + id) are migrated into a Java 15+ text block combined with.formatted(...).Motivation
Developers migrating legacy Java code to modern Java often encounter multi-line SQL queries, JSON/XML templates, and messages that mix multi-line string literals with variables. Currently,
UseTextBlocksonly migrates pure string literals. Converting concatenated variables into text blocks with.formatted(...)by hand is tedious and error-prone.Before & After
Before:
After (
formatVariables = true):Implementation Details
Strict Backward Compatibility:
@Option boolean formatVariables(defaults tofalse).formatVariables = false, the recipe strictly follows existing behavior with zero changes to existing migrations.Format Specifier Resolution:
%d%b%c%s(rather than%f) to prevent locale-dependent comma vs. dot decimal formatting issues.%sEscaping & Formatting:
%characters inside string segments are safely escaped to%%.\s) and line continuations (\) consistent with existing text block formatting rules.AST Type Attribution:
JavaType.Methodforjava.lang.String.formatted(Object... args)to bothJ.IdentifierandJ.MethodInvocationto satisfyFindMissingTypesstatic analysis.Runtime Compatibility:
-source 8 -target 8).Verification
./gradlew check -x test(all license checks, jar manifests, and recipe CSV validations passed)../gradlew test --tests org.openrewrite.java.migrate.lang.UseTextBlocksTest(all 52 tests passed, including edge cases for adjacent variables, escaped quotes, trailing spaces, and method/field arguments).