Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,11 @@ private static boolean isRegularStringLiteral(Expression expr) {
J.Literal l = (J.Literal) expr;
return TypeUtils.isString(l.getType()) &&
l.getValueSource() != null &&
!l.getValueSource().startsWith("\"\"\"");
!l.getValueSource().startsWith("\"\"\"") &&
// Defend against literals whose value still looks like the source (e.g. parser failed to
// decode supplementary characters encoded as surrogate pairs); skipping prevents emitting
// a broken text block that drops or re-escapes content.
!l.getValueSource().equals(l.getValue());
}
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -995,6 +995,27 @@ class Test {
);
}

@Issue("https://github.com/openrewrite/rewrite-migrate-java/issues/1110")
@Test
void supplementaryCharacterAsSurrogatePair() {
// The Java parser currently returns a corrupted value/valueSource for string literals containing
// supplementary characters encoded as surrogate pairs. Until that is fixed upstream, the recipe
// must bail out rather than emit a broken text block that drops or re-escapes content.
rewriteRun(
spec -> spec.recipe(new UseTextBlocks()),
java(
"""
class Test {
String json =
"{\\n"
+ " \\"euro\\": 1,\\n"
+ " \\"ligature\\": 2,\\n"
+ " \\"\\ud834\\udd20\\": 3\\n"
+ "}";
}""",
src -> src.markers(javaVersion(17))));
}

@Issue("https://github.com/openrewrite/rewrite-migrate-java/issues/555")
@Test
void textBlockTrailingEscape() {
Expand Down
Loading