Skip to content

Commit 93642f6

Browse files
authored
Fix JavaTemplate replacements passing a J.NewArray into a vararg J.MethodInvocation (#6572)
* JavaTemplate vararg replacement should not contain excess braces * Expand `J.NewArray` passed directly into `J.MethodInvocation` * Adopt `ListUtils` for expansion
1 parent 1ac5697 commit 93642f6

2 files changed

Lines changed: 63 additions & 1 deletion

File tree

rewrite-java-test/src/test/java/org/openrewrite/java/JavaTemplateSubstitutionsTest.java

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -551,4 +551,51 @@ public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx)
551551
return m;
552552
}
553553
}
554+
555+
@Test
556+
void changeVarargsToList() {
557+
rewriteRun(
558+
// Mimics what a Refaster template would generate for varargs
559+
spec -> spec.recipe(toRecipe(() -> new JavaVisitor<>() {
560+
@Override
561+
public J visitMethodInvocation(J.MethodInvocation elem, ExecutionContext ctx) {
562+
JavaTemplate.Matcher matcher;
563+
JavaTemplate before = JavaTemplate.builder("java.util.stream.Stream.of(#{value:anyArray(T)}).toList()")
564+
.bindType("java.util.List<T>")
565+
.genericTypes("T").build();
566+
if ((matcher = before.matcher(getCursor())).find()) {
567+
maybeRemoveImport("java.util.stream.Stream");
568+
return JavaTemplate.builder("java.util.Arrays.asList(#{value:anyArray(T)})")
569+
.bindType("java.util.List<T>")
570+
.genericTypes("T")
571+
.build()
572+
.apply(getCursor(), elem.getCoordinates().replace(), matcher.parameter(0));
573+
}
574+
return super.visitMethodInvocation(elem, ctx);
575+
}
576+
})),
577+
//language=java
578+
java(
579+
"""
580+
import java.util.List;
581+
import java.util.stream.Stream;
582+
583+
class Example {
584+
List<String> test() {
585+
return Stream.of("a", "b", "c").toList();
586+
}
587+
}
588+
""",
589+
"""
590+
import java.util.List;
591+
592+
class Example {
593+
List<String> test() {
594+
return java.util.Arrays.asList("a", "b", "c");
595+
}
596+
}
597+
"""
598+
)
599+
);
600+
}
554601
}

rewrite-java/src/main/java/org/openrewrite/java/internal/template/Substitutions.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,22 @@ public J visitMethodInvocation(J.MethodInvocation method, Integer integer) {
298298
} else if (param != null) {
299299
return param;
300300
}
301-
return super.visitMethodInvocation(method, integer);
301+
302+
J.MethodInvocation m = (J.MethodInvocation) super.visitMethodInvocation(method, integer);
303+
return maybeExpandVarargsNewArray(m);
304+
}
305+
306+
private J.MethodInvocation maybeExpandVarargsNewArray(J.MethodInvocation method) {
307+
return method.withArguments(ListUtils.flatMap(method.getArguments(), arg -> {
308+
if (arg instanceof J.NewArray) {
309+
J.NewArray newArray = (J.NewArray) arg;
310+
// Varargs-captured arrays have no type expression and no dimensions
311+
if (newArray.getTypeExpression() == null && newArray.getDimensions().isEmpty()) {
312+
return ListUtils.mapFirst(newArray.getInitializer(), e -> e.withPrefix(newArray.getPrefix()));
313+
}
314+
}
315+
return arg;
316+
}));
302317
}
303318

304319
@Override

0 commit comments

Comments
 (0)