在 JDT ASTVisitor 中查找 MethodInvocation 方法绑定

Find MethodInvocation method bindings in JDT ASTVisitor

我有一个使用 java.sql.Statement.execute 的 java 文件,如下所示。

public class Dummy
{
    public void execute(String q) throws SQLException
    {
        ...
        Statement stmt = conn.createStatement();
        ...
        stmt.execute(q);
        ...
    }
}

我的用例是我想使用 JDT ASTVisitor 识别所有 类 及其使用 "Statement.execute(String)" 的方法名称。这可能吗?

我使用 eclipse ASTView 插件找到了以下条目。

method binding: Statement.execute(String)

如何在我的 ASTVisitor 中获取此方法绑定值。

我试过了。

@Override
public boolean visit(MethodInvocation node)
{
    IMethodBinding iMethod = (IMethodBinding) node.resolveMethodBinding();
    if(iMethod != null)
    System.out.println("Binding "+iMethod.getName());
    return super.visit(node);
}

但 node.resolveMethodBinding() 始终 returns 为空。

... i want to identify what are all the classes and its method names which using "Statement.execute(String)"

这听起来像是 org.eclipse.jdt.core.search.SearchEngine 的工作,这将比使用访问者遍历所有源文件更快地产生结果。

... node.resolveMethodBinding() always returns null

这取决于你是如何获得AST的。参见,例如,org.eclipse.jdt.core.dom.ASTParser.setResolveBindings(boolean)