diff --git a/src/main/java/org/openrewrite/java/migrate/ChangeMethodInvocationReturnType.java b/src/main/java/org/openrewrite/java/migrate/ChangeMethodInvocationReturnType.java index 00ccdbb79e..92c38dd386 100644 --- a/src/main/java/org/openrewrite/java/migrate/ChangeMethodInvocationReturnType.java +++ b/src/main/java/org/openrewrite/java/migrate/ChangeMethodInvocationReturnType.java @@ -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; @@ -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 ) diff --git a/src/test/java/org/openrewrite/java/migrate/ChangeMethodInvocationReturnTypeTest.java b/src/test/java/org/openrewrite/java/migrate/ChangeMethodInvocationReturnTypeTest.java index 68f80192ea..f2c3a0e5b0 100644 --- a/src/test/java/org/openrewrite/java/migrate/ChangeMethodInvocationReturnTypeTest.java +++ b/src/test/java/org/openrewrite/java/migrate/ChangeMethodInvocationReturnTypeTest.java @@ -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; @@ -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(); + } + } + """ + ) + ); + } }