Skip to content

Use Java text blocks for query conversion in Java 15+ - #1762

Merged
BoykoAlex merged 2 commits into
spring-projects:mainfrom
catturtle123:main
Jan 21, 2026
Merged

BoykoAlex merged 2 commits into
spring-projects:mainfrom
catturtle123:main

Conversation

@catturtle123

Copy link
Copy Markdown
Contributor

This PR addresses issue #1726

Summary

  • JRE Version-Aware Query Generation: Added logic to detect the project's Java version using IJavaProject.

  • Java 15+ Text Block Support:

  • If the project is running on Java 15 or higher and the query string contains quotes ("), the generator now uses Java Text Blocks (""").

  • For Java versions below 15 or queries without quotes, it falls back to the standard escaped string literal behavior.

  • Unified Implementation:

  • Refactored createFixDescriptor to be a static utility, ensuring consistent behavior between CodeLens and CodeAction.

  • Applied changes across all supported modules: JDBC, JPA, and MongoDB.

Resolved Issue

  • Improved Readability: Eliminates excessive escaping (e.g., \") in generated @Query annotations, particularly for JDBC SQL and MongoDB JSON queries.

Discussion Points

  • Escaping in Text Blocks: Currently, the raw query string is wrapped in """. While standard SQL/JSON queries work fine, we may need to consider if additional escaping for internal """ sequences is necessary for edge cases.

@BoykoAlex BoykoAlex left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I was thinking that maybe it is not worth adding a test case for each type of repository (mongo, jpa, etc). I'd add a test for one of them only. If test projects are boot 4 then it should be java 17 up... might be challenging to test against java version < 15 perhaps... AOT for spring data with query generation is only available from Boot 4 which means we'd always have the text block support since java 17 is minimum for boot 4 release train. We should just always create a text block from query and forget about escaping the the string value I think... What do you think?

String key = entry.getKey();
String value = entry.getValue();
if (value == null) continue;
String escaped = org.apache.commons.text.StringEscapeUtils.escapeJava(value);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I'd move the escape value computations inside the else block (java version < 15)

assertNotNull(queryValue, "Query value should not be null");

int javaVersion = 8;
String versionStr = testProject.getClasspath().getJre().version();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

isn't this version constant for the test project? (defined in maven pom somewhere). I think you should just assert either text block query or string literal query not have an if for it

}


private String extractValueFromAttributes(CodeLens codeLens) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Isn't this method repeated for all tests?

docEdit.getEdits().get(0).getNewText().trim());
String rawNewText = docEdit.getEdits().get(0).getNewText().trim();

if (rawNewText.startsWith("@Query(\"\"\"")) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

why the if? I think for a test project it should be either one or the other.

@catturtle123

catturtle123 commented Jan 21, 2026 •

Copy link
Copy Markdown
Contributor Author

I agree with the simplification and have updated the code to always use text blocks. I have also consolidated the test cases into a single representative test in JdbcTest as you suggested. (I've updated the validation strings in the JPA and MongoDB query tests to align with the change to the text block format in the main logic.) I'm still thinking about whether we should handle backslash escaping to prevent potential compilation errors—what do you think?

@BoykoAlex BoykoAlex left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM

@BoykoAlex
BoykoAlex merged commit 69fcfb7 into spring-projects:main Jan 21, 2026
1 check passed
@martinlippert martinlippert added this to the 5.1.0.RELEASE milestone Jan 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants