Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ dependencies {

testImplementation("org.openrewrite:rewrite-test")
testImplementation("org.openrewrite:rewrite-java-tck")
testImplementation("org.openrewrite:rewrite-kotlin:$rewriteVersion")
testImplementation("org.openrewrite.gradle.tooling:model:$rewriteVersion")

testImplementation("org.assertj:assertj-core:latest.release")
Expand Down
21 changes: 13 additions & 8 deletions src/main/java/org/openrewrite/java/migrate/lang/UseTextBlocks.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import org.openrewrite.java.tree.JavaType;
import org.openrewrite.java.tree.TypeUtils;
import org.openrewrite.marker.Markers;
import org.openrewrite.staticanalysis.kotlin.KotlinFileChecker;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
Expand All @@ -46,7 +47,7 @@
public class UseTextBlocks extends Recipe {
@Option(displayName = "Whether to convert strings without newlines (the default value is true).",
description = "Whether or not strings without newlines should be converted to text block when processing code. " +
"The default value is true.",
"The default value is true.",
example = "true",
required = false)
@Nullable
Expand Down Expand Up @@ -77,7 +78,11 @@ public String getDescription() {

@Override
public TreeVisitor<?, ExecutionContext> getVisitor() {
return Preconditions.check(new HasJavaVersion("17", true), new JavaVisitor<ExecutionContext>() {
TreeVisitor<?, ExecutionContext> preconditions = Preconditions.and(
Preconditions.not(new KotlinFileChecker<>()),
new HasJavaVersion("17", true).getVisitor()
);
return Preconditions.check(preconditions, new JavaVisitor<ExecutionContext>() {
@Override
public J visitBinary(J.Binary binary, ExecutionContext ctx) {
List<J.Literal> stringLiterals = new ArrayList<>();
Expand Down Expand Up @@ -177,8 +182,8 @@ private J.Literal toTextBlock(J.Binary binary, String content, List<J.Literal> 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());
&& ((J.Binary) exp).getOperator() == J.Binary.Type.Addition
&& allLiterals(((J.Binary) exp).getLeft()) && allLiterals(((J.Binary) exp).getRight());
}

private static boolean flatAdditiveStringLiterals(Expression expression,
Expand All @@ -193,7 +198,7 @@ private static boolean flatAdditiveStringLiterals(Expression expression,
concatenationSb.append(b.getPrefix().getWhitespace()).append("-");
concatenationSb.append(b.getPadding().getOperator().getBefore().getWhitespace()).append("-");
return flatAdditiveStringLiterals(b.getLeft(), stringLiterals, contentSb, concatenationSb)
&& flatAdditiveStringLiterals(b.getRight(), stringLiterals, contentSb, concatenationSb);
&& flatAdditiveStringLiterals(b.getRight(), stringLiterals, contentSb, concatenationSb);
} else if (isRegularStringLiteral(expression)) {
J.Literal l = (J.Literal) expression;
stringLiterals.add(l);
Expand All @@ -209,8 +214,8 @@ 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("\"\"\"");
l.getValueSource() != null &&
!l.getValueSource().startsWith("\"\"\"");
}
return false;
}
Expand All @@ -232,7 +237,7 @@ private static String getIndents(String concatenation, boolean useTabCharacter,
int spaceCount = tabAndSpaceCounts[1];
if (useTabCharacter) {
return StringUtils.repeat("\t", tabCount) +
StringUtils.repeat(" ", spaceCount);
StringUtils.repeat(" ", spaceCount);
} else {
// replace tab with spaces if the style is using spaces
return StringUtils.repeat(" ", tabCount * tabSize + spaceCount);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import static org.openrewrite.Tree.randomId;
import static org.openrewrite.java.Assertions.java;
import static org.openrewrite.java.Assertions.javaVersion;
import static org.openrewrite.kotlin.Assertions.kotlin;

class UseTextBlocksTest implements RewriteTest {

Expand Down Expand Up @@ -817,4 +818,19 @@ public void method() {
)
);
}

@Test
@Issue("https://github.com/openrewrite/rewrite-migrate-java/issues/501")
void shouldNotUpdateKotlinCode() {
rewriteRun(
spec -> spec.expectedCyclesThatMakeChanges(0),
kotlin(
"""
const val MULTI_LINE_MESSAGE =
\s"This is a multi-line message and should not be updated. " +
\s"This is the second sentence of such message."
"""
)
);
}
}