If you are doing AST work, I suggest working with the AST View plugin. It is a very handy tool for understanding the JDT AST.
Your approach will work. I use a variable inside the visitor to indicate I'm in an assignment.
public boolean visit(final Assignment node) {
inVariableAssignment = true;
node.getLeftHandSide().accept(this);
inVariableAssignment = false;
node.getRightHandSide().accept(this);
return false;
}
Now, when visiting a SimpleName
or a QualifiedName
I do something like:
public boolean visit(final SimpleName node) {
if (!node.isDeclaration()) {
final IBinding nodeBinding = node.resolveBinding();
if (nodeBinding instanceof IVariableBinding) {
...
}
}
return false;
}
The ellipsis (...) will be replaced by a code which handles the field access according to the value of your inVariableAssignment
. This will get you started.
Oh, don't forget PostfixExpression
and PrefixExpression
...