Repository navigation
Conversation
…ions into formatted text blocks
|
Related ticket by me: #261 |
|
I've asked the AI to review it and this is what it gave me among others: Correctness issues
Suggestion:
|
…ions into formatted text blocks
|
Related ticket by me: #261 |
|
I've asked the AI to review it and this is what it gave me among others: Correctness issues
Suggestion:
|
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).