Skip to content

Commit

Permalink
Speed up RenameLocalVariablesToCamelCase a bit
Browse files Browse the repository at this point in the history
  • Loading branch information
knutwannheden committed Mar 14, 2024
1 parent 9bb285a commit 8007d4f
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@

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

Expand Down Expand Up @@ -75,12 +74,11 @@ public Duration getEstimatedEffortPerOccurrence() {
public TreeVisitor<?, ExecutionContext> getVisitor() {
return new RenameToCamelCase() {
@Override
protected boolean shouldRename(Set<String> hasNameKey, J.VariableDeclarations.NamedVariable variable, String toName) {
protected boolean shouldRename(Set<String> hasNameSet, J.VariableDeclarations.NamedVariable variable, String toName) {
if (toName.isEmpty() || !Character.isAlphabetic(toName.charAt(0))) {
return false;
}
Set<String> keys = computeAllKeys(toName, variable);
return keys.stream().noneMatch(hasNameKey::contains);
return isAvailableIdentifier(toName, variable, hasNameSet);
}

@Override
Expand Down Expand Up @@ -171,22 +169,27 @@ private Cursor getCursorToParentScope(Cursor cursor) {
);
}

private Set<String> computeAllKeys(String identifier, J context) {
Set<String> keys = new HashSet<>();
keys.add(identifier);
private boolean isAvailableIdentifier(String identifier, J context, Set<String> hasNameSet) {
if (hasNameSet.contains(identifier)) {
return false;
}
JavaType.Variable fieldType = getFieldType(context);
if (fieldType != null && fieldType.getOwner() != null) {
keys.add(fieldType.getOwner() + " " + identifier);
if (hasNameSet.contains(fieldType.getOwner() + " " + identifier)) {
return false;
}
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);
if (hasNameSet.contains(declaringType + " " + identifier)) {
return false;
}
declaringType = declaringType.getOwningClass();
}
}
}
return keys;
return true;
}
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,11 @@ public Duration getEstimatedEffortPerOccurrence() {
public TreeVisitor<?, ExecutionContext> getVisitor() {
return new RenameToCamelCase() {
@Override
protected boolean shouldRename(Set<String> hasNameKey, J.VariableDeclarations.NamedVariable variable, String toName) {
protected boolean shouldRename(Set<String> hasNameSet, J.VariableDeclarations.NamedVariable variable, String toName) {
if (toName.isEmpty() || !Character.isAlphabetic(toName.charAt(0))) {
return false;
}
return hasNameKey.stream().noneMatch(key ->
return hasNameSet.stream().noneMatch(key ->
key.equals(toName) ||
key.equals(variable.getSimpleName()) ||
key.endsWith(" " + toName) ||
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,16 @@

public abstract class RenameToCamelCase extends JavaIsoVisitor<ExecutionContext> {

@Nullable
private Cursor sourceFileCursor;

private Cursor getSourceFileCursor() {
if (sourceFileCursor == null) {
sourceFileCursor = getCursor().getPathAsCursors(c -> c.getValue() instanceof JavaSourceFile).next();
}
return sourceFileCursor;
}

@Override
public @Nullable J postVisit(J tree, ExecutionContext ctx) {
if (tree instanceof JavaSourceFile) {
Expand All @@ -52,17 +62,17 @@ public abstract class RenameToCamelCase extends JavaIsoVisitor<ExecutionContext>
return super.postVisit(tree, ctx);
}

protected abstract boolean shouldRename(Set<String> hasNameKey, J.VariableDeclarations.NamedVariable variable,
protected abstract boolean shouldRename(Set<String> hasNameSet, J.VariableDeclarations.NamedVariable variable,
String toName);

protected void renameVariable(J.VariableDeclarations.NamedVariable variable, String toName) {
Cursor cu = getCursor().getPathAsCursors(c -> c.getValue() instanceof JavaSourceFile).next();
cu.computeMessageIfAbsent("RENAME_VARIABLES_KEY", k -> new LinkedHashMap<>())
getSourceFileCursor()
.computeMessageIfAbsent("RENAME_VARIABLES_KEY", k -> new LinkedHashMap<>())
.put(variable, toName);
}

protected void hasNameKey(String variableName) {
getCursor().getPathAsCursors(c -> c.getValue() instanceof JavaSourceFile).next()
getSourceFileCursor()
.computeMessageIfAbsent("HAS_NAME_KEY", k -> new HashSet<>())
.add(variableName);
}
Expand Down

0 comments on commit 8007d4f

Please sign in to comment.