Skip to content

Commit

Permalink
Minor polish
Browse files Browse the repository at this point in the history
  • Loading branch information
timtebeek committed Nov 19, 2024
1 parent e1675a8 commit 7060d50
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@

/**
* A recipe to change parameter type for a method declaration.
* <P>
* <p>
* 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.
Expand All @@ -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
Expand Down Expand Up @@ -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();
Expand All @@ -133,7 +129,7 @@ public J.MethodDeclaration visitMethodDeclaration(J.MethodDeclaration method, Ex
null,
0,
parameterName,
method.getMethodType(),
md.getMethodType(),
typeTree.getType(),
null
)
Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -179,6 +168,7 @@ public void bar(int[] i) {
void parameterized() {
rewriteRun(
spec -> spec.recipe(new ChangeMethodParameter("foo.Foo#bar(..)", "java.util.List<java.util.regex.Pattern>", 0)),
//language=java
java(
"""
package foo;
Expand Down Expand Up @@ -207,6 +197,7 @@ public void bar(List<Pattern> i) {
void wildcard() {
rewriteRun(
spec -> spec.recipe(new ChangeMethodParameter("foo.Foo#bar(..)", "java.util.List<?>", 0)),
//language=java
java(
"""
package foo;
Expand Down Expand Up @@ -234,6 +225,7 @@ public void bar(List<?> i) {
void wildcardExtends() {
rewriteRun(
spec -> spec.recipe(new ChangeMethodParameter("foo.Foo#bar(..)", "java.util.List<? extends Object>", 0)),
//language=java
java(
"""
package foo;
Expand Down Expand Up @@ -261,6 +253,7 @@ public void bar(List<? extends Object> i) {
void string() {
rewriteRun(
spec -> spec.recipe(new ChangeMethodParameter("foo.Foo#bar(..)", "String", 0)),
//language=java
java(
"""
package foo;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 7060d50

Please sign in to comment.