Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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 @@ -21,9 +21,11 @@
import org.openrewrite.TreeVisitor;
import org.openrewrite.java.tree.J;
import org.openrewrite.java.tree.JavaSourceFile;
import org.openrewrite.java.tree.JavaType;

import java.time.Duration;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

Expand Down Expand Up @@ -77,7 +79,8 @@ protected boolean shouldRename(Set<String> hasNameKey, J.VariableDeclarations.Na
if (toName.isEmpty() || !Character.isAlphabetic(toName.charAt(0))) {
return false;
}
return !hasNameKey.contains(toName);
Set<String> keys = computeAllKeys(toName, variable);
return keys.stream().noneMatch(hasNameKey::contains);
}

@Override
Expand All @@ -94,7 +97,7 @@ public J.VariableDeclarations visitVariableDeclarations(J.VariableDeclarations m
if (!LOWER_CAMEL.matches(name) && name.length() > 1) {
renameVariable(v, LOWER_CAMEL.format(name));
} else {
hasNameKey(name);
hasNameKey(computeKey(name, v));
}
}
return mv;
Expand Down Expand Up @@ -140,7 +143,7 @@ private boolean isDeclaredInCatch() {

@Override
public J.Identifier visitIdentifier(J.Identifier identifier, ExecutionContext ctx) {
hasNameKey(identifier.getSimpleName());
hasNameKey(computeKey(identifier.getSimpleName(), identifier));
return identifier;
}

Expand All @@ -167,6 +170,24 @@ private Cursor getCursorToParentScope(Cursor cursor) {
is instanceof JavaSourceFile
);
}

private Set<String> computeAllKeys(String identifier, J context) {
Set<String> keys = new HashSet<>();
keys.add(identifier);
JavaType.Variable fieldType = getFieldType(context);
if (fieldType != null && fieldType.getOwner() != null) {
keys.add(fieldType.getOwner() + " " + identifier);
if (fieldType.getOwner() instanceof JavaType.Method) {
// Add all enclosing classes
JavaType.FullyQualified declaringType = ((JavaType.Method) fieldType.getOwner()).getDeclaringType();
while (declaringType != null) {
keys.add(declaringType + " " + identifier);
declaringType = declaringType.getOwningClass();
}
}
}
return keys;
}
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,12 @@ protected boolean shouldRename(Set<String> hasNameKey, J.VariableDeclarations.Na
if (toName.isEmpty() || !Character.isAlphabetic(toName.charAt(0))) {
return false;
}
return !hasNameKey.contains(toName) && !hasNameKey.contains(variable.getSimpleName());
return hasNameKey.stream().noneMatch(key ->
key.equals(toName) ||
key.equals(variable.getSimpleName()) ||
key.endsWith(" " + toName) ||
key.endsWith(" " + variable.getSimpleName())
);
}

@SuppressWarnings("all")
Expand Down Expand Up @@ -110,7 +115,7 @@ public J.VariableDeclarations.NamedVariable visitVariable(J.VariableDeclarations
String toName = LOWER_CAMEL.format(variable.getSimpleName());
renameVariable(variable, toName);
} else {
hasNameKey(variable.getSimpleName());
hasNameKey(computeKey(variable.getSimpleName(), variable));
}

return super.visitVariable(variable, ctx);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.openrewrite.java.RenameVariable;
import org.openrewrite.java.tree.J;
import org.openrewrite.java.tree.JavaSourceFile;
import org.openrewrite.java.tree.JavaType;

import java.util.HashSet;
import java.util.LinkedHashMap;
Expand All @@ -43,7 +44,7 @@ public abstract class RenameToCamelCase extends JavaIsoVisitor<ExecutionContext>
String toName = entry.getValue();
if (shouldRename(hasNameSet, variable, toName)) {
cu = (JavaSourceFile) new RenameVariable<>(variable, toName).visitNonNull(cu, ctx);
hasNameSet.add(toName);
hasNameSet.add(computeKey(toName, variable));
}
}
return cu;
Expand All @@ -65,4 +66,23 @@ protected void hasNameKey(String variableName) {
.computeMessageIfAbsent("HAS_NAME_KEY", k -> new HashSet<>())
.add(variableName);
}

protected String computeKey(String identifier, J context) {
JavaType.Variable fieldType = getFieldType(context);
if (fieldType != null && fieldType.getOwner() != null) {
return fieldType.getOwner() + " " + identifier;
}
return identifier;
}

@Nullable
protected JavaType.Variable getFieldType(J tree) {
if (tree instanceof J.Identifier) {
return ((J.Identifier) tree).getFieldType();
}
if (tree instanceof J.VariableDeclarations.NamedVariable) {
return ((J.VariableDeclarations.NamedVariable) tree).getVariableType();
}
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -372,4 +372,68 @@ public void doSomething() {
)
);
}

@Test
@Issue("https://github.com/openrewrite/rewrite-static-analysis/issues/171")
void renameMultipleOcurrencesDifferentScope() {
rewriteRun(
java(
"""
class Test {
void test() {
String ID;
}
void test2() {
String ID;
}
}
""",
"""
class Test {
void test() {
String id;
}
void test2() {
String id;
}
}
"""
)
);
}

@Test
void doNotRenameIfInParent() {
rewriteRun(
java(
"""
class Test {
String id;
void test() {
String ID;
}
}
"""
)
);
}

@Test
void doNotRenameIfInParentFromInnerClass() {
rewriteRun(
java(
"""
class Test {
String id;
class InnerClass {
void test() {
String ID;
}
}
}
"""
)
);
}

}