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

时间:2015-06-16 08:30:26

标签: java eclipse-jdt

例如,我有像

这样的代码
SomeObject1 obj1 = new SomeObject1();
SomeObject2 obj2 = new SomeObject2();
...
obj1.foo();
obj1.boo();
...
obj2.foo2();
obj2.boo2();

我想得到下一个输出:

  

输入:SomeObject1

     

姓名:obj1

     

被叫方法:foo,boo

     

==========

     

输入:SomeObject2

     

姓名:obj2

     

被叫方法:foo2,boo2

感谢

UPD: 我已经制作了一个代码

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;
}
  

Ltest / C声明:\ Test \ src \ Test~Test; .abc)I

     

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

     

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

     

调用方法"替换" 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("'", ".");
    }
}

最后两个键是相同的,这意味着我找到了适当的声明。并且很容易获得变量类型,我不会发布这个

1 个答案:

答案 0 :(得分:1)

根据this example,您需要(除了AstParserCompilationUnit之外),ASTVisitor。然后你可以让它访问VariableDeclarationFragment以获取对象的声明,并访问MethodInvocation。好吧,方法调用。

相关问题