Skip to content

Commit

Permalink
ChangeMethodInvocationReturnType should shorten FQCN (#521)
Browse files Browse the repository at this point in the history
  • Loading branch information
timtebeek authored Jul 30, 2024
1 parent 6ba16cc commit 67f25e2
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public class ChangeMethodInvocationReturnType extends Recipe {
String methodPattern;

@Option(displayName = "New method invocation return type",
description = "The return return type of method invocation.",
description = "The fully qualified new return type of method invocation.",
example = "long")
String newReturnType;

Expand Down Expand Up @@ -96,7 +96,7 @@ public J.VariableDeclarations visitVariableDeclarations(J.VariableDeclarations m
mv.getTypeExpression().getPrefix(),
Markers.EMPTY,
emptyList(),
newReturnType,
newReturnType.substring(newReturnType.lastIndexOf('.') + 1),
newType,
null
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import org.junit.jupiter.api.Test;
import org.openrewrite.DocumentExample;
import org.openrewrite.java.JavaParser;
import org.openrewrite.test.RecipeSpec;
import org.openrewrite.test.RewriteTest;

Expand Down Expand Up @@ -79,4 +80,46 @@ void bar() {
)
);
}

@Test
void replaceVariableAssignmentFullyQualified() {
rewriteRun(
spec -> spec.recipe(new ChangeMethodInvocationReturnType("bar.Bar bar()", "java.math.BigInteger"))
.parser(JavaParser.fromJavaVersion()
//language=java
.dependsOn(
"""
package bar;
public class Bar {
public static Integer bar() {
return null;
}
}
"""
)
),
//language=java
java(
"""
import bar.Bar;
class Foo {
void foo() {
Integer one = Bar.bar();
}
}
""",
"""
import bar.Bar;
import java.math.BigInteger;
class Foo {
void foo() {
BigInteger one = Bar.bar();
}
}
"""
)
);
}
}

0 comments on commit 67f25e2

Please sign in to comment.