Skip to content

Commit bf0752e

Browse files
committed
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.
1 parent 512268f commit bf0752e

File tree

3 files changed

+75
-52
lines changed

3 files changed

+75
-52
lines changed

src/main/java/org/openrewrite/java/migrate/lang/UseTextBlocks.java

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,10 @@
3434
import java.security.MessageDigest;
3535
import java.security.NoSuchAlgorithmException;
3636
import java.time.Duration;
37-
import java.util.ArrayList;
38-
import java.util.Base64;
39-
import java.util.List;
40-
import java.util.Optional;
37+
import java.util.*;
4138
import java.util.stream.Collectors;
4239

40+
import static java.util.Objects.requireNonNull;
4341
import static org.openrewrite.Tree.randomId;
4442

4543
@Value
@@ -50,7 +48,6 @@ public class UseTextBlocks extends Recipe {
5048
"The default value is true.",
5149
example = "true",
5250
required = false)
53-
@Nullable
5451
boolean convertStringsWithoutNewlines;
5552

5653
public UseTextBlocks() {
@@ -80,7 +77,7 @@ public String getDescription() {
8077
public TreeVisitor<?, ExecutionContext> getVisitor() {
8178
TreeVisitor<?, ExecutionContext> preconditions = Preconditions.and(
8279
Preconditions.not(new KotlinFileChecker<>()),
83-
new HasJavaVersion("17", true).getVisitor()
80+
new HasJavaVersion("[15,)", null).getVisitor()
8481
);
8582
return Preconditions.check(preconditions, new JavaVisitor<ExecutionContext>() {
8683
@Override
@@ -125,13 +122,13 @@ private J.Literal toTextBlock(J.Binary binary, String content, List<J.Literal> s
125122

126123
StringBuilder sb = new StringBuilder();
127124
StringBuilder originalContent = new StringBuilder();
128-
stringLiterals = stringLiterals.stream().filter(s -> !s.getValue().toString().isEmpty()).collect(Collectors.toList());
125+
stringLiterals = stringLiterals.stream().filter(s -> s.getValue() != null && !s.getValue().toString().isEmpty()).collect(Collectors.toList());
129126
for (int i = 0; i < stringLiterals.size(); i++) {
130-
String s = stringLiterals.get(i).getValue().toString();
127+
String s = requireNonNull(stringLiterals.get(i).getValue()).toString();
131128
sb.append(s);
132129
originalContent.append(s);
133130
if (i != stringLiterals.size() - 1) {
134-
String nextLine = stringLiterals.get(i + 1).getValue().toString();
131+
String nextLine = requireNonNull(stringLiterals.get(i + 1).getValue()).toString();
135132
char nextChar = nextLine.charAt(0);
136133
if (!s.endsWith("\n") && nextChar != '\n') {
137134
sb.append(passPhrase);
@@ -202,7 +199,7 @@ private static boolean flatAdditiveStringLiterals(Expression expression,
202199
} else if (isRegularStringLiteral(expression)) {
203200
J.Literal l = (J.Literal) expression;
204201
stringLiterals.add(l);
205-
contentSb.append(l.getValue().toString());
202+
contentSb.append(requireNonNull(l.getValue()));
206203
concatenationSb.append(l.getPrefix().getWhitespace()).append("-");
207204
return true;
208205
}
@@ -296,13 +293,12 @@ private static int[] shortestPrefixAfterNewline(String concatenation, int tabSiz
296293

297294
private static String generatePassword(String originalStr) throws NoSuchAlgorithmException {
298295
final String SALT = "kun";
299-
String password = "";
300296
String saltedStr = originalStr + SALT;
301297

302298
MessageDigest md = MessageDigest.getInstance("SHA-256");
303299
byte[] hashBytes = md.digest(saltedStr.getBytes());
304300

305-
password = Base64.getEncoder().encodeToString(hashBytes);
301+
String password = Base64.getEncoder().encodeToString(hashBytes);
306302

307303
while (originalStr.contains(password)) {
308304
hashBytes = md.digest(password.getBytes());

src/test/java/org/openrewrite/java/migrate/lang/UseTextBlocksTest.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import static org.openrewrite.java.Assertions.javaVersion;
3737
import static org.openrewrite.kotlin.Assertions.kotlin;
3838

39+
@SuppressWarnings({"ConcatenationWithEmptyString", "TextBlockMigration", "EscapedSpace"})
3940
class UseTextBlocksTest implements RewriteTest {
4041

4142
@Override
@@ -120,6 +121,32 @@ class Test {
120121
);
121122
}
122123

124+
@Test
125+
void worksOnNewerJavaVersions() {
126+
rewriteRun(
127+
//language=java
128+
java(
129+
"""
130+
class Test {
131+
String color = "red\\n" +
132+
"green\\n" +
133+
"blue\\n";
134+
}
135+
""",
136+
"""
137+
class Test {
138+
String color = \"""
139+
red
140+
green
141+
blue
142+
""\";
143+
}
144+
""",
145+
spec -> spec.markers(javaVersion(21))
146+
)
147+
);
148+
}
149+
123150
@Test
124151
void indentsAlignment() {
125152
rewriteRun(

0 commit comments

Comments
 (0)