Skip to content

Commit 8551095

Browse files
Rename variables if the name matches class name
1 parent 19ca58c commit 8551095

File tree

3 files changed

+35
-2
lines changed

3 files changed

+35
-2
lines changed

rewrite-core/src/main/java/org/openrewrite/internal/StringUtils.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -732,4 +732,14 @@ public static boolean containsWhitespace(String s) {
732732

733733
return false;
734734
}
735+
736+
/**
737+
* @return string with first character changed to lowercase (or empty string)
738+
*/
739+
public static String decapitalize(@Nullable String string) {
740+
if (string != null && !string.isEmpty()) {
741+
return Character.toLowerCase(string.charAt(0)) + string.substring(1);
742+
}
743+
return "";
744+
}
735745
}

rewrite-java-test/src/test/java/org/openrewrite/java/ChangeTypeTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1592,8 +1592,8 @@ public A1 method(A1 a1) {
15921592
import a.A2;
15931593
15941594
public class Example {
1595-
public A2 method(A2 a1) {
1596-
return a1;
1595+
public A2 method(A2 a2) {
1596+
return a2;
15971597
}
15981598
}
15991599
"""

rewrite-java/src/main/java/org/openrewrite/java/ChangeType.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import java.util.concurrent.atomic.AtomicBoolean;
3333

3434
import static java.util.Objects.requireNonNull;
35+
import static org.openrewrite.internal.StringUtils.decapitalize;
3536

3637
@Value
3738
@EqualsAndHashCode(callSuper = false)
@@ -368,6 +369,19 @@ public J visitIdentifier(J.Identifier ident, ExecutionContext ctx) {
368369
}
369370
}
370371

372+
// Rename variable if it matches class name (starting with a lowercase character)
373+
if (ident.getSimpleName().equals(decapitalize(className))) {
374+
if (targetType instanceof JavaType.FullyQualified) {
375+
String newName = decapitalize(((JavaType.FullyQualified) targetType).getClassName());
376+
377+
ident = ident.withSimpleName(newName);
378+
379+
if (ident.getFieldType() != null) {
380+
ident = ident.withFieldType(ident.getFieldType().withName(newName));
381+
}
382+
}
383+
}
384+
371385
// Recreate any static imports as needed
372386
if (sf != null) {
373387
for (J.Import anImport : sf.getImports()) {
@@ -387,6 +401,15 @@ public J visitIdentifier(J.Identifier ident, ExecutionContext ctx) {
387401
return visitAndCast(ident, ctx, super::visitIdentifier);
388402
}
389403

404+
@Override
405+
public J.VariableDeclarations.NamedVariable visitVariable(J.VariableDeclarations.NamedVariable variable, ExecutionContext executionContext) {
406+
J.VariableDeclarations.NamedVariable v = (J.VariableDeclarations.NamedVariable) super.visitVariable(variable, executionContext);
407+
if (v.getVariableType() != null && !v.getSimpleName().equals(v.getVariableType().getName())) {
408+
return v.withVariableType(v.getVariableType().withName(v.getSimpleName()));
409+
}
410+
return v;
411+
}
412+
390413
@Override
391414
public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) {
392415
if (method.getMethodType() != null && method.getMethodType().hasFlags(Flag.Static)) {

0 commit comments

Comments
 (0)