我创建了一个启用了绑定的 AST,当我稍后解析绑定时,我得到了一个有效的 ITypeBinding。
但是,当我想要获取绑定的声明 Node 时,它总是返回 null(除非 ITypeBinding 在 sourceFile 中声明)。
这是我的 AST 生成代码:
public void parseFunction(IFile sourceFile) {
ICompilationUnit element = JavaCore.createCompilationUnitFrom(sourceFile);
ASTParser parser = ASTParser.newParser(AST.JLS14);
parser.setResolveBindings(true);
parser.setKind(ASTParser.K_COMPILATION_UNIT);
parser.setBindingsRecovery(true);
parser.setSource(element);
CompilationUnit cu = (CompilationUnit) parser.createAST(null);
cu.accept(new ASTVisitor() {
public boolean visit(FieldDeclaration variable) {
ITypeBinding binding = variable.getType().resolveBinding();
//binding is not null here
ASTNode declartionNode = cu.findDeclaringNode(binding);
//declartionNode is null here
return false;
}
});
return;
}
任何帮助表示赞赏。
parser.setEnvironment(classpath, sources, encodings, true);
不见了。
否则,无法解析对不在已解析代码中但在类路径上某处的声明的绑定。
See ASTParser::setEnvironment https://help.eclipse.org/latest/topic/org.eclipse.jdt.doc.isv/reference/api/org/eclipse/jdt/core/dom/ASTParser.html#setEnvironment(java.lang.String%5B%5D,java.lang.String%5B%5D,java.lang.String%5B%5D,boolean)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)