Skip to content

Rename variables if the name matches class name #5454

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -732,14 +732,4 @@ public static boolean containsWhitespace(String s) {

return false;
}

/**
* @return string with first character changed to lowercase (or empty string)
*/
public static String decapitalize(@Nullable String string) {
if (string != null && !string.isEmpty()) {
return Character.toLowerCase(string.charAt(0)) + string.substring(1);
}
return "";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1630,6 +1630,92 @@ public class Test {
);
}

@Test
void updateMethodTypeWithUnicodeCharacter() {
rewriteRun(
spec -> spec.recipe(new ChangeType("a.Ą1", "a.Ą2", false)),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since Ą is in the BMP, this test should pass with the old code as well. In order to really see the change going from characters to codepoints, you probably need something like 𐐓 (\uD801\uDC13).

Copy link
Contributor Author

@radoslaw-panuszewski radoslaw-panuszewski May 21, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried changing Ą/ą to 𐐅/𐐭 but I encountered strange issues from unrelated parts of the code (like import statement started disappearing). I've spent some time debugging it but eventually gave up as it seems to be a broader issue and fixing it is out of scope of this PR.

I reverted the code of decapitalize method to the previous version and removed the test case with special characters. All of my experiments are in the git history in case someone would like to continue the fight from there 😄

java(
"""
package a;
public class Ą1 {
}
""",
"""
package a;
public class Ą2 {
}
"""
),
java(
"""
package org.foo;

import a.Ą1;

public class Example {
public Ą1 method(Ą1 ą1) {
return ą1;
}
}
""",
"""
package org.foo;

import a.Ą2;

public class Example {
public Ą2 method(Ą2 ą2) {
return ą2;
}
}
"""
)
);
}

@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 updateVariableType() {
rewriteRun(
Expand Down
23 changes: 21 additions & 2 deletions rewrite-java/src/main/java/org/openrewrite/java/ChangeType.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -32,7 +33,6 @@
import java.util.concurrent.atomic.AtomicBoolean;

import static java.util.Objects.requireNonNull;
import static org.openrewrite.internal.StringUtils.decapitalize;

@Value
@EqualsAndHashCode(callSuper = false)
Expand Down Expand Up @@ -372,7 +372,11 @@ public J visitIdentifier(J.Identifier ident, ExecutionContext ctx) {
// Rename variable if it matches class name (starting with a lowercase character)
if (ident.getSimpleName().equals(decapitalize(className))) {
if (targetType instanceof JavaType.FullyQualified) {
String newName = decapitalize(((JavaType.FullyQualified) targetType).getClassName());
String newName = VariableNameUtils.generateVariableName(
decapitalize(((JavaType.FullyQualified) targetType).getClassName()),
getCursor(),
GenerationStrategy.INCREMENT_NUMBER
);

ident = ident.withSimpleName(newName);

Expand Down Expand Up @@ -801,4 +805,19 @@ private static boolean hasSameFQN(J.Import import_, JavaType targetType) {

return fqn != null && fqn.equals(curFqn);
}

private static String decapitalize(String string) {
if (!string.isEmpty()) {
int firstCodePoint = string.codePointAt(0);
int firstCodePointLowercase = Character.toLowerCase(firstCodePoint);

if (firstCodePoint != firstCodePointLowercase) {
return new StringBuilder()
.appendCodePoint(firstCodePointLowercase)
.append(string.substring(Character.charCount(firstCodePoint)))
.toString();
}
}
return string;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ fun test(original: Original<String>) { }

import x.y.Target

fun test(original: Target<String>) { }
fun test(target: Target<String>) { }
"""
)
);
Expand Down Expand Up @@ -231,7 +231,7 @@ fun test(original: MyAlias<String>) { }

import x.y.Target as MyAlias

fun test(original: MyAlias<String>) { }
fun test(target: MyAlias<String>) { }
"""
)
);
Expand Down Expand Up @@ -263,7 +263,7 @@ fun test(original: a.b.Original<String>) { }

import x.y.Target

fun test(original: Target<String>) { }
fun test(target: Target<String>) { }
"""
)
);
Expand Down