From 07443b1ece4744963bc4159cdd3edcef5d4b38bb Mon Sep 17 00:00:00 2001 From: Tim te Beek Date: Wed, 16 Aug 2023 19:53:38 +0200 Subject: [PATCH] Do not partially use text blocks (#273) * Do not partially use text blocks Fixes #260 * Restore original order * Restore original order * Clear out duplication in recursive function * Move function down; IDE keeps moving this --- .../java/migrate/lang/UseTextBlocks.java | 19 ++++++- .../java/migrate/lang/UseTextBlocksTest.java | 57 ++++++++++++------- 2 files changed, 52 insertions(+), 24 deletions(-) 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 a678436b88..ef76d3fb50 100644 --- a/src/main/java/org/openrewrite/java/migrate/lang/UseTextBlocks.java +++ b/src/main/java/org/openrewrite/java/migrate/lang/UseTextBlocks.java @@ -33,7 +33,10 @@ import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.time.Duration; -import java.util.*; +import java.util.ArrayList; +import java.util.Base64; +import java.util.List; +import java.util.Optional; import java.util.stream.Collectors; import static org.openrewrite.Tree.randomId; @@ -82,6 +85,11 @@ public J visitBinary(J.Binary binary, ExecutionContext ctx) { StringBuilder contentSb = new StringBuilder(); StringBuilder concatenationSb = new StringBuilder(); + boolean allLiterals = allLiterals(binary); + if (!allLiterals) { + return binary; // Not super.visitBinary(binary, ctx) because we don't want to visit the children + } + boolean flattenable = flatAdditiveStringLiterals(binary, stringLiterals, contentSb, concatenationSb); if (!flattenable) { return super.visitBinary(binary, ctx); @@ -133,7 +141,7 @@ private J.Literal toTextBlock(J.Binary binary, String content, List s boolean useTab = tabsAndIndentsStyle.getUseTabCharacter(); int tabSize = tabsAndIndentsStyle.getTabSize(); - String indentation = getIndents(concatenation.toString(), useTab, tabSize); + String indentation = getIndents(concatenation, useTab, tabSize); boolean isEndsWithNewLine = content.endsWith("\n"); @@ -167,6 +175,12 @@ private J.Literal toTextBlock(J.Binary binary, String content, List s }); } + private static boolean allLiterals(Expression exp) { + return isRegularStringLiteral(exp) || exp instanceof J.Binary + && ((J.Binary) exp).getOperator() == J.Binary.Type.Addition + && allLiterals(((J.Binary) exp).getLeft()) && allLiterals(((J.Binary) exp).getRight()); + } + private static boolean flatAdditiveStringLiterals(Expression expression, List stringLiterals, StringBuilder contentSb, @@ -194,7 +208,6 @@ private static boolean flatAdditiveStringLiterals(Expression expression, private static boolean isRegularStringLiteral(Expression expr) { if (expr instanceof J.Literal) { J.Literal l = (J.Literal) expr; - return TypeUtils.isString(l.getType()) && l.getValueSource() != null && !l.getValueSource().startsWith("\"\"\""); 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 e85be3144d..fb756c529a 100644 --- a/src/test/java/org/openrewrite/java/migrate/lang/UseTextBlocksTest.java +++ b/src/test/java/org/openrewrite/java/migrate/lang/UseTextBlocksTest.java @@ -24,6 +24,7 @@ import org.openrewrite.style.NamedStyles; import org.openrewrite.test.RecipeSpec; import org.openrewrite.test.RewriteTest; + import java.util.function.Consumer; import java.util.function.UnaryOperator; @@ -31,7 +32,8 @@ import static java.util.Collections.singletonList; import static org.assertj.core.api.Assertions.assertThat; import static org.openrewrite.Tree.randomId; -import static org.openrewrite.java.Assertions.*; +import static org.openrewrite.java.Assertions.java; +import static org.openrewrite.java.Assertions.javaVersion; class UseTextBlocksTest implements RewriteTest { @@ -95,6 +97,7 @@ class Test { @Test void preserveTrailingWhiteSpaces2() { rewriteRun( + //language=java java( """ class Test { @@ -372,11 +375,11 @@ class Test { private static Consumer tabsAndIndents(UnaryOperator with, int tabSize) { return spec -> spec.parser(JavaParser.fromJavaVersion().styles(singletonList( - new NamedStyles( - randomId(), "TabsOnlyFile", "TabsOnlyFile", "tabSize is x", emptySet(), - singletonList(with.apply(buildTabsAndIndents(tabSize))) - ) - ))); + new NamedStyles( + randomId(), "TabsOnlyFile", "TabsOnlyFile", "tabSize is x", emptySet(), + singletonList(with.apply(buildTabsAndIndents(tabSize))) + ) + ))); } private static TabsAndIndentsStyle buildTabsAndIndents(int tabSize) { @@ -500,6 +503,7 @@ void textBlockDemo() { @Test void newlineAtBeginningOfLines() { rewriteRun( + //language=java java( """ class A { @@ -541,6 +545,7 @@ void log(String s) {} @Test void consecutiveNewLines() { rewriteRun( + //language=java java( """ class Test { @@ -693,6 +698,7 @@ class Test { @Test void eightQuotes() { rewriteRun( + //language=java java( """ class Test { @@ -758,10 +764,12 @@ class Test { ); } - @Disabled @Test - void grouping() { + @Issue("https://github.com/openrewrite/rewrite-migrate-java/issues/260") + //@Issue("https://github.com/openrewrite/rewrite-migrate-java/issues/261") + void doNotPartiallyReplaceWithTextBlocks() { rewriteRun( + //language=java java( """ public class Test { @@ -779,23 +787,30 @@ public void method() { "AFTER the variable. "; } } - """, + """ + ) + ); + } + + @Test + @Issue("https://github.com/openrewrite/rewrite-migrate-java/issues/261") + void doNotPartiallyReplaceWithTextBlocksWithIntLiteral() { + rewriteRun( + //language=java + java( """ public class Test { public void method() { - int variable = 1; String stringWithVariableInIt = - \""" - This \\ - is \\ - text \\ - BEFORE the variable \\ - \""" - This \\ - is \\ - text \\ - AFTER the variable. \\ - \"""; + "This " + + "is " + + "text " + + "BEFORE the variable " + + 1 + + "This " + + "is " + + "text " + + "AFTER the variable. "; } } """