-
Notifications
You must be signed in to change notification settings - Fork 423
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
base: main
Are you sure you want to change the base?
Changes from all commits
8551095
8e132e2
0820d5c
f2f61a6
5bad96a
9c4ea8f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
@@ -368,6 +369,23 @@ 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 = VariableNameUtils.generateVariableName( | ||
decapitalize(((JavaType.FullyQualified) targetType).getClassName()), | ||
getCursor(), | ||
GenerationStrategy.INCREMENT_NUMBER | ||
); | ||
|
||
ident = ident.withSimpleName(newName); | ||
|
||
if (ident.getFieldType() != null) { | ||
ident = ident.withFieldType(ident.getFieldType().withName(newName)); | ||
} | ||
} | ||
} | ||
|
||
// Recreate any static imports as needed | ||
if (sf != null) { | ||
for (J.Import anImport : sf.getImports()) { | ||
|
@@ -387,6 +405,15 @@ 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 executionContext) { | ||
J.VariableDeclarations.NamedVariable v = (J.VariableDeclarations.NamedVariable) super.visitVariable(variable, executionContext); | ||
if (v.getVariableType() != null && !v.getSimpleName().equals(v.getVariableType().getName())) { | ||
return v.withVariableType(v.getVariableType().withName(v.getSimpleName())); | ||
} | ||
Comment on lines
+411
to
+413
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would we be replacing any variable here? Or only when it matched the old variable name? Want to make sure there's no unexpected changes, as the recipe is used so often, and the tests are very minimal. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This piece of code only updates variable type name if the underlying I've added a test case There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Great thanks! I'm rushing out the door here, but will ask the wider team to review while I'm out over the next few days. |
||
return v; | ||
} | ||
|
||
@Override | ||
public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) { | ||
if (method.getMethodType() != null && method.getMethodType().hasFlags(Flag.Static)) { | ||
|
@@ -778,4 +805,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 ""; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Neat idea indeed to rename these as well!