gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 11:25:18 28 4
gpt4 key购买 nike

例如,我有这样的代码

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("'", ".");
}
}

最后两个键相等,这意味着我找到了适当的声明。而且获取变量类型很容易,我就不贴这个了

最佳答案

根据this example ,您需要(除了 AstParser 和它创建的 CompilationUnit 之外),一个 ASTVisitor 。然后您可以让它访问 VariableDeclarationFragment 来声明对象,并访问 MethodInitation 。好吧,方法调用。

关于java - JDT - AstParser - 获取为特定对象调用的方法列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30862438/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com