Skip to content

Commit

Permalink
Polish. Use HashSet in RemoveUnusedLocalVariables.
Browse files Browse the repository at this point in the history
  • Loading branch information
traceyyoshima committed Jan 18, 2022
1 parent 5cb904f commit 6576255
Showing 1 changed file with 12 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,19 @@ public Duration getEstimatedEffortPerOccurrence() {

@Override
protected TreeVisitor<?, ExecutionContext> getVisitor() {
return new RemoveUnusedLocalVariablesVisitor();
return new RemoveUnusedLocalVariablesVisitor(ignoreVariablesNamed);
}

private class RemoveUnusedLocalVariablesVisitor extends JavaIsoVisitor<ExecutionContext> {
private static class RemoveUnusedLocalVariablesVisitor extends JavaIsoVisitor<ExecutionContext> {
private Set<String> ignoreVariableNames;

RemoveUnusedLocalVariablesVisitor(String[] ignoreVariablesNamed) {
if (ignoreVariablesNamed != null) {
ignoreVariableNames = new HashSet<>(ignoreVariablesNamed.length);
ignoreVariableNames.addAll(Arrays.asList(ignoreVariablesNamed));
}
}

private Cursor getCursorToParentScope(Cursor cursor) {
return cursor.dropParentUntil(is ->
is instanceof J.Block ||
Expand All @@ -87,8 +96,7 @@ private Cursor getCursorToParentScope(Cursor cursor) {
@Override
public J.VariableDeclarations.NamedVariable visitVariable(J.VariableDeclarations.NamedVariable variable, ExecutionContext ctx) {
// skip matching ignored variable names right away
if (ignoreVariablesNamed != null &&
Arrays.stream(ignoreVariablesNamed).anyMatch(v -> variable.getSimpleName().equals(v))) {
if (ignoreVariableNames != null && ignoreVariableNames.contains(variable.getSimpleName())) {
return variable;
}

Expand Down

0 comments on commit 6576255

Please sign in to comment.