diff --git a/src/main/java/org/openrewrite/java/migrate/lang/UseTextBlocks.java b/src/main/java/org/openrewrite/java/migrate/lang/UseTextBlocks.java index 2d6a5b7989..7a5a234e32 100644 --- a/src/main/java/org/openrewrite/java/migrate/lang/UseTextBlocks.java +++ b/src/main/java/org/openrewrite/java/migrate/lang/UseTextBlocks.java @@ -125,7 +125,9 @@ private J.Literal toTextBlock(J.Binary binary, String content, List s StringBuilder sb = new StringBuilder(); StringBuilder originalContent = new StringBuilder(); - stringLiterals = stringLiterals.stream().filter(s -> s.getValue() != null && !s.getValue().toString().isEmpty()).collect(Collectors.toList()); + stringLiterals = stringLiterals.stream() + .filter(s -> s.getValue() != null && !s.getValue().toString().isEmpty()) + .collect(Collectors.toList()); for (int i = 0; i < stringLiterals.size(); i++) { String s = requireNonNull(stringLiterals.get(i).getValue()).toString(); sb.append(s); @@ -139,8 +141,6 @@ private J.Literal toTextBlock(J.Binary binary, String content, List s } } - content = sb.toString(); - TabsAndIndentsStyle tabsAndIndentsStyle = Optional.ofNullable(getCursor().firstEnclosingOrThrow(SourceFile.class) .getStyle(TabsAndIndentsStyle.class)).orElse(IntelliJ.tabsAndIndents()); boolean useTab = tabsAndIndentsStyle.getUseTabCharacter(); @@ -148,12 +148,11 @@ private J.Literal toTextBlock(J.Binary binary, String content, List s String indentation = getIndents(concatenation, useTab, tabSize); - boolean isEndsWithNewLine = content.endsWith("\n"); - // references: // - https://docs.oracle.com/en/java/javase/14/docs/specs/text-blocks-jls.html // - https://javaalmanac.io/features/textblocks/ + content = sb.toString(); // escape backslashes content = content.replace("\\", "\\\\"); // escape triple quotes @@ -170,13 +169,20 @@ private J.Literal toTextBlock(J.Binary binary, String content, List s // add last line to ensure the closing delimiter is in a new line to manage indentation & remove the // need to escape ending quote in the content - if (!isEndsWithNewLine) { + if (isEndsWithSpecialCharacters(sb.toString())) { content = content + "\\\n" + indentation; } return new J.Literal(randomId(), binary.getPrefix(), Markers.EMPTY, originalContent.toString(), String.format("\"\"\"%s\"\"\"", content), null, JavaType.Primitive.String); } + + private boolean isEndsWithSpecialCharacters(String content) { + return content.endsWith("\"") || + content.endsWith("=") || + content.endsWith("}") || + content.endsWith(";"); + } }); } diff --git a/src/test/java/org/openrewrite/java/migrate/lang/UseTextBlocksTest.java b/src/test/java/org/openrewrite/java/migrate/lang/UseTextBlocksTest.java index 8cc9d71d21..995e9acd8a 100644 --- a/src/test/java/org/openrewrite/java/migrate/lang/UseTextBlocksTest.java +++ b/src/test/java/org/openrewrite/java/migrate/lang/UseTextBlocksTest.java @@ -88,8 +88,7 @@ class Test { String query = \""" SELECT * FROM \\s my_table \\s - WHERE something = 1; \\ - \"""; + WHERE something = 1; \"""; } """ ) @@ -738,8 +737,7 @@ class Test { class Test { String eightQuotes = ""\" ""\\""\"\\""\"\\ - after 8 quotes\\ - ""\"; + after 8 quotes""\"; } """ ) @@ -860,4 +858,30 @@ void shouldNotUpdateKotlinCode() { ) ); } + + @Test + @Issue("https://github.com/openrewrite/rewrite-migrate-java/issues/555") + void textBlockTrailingEscape() { + rewriteRun( + spec -> spec.recipe(new UseTextBlocks()), + java( + """ + package com.helloworld; + + public class Main { + String foo = + "hello\\n" + + "world"; + }""", + """ + package com.helloworld; + + public class Main { + String foo = + \""" + hello + world\"""; + }""", + src -> src.markers(javaVersion(17)))); + } }