Skip to content
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

update method type on DeleteMethodArgument #3660

Merged
merged 1 commit into from
Nov 3, 2023
Merged
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 @@ -37,8 +37,7 @@ public B(int n) {}
@Test
void deleteMiddleArgumentDeclarative() {
rewriteRun(
spec -> spec.recipe(new DeleteMethodArgument("B foo(int, int, int)", 1))
.cycles(1).expectedCyclesThatMakeChanges(1),
spec -> spec.recipes(new DeleteMethodArgument("B foo(int, int, int)", 1)),
java(b),
java(
"public class A {{ B.foo(0, 1, 2); }}",
Expand All @@ -50,8 +49,7 @@ void deleteMiddleArgumentDeclarative() {
@Test
void deleteMiddleArgument() {
rewriteRun(
spec -> spec.recipe(new DeleteMethodArgument("B foo(int, int, int)", 1))
.cycles(1).expectedCyclesThatMakeChanges(1),
spec -> spec.recipe(new DeleteMethodArgument("B foo(int, int, int)", 1)),
java(b),
java(
"public class A {{ B.foo(0, 1, 2); }}",
Expand All @@ -65,7 +63,7 @@ void deleteArgumentsConsecutively() {
rewriteRun(
spec -> spec.recipes(
new DeleteMethodArgument("B foo(int, int, int)", 1),
new DeleteMethodArgument("B foo(int, int, int)", 1)
new DeleteMethodArgument("B foo(int, int)", 1)
Comment on lines -68 to +66
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we are updating the methodType, now it is required to match the new resulting "signature"

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here is one case where that was required: openrewrite/rewrite-testing-frameworks@47ccd37

),
java(b),
java("public class A {{ B.foo(0, 1, 2); }}",
Expand Down Expand Up @@ -98,8 +96,7 @@ void insertEmptyWhenLastArgumentIsDeleted() {
@Test
void deleteConstructorArgument() {
rewriteRun(
spec -> spec.recipe(new DeleteMethodArgument("B <constructor>(int)", 0))
.cycles(1).expectedCyclesThatMakeChanges(1),
spec -> spec.recipe(new DeleteMethodArgument("B <constructor>(int)", 0)),
java(b),
java(
"public class A { B b = new B(0); }",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,7 @@
import lombok.Value;
import org.openrewrite.*;
import org.openrewrite.java.search.UsesMethod;
import org.openrewrite.java.tree.Expression;
import org.openrewrite.java.tree.J;
import org.openrewrite.java.tree.MethodCall;
import org.openrewrite.java.tree.Space;
import org.openrewrite.java.tree.*;
import org.openrewrite.marker.Markers;

import java.util.ArrayList;
Expand Down Expand Up @@ -104,6 +101,18 @@ private MethodCall visitMethodCall(MethodCall methodCall) {
}

m = m.withArguments(args);

JavaType.Method methodType = m.getMethodType();
if (methodType != null) {
List<String> parameterNames = new ArrayList<>(methodType.getParameterNames());
parameterNames.remove(argumentIndex);
List<JavaType> parameterTypes = new ArrayList<>(methodType.getParameterTypes());
parameterTypes.remove(argumentIndex);

m = m.withMethodType(methodType
.withParameterNames(parameterNames)
.withParameterTypes(parameterTypes));
}
}
return m;
}
Expand Down
Loading