Repository navigation
Use Java text blocks for query conversion in Java 15+ - #1762
Conversation
Signed-off-by: Junhwan Kim <[email protected]>
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
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(); |
There was a problem hiding this comment.
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) { |
There was a problem hiding this comment.
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(\"\"\"")) { |
There was a problem hiding this comment.
why the if? I think for a test project it should be either one or the other.
Signed-off-by: Junhwan Kim <[email protected]>
|
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? |
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
createFixDescriptorto be a static utility, ensuring consistent behavior betweenCodeLensandCodeAction.Applied changes across all supported modules: JDBC, JPA, and MongoDB.
Resolved Issue
\") in generated@Queryannotations, particularly for JDBC SQL and MongoDB JSON queries.Discussion Points
""". While standard SQL/JSON queries work fine, we may need to consider if additional escaping for internal"""sequences is necessary for edge cases.