0

I am writing a Eclipse ASTVisitor. How to tell if a field is read or written in a method?

The idea provided was "You need to vist Assignment node. Field on the LHS is written, while fields on the RHS expression is read."

After I visit the assignment and get the LHS and RHS which are both of Expression, how do I tell if the Expression contains the field?

4

1 回答 1

0

如果您正在做 AST 工作,我建议您使用AST View 插件。它是理解 JDT AST 的一个非常方便的工具。

你的方法会奏效。我在访问者中使用一个变量来表示我正在分配。

    public boolean visit(final Assignment node) {
    inVariableAssignment = true;
    node.getLeftHandSide().accept(this);
    inVariableAssignment = false;
    node.getRightHandSide().accept(this);
    return false;
}

现在,当访问 aSimpleName或 a时,QualifiedName我会执行以下操作:

    public boolean visit(final SimpleName node) {
    if (!node.isDeclaration()) {
        final IBinding nodeBinding = node.resolveBinding();
        if (nodeBinding instanceof IVariableBinding) {
            ...
        }
    }
    return false;
}

省略号 (...) 将被替换为根据您的值处理字段访问的代码inVariableAssignment。这会让你开始。

哦,别忘了PostfixExpression还有PrefixExpression...

于 2009-10-18T05:05:30.523 回答