diff --git a/rewrite-gradle/src/test/java/org/openrewrite/gradle/search/FindPluginsTest.java b/rewrite-gradle/src/test/java/org/openrewrite/gradle/search/FindPluginsTest.java index ede21f7973d..78cd8c053fc 100644 --- a/rewrite-gradle/src/test/java/org/openrewrite/gradle/search/FindPluginsTest.java +++ b/rewrite-gradle/src/test/java/org/openrewrite/gradle/search/FindPluginsTest.java @@ -69,7 +69,8 @@ void findPlugin() { void settingsResolutionStrategy() { rewriteRun( spec -> spec.beforeRecipe(withToolingApi()), - settingsGradle(""" + settingsGradle( + """ pluginManagement { repositories { mavenLocal() @@ -83,7 +84,8 @@ void settingsResolutionStrategy() { } } } - """), + """ + ), buildGradle( """ plugins { diff --git a/rewrite-java-test/src/test/java/org/openrewrite/java/DeleteMethodArgumentTest.java b/rewrite-java-test/src/test/java/org/openrewrite/java/DeleteMethodArgumentTest.java index 6949a868520..5ad21d41e15 100644 --- a/rewrite-java-test/src/test/java/org/openrewrite/java/DeleteMethodArgumentTest.java +++ b/rewrite-java-test/src/test/java/org/openrewrite/java/DeleteMethodArgumentTest.java @@ -17,11 +17,14 @@ import org.intellij.lang.annotations.Language; import org.junit.jupiter.api.Test; +import org.openrewrite.Issue; +import org.openrewrite.test.RecipeSpec; import org.openrewrite.test.RewriteTest; import static org.openrewrite.java.Assertions.java; class DeleteMethodArgumentTest implements RewriteTest { + @Language("java") String b = """ class B { @@ -34,11 +37,15 @@ public B(int n) {} } """; + @Override + public void defaults(RecipeSpec spec) { + spec.parser(JavaParser.fromJavaVersion().dependsOn(b)); + } + @Test void deleteMiddleArgumentDeclarative() { rewriteRun( spec -> spec.recipes(new DeleteMethodArgument("B foo(int, int, int)", 1)), - java(b), java( "public class A {{ B.foo(0, 1, 2); }}", "public class A {{ B.foo(0, 2); }}" @@ -50,7 +57,6 @@ void deleteMiddleArgumentDeclarative() { void deleteMiddleArgument() { rewriteRun( spec -> spec.recipe(new DeleteMethodArgument("B foo(int, int, int)", 1)), - java(b), java( "public class A {{ B.foo(0, 1, 2); }}", "public class A {{ B.foo(0, 2); }}" @@ -65,8 +71,8 @@ void deleteArgumentsConsecutively() { new DeleteMethodArgument("B foo(int, int, int)", 1), new DeleteMethodArgument("B foo(int, int)", 1) ), - java(b), - java("public class A {{ B.foo(0, 1, 2); }}", + java( + "public class A {{ B.foo(0, 1, 2); }}", "public class A {{ B.foo(0); }}" ) ); @@ -76,7 +82,6 @@ void deleteArgumentsConsecutively() { void doNotDeleteEmptyContainingFormatting() { rewriteRun( spec -> spec.recipe(new DeleteMethodArgument("B foo(..)", 0)), - java(b), java("public class A {{ B.foo( ); }}") ); } @@ -85,7 +90,6 @@ void doNotDeleteEmptyContainingFormatting() { void insertEmptyWhenLastArgumentIsDeleted() { rewriteRun( spec -> spec.recipe(new DeleteMethodArgument("B foo(..)", 0)), - java(b), java( "public class A {{ B.foo(1); }}", "public class A {{ B.foo(); }}" @@ -97,11 +101,26 @@ void insertEmptyWhenLastArgumentIsDeleted() { void deleteConstructorArgument() { rewriteRun( spec -> spec.recipe(new DeleteMethodArgument("B (int)", 0)), - java(b), java( "public class A { B b = new B(0); }", "public class A { B b = new B(); }" ) ); } + + @Issue("https://github.com/openrewrite/rewrite/issues/4676") + @Test + void deleteFirstArgument() { + rewriteRun( + spec -> spec.recipe(new DeleteMethodArgument("B foo(int, int, int)", 0)), + java( + "public class A {{ B.foo(0, 1, 2); }}", + "public class A {{ B.foo(1, 2); }}" + ), + java( + "public class C {{ B.foo(\n\t\t0, 1, 2); }}", + "public class C {{ B.foo(\n\t\t1, 2); }}" + ) + ); + } } diff --git a/rewrite-java/src/main/java/org/openrewrite/java/DeleteMethodArgument.java b/rewrite-java/src/main/java/org/openrewrite/java/DeleteMethodArgument.java index 2fd26e66556..c6c4676656d 100644 --- a/rewrite-java/src/main/java/org/openrewrite/java/DeleteMethodArgument.java +++ b/rewrite-java/src/main/java/org/openrewrite/java/DeleteMethodArgument.java @@ -100,9 +100,11 @@ private MethodCall visitMethodCall(MethodCall methodCall) { .count() >= argumentIndex + 1) { List args = new ArrayList<>(originalArgs); - args.remove(argumentIndex); + Expression removed = args.remove(argumentIndex); if (args.isEmpty()) { args = singletonList(new J.Empty(randomId(), Space.EMPTY, Markers.EMPTY)); + } else if (argumentIndex == 0) { + args.set(0, args.get(0).withPrefix(removed.getPrefix())); } m = m.withArguments(args);