Skip to content
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package org.openrewrite.java;

import org.junit.jupiter.api.Test;
import org.junitpioneer.jupiter.ExpectedToFail;
import org.openrewrite.DocumentExample;
import org.openrewrite.ExecutionContext;
import org.openrewrite.Issue;
Expand Down Expand Up @@ -336,6 +337,68 @@ public class Test {
);
}

@ExpectedToFail("JavaTemplate has no escape for a literal #{ in template source")
@Issue("https://github.com/openrewrite/rewrite-static-analysis/pull/976")
@Test
void hashBraceInStringLiteral() {
rewriteRun(
spec -> spec.recipe(toRecipe(() -> new JavaVisitor<>() {
@Override
public J visitLiteral(J.Literal literal, ExecutionContext ctx) {
if ("placeholder".equals(literal.getValue())) {
return JavaTemplate.builder("\"#{foo}\"")
.build()
.apply(getCursor(), literal.getCoordinates().replace());
}
return super.visitLiteral(literal, ctx);
}
})),
java(
"""
public class Test {
String s = "placeholder";
}
""",
"""
public class Test {
String s = "#{foo}";
}
"""
)
);
}

@ExpectedToFail("Substitutions loops replacePlaceholders to a fixed point, so the #{ unescaped in pass 1 is re-parsed as a placeholder in pass 2")
@Issue("https://github.com/openrewrite/rewrite-static-analysis/pull/976")
@Test
void escapedHashBraceInStringLiteral() {
rewriteRun(
spec -> spec.recipe(toRecipe(() -> new JavaVisitor<>() {
@Override
public J visitLiteral(J.Literal literal, ExecutionContext ctx) {
if ("placeholder".equals(literal.getValue())) {
return JavaTemplate.builder("\"\\#{foo}\"")
.build()
.apply(getCursor(), literal.getCoordinates().replace());
}
return super.visitLiteral(literal, ctx);
}
})),
java(
"""
public class Test {
String s = "placeholder";
}
""",
"""
public class Test {
String s = "#{foo}";
}
"""
)
);
}

@Issue("https://github.com/openrewrite/rewrite/issues/1985")
@Test
void newArray() {
Expand Down