Skip to content

Commit

Permalink
Add used enums into the list of used variable names (#4600)
Browse files Browse the repository at this point in the history
* Add used enums into the list of used variable names

Currently, Enums aren't being added to the list of used variable names which can result in collisions when naming new variable.

* Update rewrite-java/src/main/java/org/openrewrite/java/VariableNameUtils.java

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>

---------

Co-authored-by: Sam Snyder <sam@moderne.io>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
  • Loading branch information
3 people authored Oct 22, 2024
1 parent 212059e commit 8311e0c
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,23 @@ class Test {
);
}

@Test
void enumFieldNames() {
rewriteRun(
baseTest("myType", "myType,TYPES"),
java(
"""
class Test {
TYPES myType;
}
enum TYPES {
VALUE, NUMBER, TEXT
}
"""
)
);
}

@Test
void allClassFieldsAreFound() {
rewriteRun(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,16 @@ public J.VariableDeclarations.NamedVariable visitVariable(J.VariableDeclarations
return super.visitVariable(variable, strings);
}

@Override
public J.Identifier visitIdentifier(J.Identifier identifier, Set<String> namesInScope) {
J.Identifier v = super.visitIdentifier(identifier, namesInScope);
if (v.getType() instanceof JavaType.Class &&
((JavaType.Class) v.getType()).getKind() == JavaType.FullyQualified.Kind.Enum) {
namesInScope.add(v.getSimpleName());
}
return v;
}

private void addImportedStaticFieldNames(@Nullable JavaSourceFile cu, Cursor classCursor) {
if (cu != null) {
List<J.Import> imports = cu.getImports();
Expand Down

0 comments on commit 8311e0c

Please sign in to comment.