如何获取ASTVisitor节点的全名

时间:2014-04-10 17:20:28

标签: java abstract-syntax-tree

我已经实现了一个AST访问者,它访问了每个方法调用节点。方法

node.getName()

给我方法的名称,但我想知道全名,Package.Class.Method。我确信有一种简单的方法可以解决这个问题,但我找不到任何东西。以下是我到目前为止的情况:

public boolean visit(MethodInvocation node) {
    assert callmap.containsKey(curMethod);
    String m = node.getName().toString();
    callmap.get(curMethod).add(m);
    return false; // do not continue to avoid usage info
}

如何获取此方法的全名?

1 个答案:

答案 0 :(得分:0)

你应该使用类似的东西:

    IMethodBinding binding = node.resolveMethodBinding();        
    ITypeBinding typeBinding = expression.resolveTypeBinding();
    ITypeBinding type = binding.getDeclaringClass();        
    Expression expression = node.getExpression();


    System.out.println("Type: " + typeBinding.getName());

从文档中我发现了许多你想要的方法: http://help.eclipse.org/indigo/index.jsp?topic=%2Forg.eclipse.jdt.doc.isv%2Freference%2Fapi%2Forg%2Feclipse%2Fjdt%2Fcore%2Fdom%2FMethodInvocation.html

http://help.eclipse.org/indigo/index.jsp?topic=%2Forg.eclipse.jdt.doc.isv%2Freference%2Fapi%2Forg%2Feclipse%2Fjdt%2Fcore%2Fdom%2FIMethodBinding.html

http://help.eclipse.org/indigo/index.jsp?topic=%2Forg.eclipse.jdt.doc.isv%2Freference%2Fapi%2Forg%2Feclipse%2Fjdt%2Fcore%2Fdom%2FITypeBinding.html

尝试检查我给你的可变的所有方法

您的问题与How to get a class name of a method by using Eclipse JDT ASTParser?

类似
相关问题