diff --git a/rewrite-java-test/src/test/java/org/openrewrite/java/ChangeTypeTest.java b/rewrite-java-test/src/test/java/org/openrewrite/java/ChangeTypeTest.java index 936acbc7e22..e8be83430dc 100644 --- a/rewrite-java-test/src/test/java/org/openrewrite/java/ChangeTypeTest.java +++ b/rewrite-java-test/src/test/java/org/openrewrite/java/ChangeTypeTest.java @@ -1592,8 +1592,8 @@ public A1 method(A1 a1) { import a.A2; public class Example { - public A2 method(A2 a1) { - return a1; + public A2 method(A2 a2) { + return a2; } } """ @@ -1630,6 +1630,101 @@ public class Test { ); } + @Test + void doNotRenameRandomVariablesMatchingClassName() { + rewriteRun( + spec -> spec.recipe(new ChangeType("a.A1", "a.A2", false)), + java( + """ + package a; + public class A1 { + } + """, + """ + package a; + public class A2 { + } + """ + ), + java( + """ + package org.foo; + + import a.A1; + + public class Example { + public String method(A1 a, String a1) { + return a1; + } + } + """, + """ + package org.foo; + + import a.A2; + + public class Example { + public String method(A2 a, String a1) { + return a1; + } + } + """ + ) + ); + } + + @Test + void noChangeToVariableNameWithoutChangeToType() { + rewriteRun( + spec -> spec.recipe(new ChangeType("a.A1", "a.A2", true)), + java( + """ + package a; + public class A1 { + } + """ + ), + java( + """ + package a; + public class A2 { + } + """ + ), + java( + """ + package org.foo; + + import a.A1; + import a.A2; + + public class Example { + public A1 method1(A1 a1) { + return a1; + } + public A2 method2(A2 a1) { + return a1; // Unchanged + } + } + """, + """ + package org.foo; + + import a.A2; + + public class Example { + public A2 method1(A2 a2) { + return a2; + } + public A2 method2(A2 a1) { + return a1; // Unchanged + } + } + """ + ) + ); + } + @Test void updateVariableType() { rewriteRun( diff --git a/rewrite-java/src/main/java/org/openrewrite/java/ChangeType.java b/rewrite-java/src/main/java/org/openrewrite/java/ChangeType.java index f4b02a7fd9a..b572cb3517a 100644 --- a/rewrite-java/src/main/java/org/openrewrite/java/ChangeType.java +++ b/rewrite-java/src/main/java/org/openrewrite/java/ChangeType.java @@ -20,6 +20,7 @@ import org.jspecify.annotations.Nullable; import org.openrewrite.*; import org.openrewrite.internal.ListUtils; +import org.openrewrite.java.VariableNameUtils.GenerationStrategy; import org.openrewrite.java.search.UsesType; import org.openrewrite.java.tree.*; import org.openrewrite.marker.Markers; @@ -387,6 +388,26 @@ public J visitIdentifier(J.Identifier ident, ExecutionContext ctx) { return visitAndCast(ident, ctx, super::visitIdentifier); } + @Override + public J.VariableDeclarations.NamedVariable visitVariable(J.VariableDeclarations.NamedVariable variable, ExecutionContext ctx) { + J.VariableDeclarations.NamedVariable v = (J.VariableDeclarations.NamedVariable) super.visitVariable(variable, ctx); + if (v != variable) { + if (v.getSimpleName().equals(decapitalize(originalType.getClassName()))) { + if (targetType instanceof JavaType.FullyQualified) { + if (v.getVariableType() != null && TypeUtils.isOfType(targetType, v.getVariableType().getType())) { + String newName = VariableNameUtils.generateVariableName( + decapitalize(((JavaType.FullyQualified) targetType).getClassName()), + updateCursor(v), + GenerationStrategy.INCREMENT_NUMBER + ); + doAfterVisit(new RenameVariable<>(v, newName)); + } + } + } + } + return v; + } + @Override public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) { if (method.getMethodType() != null && method.getMethodType().hasFlags(Flag.Static)) { @@ -778,4 +799,11 @@ private static boolean hasSameFQN(J.Import import_, JavaType targetType) { return fqn != null && fqn.equals(curFqn); } + + private static String decapitalize(@Nullable String string) { + if (string != null && !string.isEmpty()) { + return Character.toLowerCase(string.charAt(0)) + string.substring(1); + } + return ""; + } }