Skip to content
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

Merged
merged 4 commits into from
Jul 20, 2023

Conversation

aaronist
Copy link
Contributor

@aaronist aaronist commented Jul 20, 2023

  • Create findAllValues method for VarAccess
  • Add test cases for getAssignedValue

Comment on lines 139 to 147
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());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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) {
Copy link
Contributor

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.

Copy link
Contributor

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?

Comment on lines +127 to +129
if (methodDeclaration.getBody() == null) {
return Collections.emptySet();
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good!

Copy link
Contributor

@JLLeitschuh JLLeitschuh Jul 20, 2023

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;
    }
}

Copy link
Contributor

@JLLeitschuh JLLeitschuh left a 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!

Copy link
Contributor

@JLLeitschuh JLLeitschuh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@JLLeitschuh JLLeitschuh merged commit 6cc80ce into openrewrite:main Jul 20, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

2 participants