diff --git a/src/main/java/org/openrewrite/java/spring/ChangeMethodParameter.java b/src/main/java/org/openrewrite/java/spring/ChangeMethodParameter.java index c779a9c89..8ad35d1c7 100644 --- a/src/main/java/org/openrewrite/java/spring/ChangeMethodParameter.java +++ b/src/main/java/org/openrewrite/java/spring/ChangeMethodParameter.java @@ -37,7 +37,7 @@ /** * A recipe to change parameter type for a method declaration. - *

+ *

* NOTE: This recipe is usually used for initial modification of parameter changes. * After modifying method parameters using this recipe, you may also need to modify * the method definition as needed to avoid compilation errors. @@ -63,7 +63,6 @@ public class ChangeMethodParameter extends Recipe { @Option(displayName = "Parameter index", description = "A zero-based index that indicates the position at which the parameter will be added.", example = "0") - @Nullable Integer parameterIndex; @Override @@ -97,23 +96,20 @@ public ChangeMethodArgumentVisitor(String methodPattern) { } @Override - public J.MethodDeclaration visitMethodDeclaration(J.MethodDeclaration method, ExecutionContext ctx) { - method = super.visitMethodDeclaration(method, ctx); - if (parameterIndex == null || parameterIndex < 0) { - return method; - } + public J.MethodDeclaration visitMethodDeclaration(J.MethodDeclaration methodDeclaration, ExecutionContext ctx) { + J.MethodDeclaration md = super.visitMethodDeclaration(methodDeclaration, ctx); J.ClassDeclaration enclosing = getCursor().firstEnclosing(J.ClassDeclaration.class); - if (enclosing != null && methodMatcher.matches(method, enclosing)) { - if (method.getParameters().isEmpty() || method.getParameters().size() <= parameterIndex) { - return method; + if (enclosing != null && methodMatcher.matches(md, enclosing)) { + if (md.getParameters().isEmpty() || md.getParameters().size() <= parameterIndex) { + return md; } - if (method.getParameters().get(parameterIndex) instanceof J.VariableDeclarations) { - J.VariableDeclarations parameter = (J.VariableDeclarations) method.getParameters().get(parameterIndex); + if (md.getParameters().get(parameterIndex) instanceof J.VariableDeclarations) { + J.VariableDeclarations parameter = (J.VariableDeclarations) md.getParameters().get(parameterIndex); TypeTree typeTree = createTypeTree(parameterType); if (TypeUtils.isOfType(parameter.getType(), typeTree.getType())) { - return method; + return md; } String parameterName = parameter.getVariables().get(0).getSimpleName(); @@ -133,7 +129,7 @@ public J.MethodDeclaration visitMethodDeclaration(J.MethodDeclaration method, Ex null, 0, parameterName, - method.getMethodType(), + md.getMethodType(), typeTree.getType(), null ) @@ -144,11 +140,11 @@ public J.MethodDeclaration visitMethodDeclaration(J.MethodDeclaration method, Ex ) )); - method = autoFormat(changeParameter(method, parameter), parameter, ctx, getCursor().getParentTreeCursor()); + md = autoFormat(changeParameter(md, parameter), parameter, ctx, getCursor().getParentTreeCursor()); } } - return method; + return md; } private J.MethodDeclaration changeParameter(J.MethodDeclaration method, J.VariableDeclarations parameter) { diff --git a/src/test/java/org/openrewrite/java/spring/ChangeMethodParameterTest.java b/src/test/java/org/openrewrite/java/spring/ChangeMethodParameterTest.java index 3d8f3210c..7c859bc5a 100644 --- a/src/test/java/org/openrewrite/java/spring/ChangeMethodParameterTest.java +++ b/src/test/java/org/openrewrite/java/spring/ChangeMethodParameterTest.java @@ -28,6 +28,7 @@ class ChangeMethodParameterTest implements RewriteTest { void primitive() { rewriteRun( spec -> spec.recipe(new ChangeMethodParameter("foo.Foo#bar(..)", "long", 0)), + //language=java java( """ package foo; @@ -53,6 +54,7 @@ public void bar(long i) { void indexLargeThanZero() { rewriteRun( spec -> spec.recipe(new ChangeMethodParameter("foo.Foo#bar(..)", "long", 1)), + //language=java java( """ package foo; @@ -78,23 +80,7 @@ public void bar(int i, long j) { void sameType() { rewriteRun( spec -> spec.recipe(new ChangeMethodParameter("foo.Foo#bar(..)", "int", 0)), - java( - """ - package foo; - - public class Foo { - public void bar(int i) { - } - } - """ - ) - ); - } - - @Test - void invalidIndex() { - rewriteRun( - spec -> spec.recipe(new ChangeMethodParameter("foo.Foo#bar(..)", "int", -1)), + //language=java java( """ package foo; @@ -112,6 +98,7 @@ public void bar(int i) { void notExistsIndex() { rewriteRun( spec -> spec.recipe(new ChangeMethodParameter("foo.Foo#bar(..)", "int", 1)), + //language=java java( """ package foo; @@ -129,6 +116,7 @@ public void bar(int i) { void typePattern() { rewriteRun( spec -> spec.recipe(new ChangeMethodParameter("*..*#bar(..)", "long", 0)), + //language=java java( """ package foo; @@ -154,6 +142,7 @@ public void bar(long i) { void primitiveArray() { rewriteRun( spec -> spec.recipe(new ChangeMethodParameter("foo.Foo#bar(..)", "int[]", 0)), + //language=java java( """ package foo; @@ -179,6 +168,7 @@ public void bar(int[] i) { void parameterized() { rewriteRun( spec -> spec.recipe(new ChangeMethodParameter("foo.Foo#bar(..)", "java.util.List", 0)), + //language=java java( """ package foo; @@ -207,6 +197,7 @@ public void bar(List i) { void wildcard() { rewriteRun( spec -> spec.recipe(new ChangeMethodParameter("foo.Foo#bar(..)", "java.util.List", 0)), + //language=java java( """ package foo; @@ -234,6 +225,7 @@ public void bar(List i) { void wildcardExtends() { rewriteRun( spec -> spec.recipe(new ChangeMethodParameter("foo.Foo#bar(..)", "java.util.List", 0)), + //language=java java( """ package foo; @@ -261,6 +253,7 @@ public void bar(List i) { void string() { rewriteRun( spec -> spec.recipe(new ChangeMethodParameter("foo.Foo#bar(..)", "String", 0)), + //language=java java( """ package foo; @@ -298,6 +291,7 @@ public void bar(String i, int j) { void first() { rewriteRun( spec -> spec.recipe(new ChangeMethodParameter("foo.Foo#bar(..)", "long", 0)), + //language=java java( """ package foo; @@ -329,6 +323,7 @@ public void bar(long i, int j) { void qualified() { rewriteRun( spec -> spec.recipe(new ChangeMethodParameter("foo.Foo#bar(..)", "java.util.regex.Pattern", 0)), + //language=java java( """ package foo; @@ -361,6 +356,7 @@ public void bar(Pattern i, int j) { void object() { rewriteRun( spec -> spec.recipe(new ChangeMethodParameter("foo.Foo#bar(..)", "Object", 0)), + //language=java java( """ package foo;