From bf0752e59eba584c33f17cbba1591ee5fa5d6d4e Mon Sep 17 00:00:00 2001 From: Sam Snyder Date: Wed, 4 Sep 2024 12:12:40 -0700 Subject: [PATCH] Fix UseTextBlocks working only on sources that are using exactly Java 17. We noticed this wasn't making any changes to Java 21 sources. Multi-line strings were added in 15 and work in all subsequent versions. --- .../java/migrate/lang/UseTextBlocks.java | 20 ++--- .../java/migrate/lang/UseTextBlocksTest.java | 27 +++++++ ...ompilerPluginReleaseConfigurationTest.java | 80 +++++++++---------- 3 files changed, 75 insertions(+), 52 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 75311d3ac9..b00de989a4 100644 --- a/src/main/java/org/openrewrite/java/migrate/lang/UseTextBlocks.java +++ b/src/main/java/org/openrewrite/java/migrate/lang/UseTextBlocks.java @@ -34,12 +34,10 @@ import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.time.Duration; -import java.util.ArrayList; -import java.util.Base64; -import java.util.List; -import java.util.Optional; +import java.util.*; import java.util.stream.Collectors; +import static java.util.Objects.requireNonNull; import static org.openrewrite.Tree.randomId; @Value @@ -50,7 +48,6 @@ public class UseTextBlocks extends Recipe { "The default value is true.", example = "true", required = false) - @Nullable boolean convertStringsWithoutNewlines; public UseTextBlocks() { @@ -80,7 +77,7 @@ public String getDescription() { public TreeVisitor getVisitor() { TreeVisitor preconditions = Preconditions.and( Preconditions.not(new KotlinFileChecker<>()), - new HasJavaVersion("17", true).getVisitor() + new HasJavaVersion("[15,)", null).getVisitor() ); return Preconditions.check(preconditions, new JavaVisitor() { @Override @@ -125,13 +122,13 @@ 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().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 = stringLiterals.get(i).getValue().toString(); + String s = requireNonNull(stringLiterals.get(i).getValue()).toString(); sb.append(s); originalContent.append(s); if (i != stringLiterals.size() - 1) { - String nextLine = stringLiterals.get(i + 1).getValue().toString(); + String nextLine = requireNonNull(stringLiterals.get(i + 1).getValue()).toString(); char nextChar = nextLine.charAt(0); if (!s.endsWith("\n") && nextChar != '\n') { sb.append(passPhrase); @@ -202,7 +199,7 @@ private static boolean flatAdditiveStringLiterals(Expression expression, } else if (isRegularStringLiteral(expression)) { J.Literal l = (J.Literal) expression; stringLiterals.add(l); - contentSb.append(l.getValue().toString()); + contentSb.append(requireNonNull(l.getValue())); concatenationSb.append(l.getPrefix().getWhitespace()).append("-"); return true; } @@ -296,13 +293,12 @@ private static int[] shortestPrefixAfterNewline(String concatenation, int tabSiz private static String generatePassword(String originalStr) throws NoSuchAlgorithmException { final String SALT = "kun"; - String password = ""; String saltedStr = originalStr + SALT; MessageDigest md = MessageDigest.getInstance("SHA-256"); byte[] hashBytes = md.digest(saltedStr.getBytes()); - password = Base64.getEncoder().encodeToString(hashBytes); + String password = Base64.getEncoder().encodeToString(hashBytes); while (originalStr.contains(password)) { hashBytes = md.digest(password.getBytes()); 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 6df193c50e..8cc9d71d21 100644 --- a/src/test/java/org/openrewrite/java/migrate/lang/UseTextBlocksTest.java +++ b/src/test/java/org/openrewrite/java/migrate/lang/UseTextBlocksTest.java @@ -36,6 +36,7 @@ import static org.openrewrite.java.Assertions.javaVersion; import static org.openrewrite.kotlin.Assertions.kotlin; +@SuppressWarnings({"ConcatenationWithEmptyString", "TextBlockMigration", "EscapedSpace"}) class UseTextBlocksTest implements RewriteTest { @Override @@ -120,6 +121,32 @@ class Test { ); } + @Test + void worksOnNewerJavaVersions() { + rewriteRun( + //language=java + java( + """ + class Test { + String color = "red\\n" + + "green\\n" + + "blue\\n"; + } + """, + """ + class Test { + String color = \""" + red + green + blue + ""\"; + } + """, + spec -> spec.markers(javaVersion(21)) + ) + ); + } + @Test void indentsAlignment() { rewriteRun( diff --git a/src/test/java/org/openrewrite/java/migrate/maven/UseMavenCompilerPluginReleaseConfigurationTest.java b/src/test/java/org/openrewrite/java/migrate/maven/UseMavenCompilerPluginReleaseConfigurationTest.java index 2e33f6299e..659c7f4c74 100644 --- a/src/test/java/org/openrewrite/java/migrate/maven/UseMavenCompilerPluginReleaseConfigurationTest.java +++ b/src/test/java/org/openrewrite/java/migrate/maven/UseMavenCompilerPluginReleaseConfigurationTest.java @@ -43,7 +43,7 @@ void replacesSourceAndTargetConfig() { org.sample sample 1.0.0 - + @@ -57,7 +57,7 @@ void replacesSourceAndTargetConfig() { - + """, """ @@ -67,7 +67,7 @@ void replacesSourceAndTargetConfig() { org.sample sample 1.0.0 - + @@ -80,7 +80,7 @@ void replacesSourceAndTargetConfig() { - + """ ) @@ -100,7 +100,7 @@ void replaceSourceAndTargetConfigIfDefault() { org.sample sample 1.0.0 - + @@ -115,7 +115,7 @@ void replaceSourceAndTargetConfigIfDefault() { - + """, """ @@ -125,7 +125,7 @@ void replaceSourceAndTargetConfigIfDefault() { org.sample sample 1.0.0 - + @@ -139,7 +139,7 @@ void replaceSourceAndTargetConfigIfDefault() { - + """ ) @@ -158,11 +158,11 @@ void reusesJavaVersionVariableIfAvailable() { org.sample sample 1.0.0 - + 11 - + @@ -175,7 +175,7 @@ void reusesJavaVersionVariableIfAvailable() { - + """, """ @@ -185,11 +185,11 @@ void reusesJavaVersionVariableIfAvailable() { org.sample sample 1.0.0 - + 11 - + @@ -201,7 +201,7 @@ void reusesJavaVersionVariableIfAvailable() { - + """ ) @@ -220,7 +220,7 @@ void upgradesExistingReleaseConfig() { org.sample sample 1.0.0 - + @@ -233,7 +233,7 @@ void upgradesExistingReleaseConfig() { - + """, """ @@ -243,7 +243,7 @@ void upgradesExistingReleaseConfig() { org.sample sample 1.0.0 - + @@ -256,7 +256,7 @@ void upgradesExistingReleaseConfig() { - + """ ) @@ -275,11 +275,11 @@ void prefersJavaVersionIfAvailable() { org.sample sample 1.0.0 - + 11 - + @@ -292,7 +292,7 @@ void prefersJavaVersionIfAvailable() { - + """, """ @@ -302,11 +302,11 @@ void prefersJavaVersionIfAvailable() { org.sample sample 1.0.0 - + 11 - + @@ -319,7 +319,7 @@ void prefersJavaVersionIfAvailable() { - + """ ) @@ -338,11 +338,11 @@ void notMisledByUnrelatedProperty() { org.sample sample 1.0.0 - + 11 - + @@ -356,7 +356,7 @@ void notMisledByUnrelatedProperty() { - + """, """ @@ -366,11 +366,11 @@ void notMisledByUnrelatedProperty() { org.sample sample 1.0.0 - + 11 - + @@ -384,7 +384,7 @@ void notMisledByUnrelatedProperty() { - + """ ) @@ -404,7 +404,7 @@ void noVersionDowngrade() { org.sample sample 1.0.0 - + @@ -417,7 +417,7 @@ void noVersionDowngrade() { - + """) ); @@ -435,11 +435,11 @@ void reusesJavaVersionVariableIfDefinedInParentPom() { org.sample parent 1.0.0 - + 11 - + pom """), @@ -450,16 +450,16 @@ void reusesJavaVersionVariableIfDefinedInParentPom() { 4.0.0 - + org.sample parent 1.0.0 - + sample 1.0.0 - + @@ -478,16 +478,16 @@ void reusesJavaVersionVariableIfDefinedInParentPom() { 4.0.0 - + org.sample parent 1.0.0 - + sample 1.0.0 - +