使用类型绑定创建编译单元
我正在使用 AST API,我正在尝试创建一个编译具有类型绑定的单元。我编写了以下代码:
private static CompilationUnit parse(ICompilationUnit unit) {
ASTParser parser = ASTParser.newParser(AST.JLS3);
parser.setKind(ASTParser.K_COMPILATION_UNIT);
parser.setSource(unit);
parser.setResolveBindings(true);
CompilationUnit compiUnit = (CompilationUnit) parser.createAST(null);
return compiUnit;
}
不幸的是,当我在调试模式下运行此代码并检查 compiUnit
时,我发现 compiUnit.ast.resolver.isRecoveringBindings
为 false。
谁能想到为什么它不会像我指定的那样true
的原因吗?
谢谢
I am working with the AST API in java, and I am trying to create a Compilation Unit with type bindings. I wrote the following code:
private static CompilationUnit parse(ICompilationUnit unit) {
ASTParser parser = ASTParser.newParser(AST.JLS3);
parser.setKind(ASTParser.K_COMPILATION_UNIT);
parser.setSource(unit);
parser.setResolveBindings(true);
CompilationUnit compiUnit = (CompilationUnit) parser.createAST(null);
return compiUnit;
}
Unfortunately, when I run this code in debug mode and inspect compiUnit
I find that compiUnit.ast.resolver.isRecoveringBindings
is false.
Can anyone think of a reason why it wouldn't be true
, as I specified it to be?
Thank you
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您混淆了 API 的两个部分:绑定解析和绑定恢复。来自
setBindingsRecovery
的 JavaDoc:所以,是的。预计绑定恢复设置为 false,因为这是默认设置。但是,由于您显式设置了要解析的绑定,因此应在 AST 对象中将其设置为 true。您应该检查方法
AST.hasBindingsResolved()
以查看是否可以获得绑定。需要明确的是:绑定解析是为了让解析器/编译器在创建 AST 时计算类型绑定,而绑定恢复是为了使绑定能够部分计算。老实说,我不太确定绑定恢复有什么帮助,但我相当确定它不是您需要的东西。
You are mixing up two pieces of the API: binding resolving and binding recovery. From the JavaDoc for
setBindingsRecovery
:So, yes. It is expected that bindings recovery is set to false because that is the default. However, since you explicitly set bindings to be resolved, that should be set to true in the
AST
object. You should check the methodAST.hasBindingsResolved()
to see if you can get your bindings.To be clear: bindings resolution is about getting the parser/compiler to calculate type bindings as the AST is being created, while bindings recovery is about enabling bindings to be partially computed. Honestly, I'm not exactly sure what bindings recovery helps with, but I am fairly certain that it is not something that you need.
好吧,看来 compiUnit.ast.resolver.isRecoveringBindings 并不意味着它看起来的意思,或者它根本就是不正确的(java AST API 不是最稳定的 API...) 。
不管怎样,后来与编译单元的合作表明,它确实会在需要时解析类型绑定。
引发我问题的最初问题是我在尝试获取 arg.resolveTypeBinding().getName(); 时遇到 NullPointerException。这确实意味着
arg
没有绑定。然而,原因是它不是java环境创建的原始AST的一部分,而是arg = ASTNode.copySubtree(classAst, arg2);
的结果。因此,我在
arg2
而不是arg
上调用了resolveTypeBinding().getName()
,这给了我想要的结果。Ok, it seems that either
compiUnit.ast.resolver.isRecoveringBindings
dosen't mean what it seems to mean, or it is simply incorrect (The java AST API is not the most stable one...).Either way, working with the Compilation Unit later on revealed that it does resolve type binding when asked to.
The original problem which raised my question was that I got a NullPointerException when trying to fetch
arg.resolveTypeBinding().getName();
. This does mean thatarg
doesn't have bindings. However, The reason for that was that it was not part of the original AST created by the java environment, but the result ofarg = ASTNode.copySubtree(classAst, arg2);
.So instead, I called
resolveTypeBinding().getName()
onarg2
rather than onarg
, which gave me the wanted result.