Skip to content

Commit

Permalink
Fix UseTextBlocks working only on sources that are using exactly Java…
Browse files Browse the repository at this point in the history
… 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.
  • Loading branch information
sambsnyd committed Sep 4, 2024
1 parent 512268f commit bf0752e
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 52 deletions.
20 changes: 8 additions & 12 deletions src/main/java/org/openrewrite/java/migrate/lang/UseTextBlocks.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -50,7 +48,6 @@ public class UseTextBlocks extends Recipe {
"The default value is true.",
example = "true",
required = false)
@Nullable
boolean convertStringsWithoutNewlines;

public UseTextBlocks() {
Expand Down Expand Up @@ -80,7 +77,7 @@ public String getDescription() {
public TreeVisitor<?, ExecutionContext> getVisitor() {
TreeVisitor<?, ExecutionContext> preconditions = Preconditions.and(
Preconditions.not(new KotlinFileChecker<>()),
new HasJavaVersion("17", true).getVisitor()
new HasJavaVersion("[15,)", null).getVisitor()
);
return Preconditions.check(preconditions, new JavaVisitor<ExecutionContext>() {
@Override
Expand Down Expand Up @@ -125,13 +122,13 @@ private J.Literal toTextBlock(J.Binary binary, String content, List<J.Literal> 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);
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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(
Expand Down
Loading

0 comments on commit bf0752e

Please sign in to comment.