-
Notifications
You must be signed in to change notification settings - Fork 10
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
Add support for finding all values for VarAccess #25
Conversation
new JavaIsoVisitor<List<Expr>>() { | ||
@Override | ||
public @Nullable J visit(@Nullable Tree tree, List<Expr> exprs, Cursor parent) { | ||
if (tree instanceof J.FieldAccess) { | ||
varAccess[0] = VarAccess.viewOf(new Cursor(parent, ((J.FieldAccess) tree).getName())); | ||
} | ||
return super.visit(tree, exprs, parent); | ||
} | ||
}.visit(assignment.getVariable(), exprs, getCursor()); |
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.
new JavaIsoVisitor<List<Expr>>() { | |
@Override | |
public @Nullable J visit(@Nullable Tree tree, List<Expr> exprs, Cursor parent) { | |
if (tree instanceof J.FieldAccess) { | |
varAccess[0] = VarAccess.viewOf(new Cursor(parent, ((J.FieldAccess) tree).getName())); | |
} | |
return super.visit(tree, exprs, parent); | |
} | |
}.visit(assignment.getVariable(), exprs, getCursor()); | |
new JavaIsoVisitor<Integer>() { | |
@Override | |
public J.FieldAccess visitFieldAccess(J.FieldAccess fieldAccess, Integer x) { | |
varAccess[0] = VarAccess.viewOf(new Cursor(parent, fieldAccess.getName())); | |
return super.visitFieldAccess(tree, x); | |
} | |
}.visit(assignment.getVariable(), 0, getCursor()); |
@@ -110,6 +111,50 @@ public J visitIdentifier(J.Identifier ident, List<VarAccess> varAccesses) { | |||
}.visit(scope.getValue(), varAccesses, scope.getParentOrThrow()); | |||
return varAccesses; | |||
} | |||
|
|||
static Collection<Expr> findAllValues(Cursor scope, Variable variable) { |
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.
I don't think this method should live here (on this interface) since it's not being used to find instances of VarAccess
unlike the method above. Also, I would call it findAssignedValues
.
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.
Maybe create a package-private class called VariableUtil
and put this method there?
if (methodDeclaration.getBody() == null) { | ||
return Collections.emptySet(); | ||
} |
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.
Good!
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.
One more test I would verify as well
class Test {
int i = 1;
void test(int i) {
this.i = 2;
i = 3;
}
void test() {
this.i = 4;
int i = 5;
i = 6;
}
}
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.
Excellent! Just one suggestion, once fixed, I'm happy to merge!
src/main/java/org/openrewrite/analysis/trait/variable/VariableUtil.java
Outdated
Show resolved
Hide resolved
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.
LGTM
findAllValues
method forVarAccess
getAssignedValue