Skip to content

Uniform behaviour for the methodInvocations that are needed for template unsubstitution #5658

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
package org.openrewrite.java;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
import org.openrewrite.Cursor;
import org.openrewrite.DocumentExample;
import org.openrewrite.ExecutionContext;
Expand Down Expand Up @@ -1524,4 +1526,50 @@ abstract static class Two<TwoT extends Two<TwoT, OneT>, OneT extends One<TwoT, O
)
);
}

@ParameterizedTest(name = "Keep template prefix for block argument when template is \"{0}\"")
@CsvSource({"#{}", "#{any()}"})
void keepTemplatePrefixForBlock(String argument) {
rewriteRun(
spec -> spec.recipe(toRecipe(() -> new JavaVisitor<>() {
@Override
public J visitIf(J.If if_, ExecutionContext ctx) {
String template = """
switch (#{any()}) {
case null -> %s;
}
""".formatted(argument);
return JavaTemplate.apply(template, getCursor(), if_.getCoordinates().replace(), ((J.Binary) if_.getIfCondition().getTree()).getLeft(), if_.getThenPart());
}
}).withMaxCycles(1)),
java(
"""
class Test {
static String formatter(Object obj) {
String formatted = "initialValue";
if (obj == null) {
String str = "String";
formatted = String.format("%s %s", "null", str);
}
return formatted;
}
}
""",
"""
class Test {
static String formatter(Object obj) {
String formatted = "initialValue";
switch (obj) {
case null -> {
String str = "String";
formatted = String.format("%s %s", "null", str);
}
}
return formatted;
}
}
"""
)
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ public J visitBlock(J.Block block, Integer integer) {

@Override
public J visitMethodInvocation(J.MethodInvocation method, Integer integer) {
J param = maybeParameter(method.getName());
J param = maybeParameter(method.getName(), method);
if (param instanceof Expression) {
return maybeParenthesize((Expression) param, getCursor());
} else if (param != null) {
Expand Down Expand Up @@ -323,10 +323,14 @@ public J visitLiteral(J.Literal literal, Integer integer) {
}

private @Nullable J maybeParameter(J j1) {
return maybeParameter(j1, j1);
}

private @Nullable J maybeParameter(J j1, J prefixedElem) {
Integer param = parameterIndex(j1.getPrefix());
if (param != null) {
J j2 = (J) parameters[param];
return j2.withPrefix(j2.getPrefix().withWhitespace(j1.getPrefix().getWhitespace()));
return j2.withPrefix(j2.getPrefix().withWhitespace(prefixedElem.getPrefix().getWhitespace()));
}
return null;
}
Expand Down