我正在使用 Java AST。如何获取 FieldDeclaration 或 VariableDeclaration 的类型(例如 String 或 MyOwnType)?在 ASTView 中,我可以在 SimpleName > 类型绑定(bind)下看到它,但使用 getters 我无法访问该成员。 我尝试了 FieldDeclaration to IField - Getting IBinding from FieldDeclaration 中的解决方案但在访问 FieldDeclaration 时,resolveBinding 返回 null。
Why does resolveBinding() return null even though I setResolveBindings(true) on my ASTParser?也不工作
最佳答案
哇,这太难了。
最后一行可以解析绑定(bind)并通过 varDeclFrag.resolveBinding().getType().getQualifiedName();
检索类型。尽管我已经认为在引用 sources
时我在 setEnvironment 中做了同样的事情:
String[] sources = { "C:\\a\\TheMightyExampleProject\\src" };
String[] classPaths = { "C:\\a\\antlr-4.1-complete.jar" };
parser.setEnvironment(classPaths, sources, new String[] { "UTF-8" }, true);
parser.setBindingsRecovery(true);
parser.setResolveBindings(true);
parser.setCompilerOptions(options);
parser.setStatementsRecovery(true);
parser.setUnitName("C:\\a\\TheMightyExampleProject\\src"); // ftw
您也可以查看Ida的回答bindings not resolving with AST processing in eclipse
关于java - 获取 AST 中 FieldDeclaration 的类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21981101/