JDT - AstParser - 获取为特定对象调用的方法列表

JDT - AstParser - Get the list of methods which were called for a specific object

例如,我有这样的代码

SomeObject1 obj1 = new SomeObject1();
SomeObject2 obj2 = new SomeObject2();
...
obj1.foo();
obj1.boo();
...
obj2.foo2();
obj2.boo2();

我想获得下一个输出:

Type: SomeObject1

Name: obj1

Called methods: foo, boo

==========

Type: SomeObject2

Name: obj2

Called methods: foo2, boo2

谢谢

更新: 我做了一个代码

public boolean visit(VariableDeclarationFragment v)
{
    System.out.println("Declaration of " + v.getName().resolveBinding().getKey());
    return true;
}

public boolean visit(MethodInvocation inv)
{               
    Expression e = inv.getExpression();

    if(e instanceof Name)
    {
        Name n = (Name) e;
        System.out.println("Calling the method \"" + inv.getName().getFullyQualifiedName() + "\" for " + n.resolveBinding().getKey());
    }
    return true;
}

Declaration of Ltest/C:\Test\src\Test~Test;.abc)I

Declaration of Ltest/C:\Test\src\Test~Test;.method()V#a

Declaration of Ltest/C:\Test\src\Test~Test;.method()V#url

Calling the method "replace" for Ltest/C:\Test\src\Test~Test;.method()V#url

测试代码:

package test;

public class Test
{
    private int abc;
    public void method()
    {
        int a;
        String url = "ftp://fdh/sdcard/dfsgh";
        url.replace("'", ".");
    }
}

最后两个键值相等,说明我找到了合适的声明。而且很容易获取变量的类型,我不会post这个

根据this example, you need, (besides an AstParser and the CompilationUnit it creates), a ASTVisitor。然后你可以让它访问一个 VariableDeclarationFragment 用于对象的声明和一个 MethodInvocation 用于。嗯,方法调用。