使用类型绑定创建编译单元

发布于 2024-10-11 19:47:36 字数 958 浏览 2 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

少女情怀诗 2024-10-18 19:47:36

您混淆了 API 的两个部分:绑定解析和绑定恢复。来自 setBindingsRecovery 的 JavaDoc:

void org.eclipse.jdt.core.dom.ASTParser.setBindingsRecovery(boolean enabled)

Requests that the compiler should perform bindings recovery. When bindings recovery is enabled the compiler returns incomplete bindings.

Default to false.

This should be set to true only if bindings are resolved. It has no effect if there is no binding resolution.

Parameters:
enabled true if incomplete bindings are expected, and false if only complete bindings are expected.

所以,是的。预计绑定恢复设置为 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:

void org.eclipse.jdt.core.dom.ASTParser.setBindingsRecovery(boolean enabled)

Requests that the compiler should perform bindings recovery. When bindings recovery is enabled the compiler returns incomplete bindings.

Default to false.

This should be set to true only if bindings are resolved. It has no effect if there is no binding resolution.

Parameters:
enabled true if incomplete bindings are expected, and false if only complete bindings are expected.

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 method AST.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.

情定在深秋 2024-10-18 19:47:36

好吧,看来 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 that arg 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 of arg = ASTNode.copySubtree(classAst, arg2);.
So instead, I called resolveTypeBinding().getName() on arg2 rather than on arg, which gave me the wanted result.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文