diff --git a/src/main/java/org/openrewrite/staticanalysis/RemoveToStringCallsFromArrayInstances.java b/src/main/java/org/openrewrite/staticanalysis/RemoveToStringCallsFromArrayInstances.java index 30537759b5..792b8a840b 100644 --- a/src/main/java/org/openrewrite/staticanalysis/RemoveToStringCallsFromArrayInstances.java +++ b/src/main/java/org/openrewrite/staticanalysis/RemoveToStringCallsFromArrayInstances.java @@ -19,7 +19,10 @@ import org.openrewrite.java.JavaTemplate; import org.openrewrite.java.JavaVisitor; import org.openrewrite.java.MethodMatcher; -import org.openrewrite.java.tree.*; +import org.openrewrite.java.tree.Expression; +import org.openrewrite.java.tree.J; +import org.openrewrite.java.tree.JavaType; +import org.openrewrite.java.tree.TypedTree; import java.util.Arrays; import java.util.Collections; @@ -75,9 +78,17 @@ public J visitMethodInvocation(J.MethodInvocation mi, ExecutionContext ctx) { return buildReplacement(select, mi); } else if (METHOD_MATCHERS.stream().anyMatch(matcher -> matcher.matches(mi))) { // deals with edge cases where .toString() is called implicitly + JavaType.Method methodType = mi.getMethodType(); + if (methodType == null) { + return mi; + } + List parameterTypes = methodType.getParameterTypes(); List arguments = mi.getArguments(); - for (Expression arg : arguments) { - if (arg.getType() instanceof JavaType.Array) { + for (int i = 0; i < arguments.size(); i++) { + Expression arg = arguments.get(i); + if (arg.getType() instanceof JavaType.Array && + (i > parameterTypes.size() - 1 || + !(parameterTypes.get(i) instanceof JavaType.Array))) { getCursor().putMessage("METHOD_KEY", mi); break; } diff --git a/src/test/java/org/openrewrite/staticanalysis/RemoveToStringCallsFromArrayInstancesTest.java b/src/test/java/org/openrewrite/staticanalysis/RemoveToStringCallsFromArrayInstancesTest.java index 9eeb732f5a..08846e6185 100644 --- a/src/test/java/org/openrewrite/staticanalysis/RemoveToStringCallsFromArrayInstancesTest.java +++ b/src/test/java/org/openrewrite/staticanalysis/RemoveToStringCallsFromArrayInstancesTest.java @@ -27,8 +27,7 @@ public class RemoveToStringCallsFromArrayInstancesTest implements RewriteTest { @Override public void defaults(RecipeSpec spec) { - spec - .recipe(new RemoveToStringCallsFromArrayInstances()); + spec.recipe(new RemoveToStringCallsFromArrayInstances()); } @Test @@ -227,16 +226,6 @@ public static void main(String[] args) { System.out.println(String.format("s=%s", s)); } } - """, - """ - import java.util.Arrays; - - class SomeClass { - public static void main(String[] args) { - int[] s = new int[]{1, 2, 3}; - System.out.println(String.format("s=%s", Arrays.toString(s))); - } - } """ ) ); @@ -429,7 +418,7 @@ public static void main(String[] args) { } @Test - void worksWithPrintStreamFormat() { + void doesNotRunOnPrintStreamFormat() { //language=java rewriteRun( java( @@ -445,20 +434,6 @@ public static void main(String[] args) { ps.flush(); } } - """, - """ - import java.io.PrintStream; - import java.util.Arrays; - - class SomeClass { - public static void main(String[] args) { - PrintStream ps = new PrintStream(System.out); - String[] arr = new String[]{"test", "array"}; - - ps.format("formatting array: %s", Arrays.toString(arr)); - ps.flush(); - } - } """ ) ); @@ -500,4 +475,44 @@ public static void main(String[] args) { ); } + @Test + void varargs() { + //language=java + rewriteRun( + java( + """ + class SomeClass { + String foo(Object[] strings) { + return String.format("%s %s", strings); + } + } + """ + ) + ); + } + + @Test + void varargsButTwoArrays() { + //language=java + rewriteRun( + java( + """ + class SomeClass { + String foo(Object[] array1, Object[] array2) { + return String.format("%s %s", array1, array2); + } + } + """, + """ + import java.util.Arrays; + + class SomeClass { + String foo(Object[] array1, Object[] array2) { + return String.format("%s %s", Arrays.toString(array1), Arrays.toString(array2)); + } + } + """ + ) + ); + } }