gpt4 book ai didi

java - 在eclipse中的方法中获取字段类型

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:03:14 25 4
gpt4 key购买 nike

如何以编程方式从方法中的语句中获取字段类型,如下所示:

Foo foo = getSomeFoo();

如果是字段,我可以知道元素的类型。

最佳答案

你需要使用Eclipse的AST

ICompilationUnit icu = ...

ASTParser parser = ASTParser.newParser(AST.JLS3);
parser.setResolveBindings(true);
parser.setSource(icu);
CompilationUnit cu = (CompilationUnit) parser.createAST(null);
cu.accept(new ASTVisitor() {
@Override
public boolean visit(VariableDeclarationStatement node) {
System.out.println("node=" + node);
System.out.println("node.getType()=" + node.getType());
return true;
}
});

关于java - 在eclipse中的方法中获取字段类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3938528/

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