Skip to content

Commit 67f25e2

Browse files
authored
ChangeMethodInvocationReturnType should shorten FQCN (#521)
1 parent 6ba16cc commit 67f25e2

File tree

2 files changed

+45
-2
lines changed

2 files changed

+45
-2
lines changed

src/main/java/org/openrewrite/java/migrate/ChangeMethodInvocationReturnType.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public class ChangeMethodInvocationReturnType extends Recipe {
4141
String methodPattern;
4242

4343
@Option(displayName = "New method invocation return type",
44-
description = "The return return type of method invocation.",
44+
description = "The fully qualified new return type of method invocation.",
4545
example = "long")
4646
String newReturnType;
4747

@@ -96,7 +96,7 @@ public J.VariableDeclarations visitVariableDeclarations(J.VariableDeclarations m
9696
mv.getTypeExpression().getPrefix(),
9797
Markers.EMPTY,
9898
emptyList(),
99-
newReturnType,
99+
newReturnType.substring(newReturnType.lastIndexOf('.') + 1),
100100
newType,
101101
null
102102
)

src/test/java/org/openrewrite/java/migrate/ChangeMethodInvocationReturnTypeTest.java

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import org.junit.jupiter.api.Test;
1919
import org.openrewrite.DocumentExample;
20+
import org.openrewrite.java.JavaParser;
2021
import org.openrewrite.test.RecipeSpec;
2122
import org.openrewrite.test.RewriteTest;
2223

@@ -79,4 +80,46 @@ void bar() {
7980
)
8081
);
8182
}
83+
84+
@Test
85+
void replaceVariableAssignmentFullyQualified() {
86+
rewriteRun(
87+
spec -> spec.recipe(new ChangeMethodInvocationReturnType("bar.Bar bar()", "java.math.BigInteger"))
88+
.parser(JavaParser.fromJavaVersion()
89+
//language=java
90+
.dependsOn(
91+
"""
92+
package bar;
93+
public class Bar {
94+
public static Integer bar() {
95+
return null;
96+
}
97+
}
98+
"""
99+
)
100+
),
101+
//language=java
102+
java(
103+
"""
104+
import bar.Bar;
105+
class Foo {
106+
void foo() {
107+
Integer one = Bar.bar();
108+
}
109+
}
110+
""",
111+
"""
112+
import bar.Bar;
113+
114+
import java.math.BigInteger;
115+
116+
class Foo {
117+
void foo() {
118+
BigInteger one = Bar.bar();
119+
}
120+
}
121+
"""
122+
)
123+
);
124+
}
82125
}

0 commit comments

Comments
 (0)