为什么我在从 ASTParser.createASTs() 返回的 CompilationUnit 实例中收到 NullPointerException
我正在开发一个 Eclipse JDT 插件,需要解析大量源文件, 所以我希望使用批处理方法 ASTParser.createASTs()。解析执行时没有错误,但在它生成的 CompilationUnit 实例中,许多 org.eclipse.jdt.internal.compiler.lookup.SourceTypeBinding 实例都有其 scope
字段设置为null
。此设置为 null 发生在 CompilationUnitDeclaration.cleanUp()
方法中,该方法在与我的插件代码无关的工作线程上调用(即,我的插件的类不会出现在 cleanUp()
方法调用堆栈)。
我的解析代码如下所示(所有 rawSources
都在同一个项目中):
ASTParser parser = ASTParser.newParser(AST.JLS3);
parser.setResolveBindings(true);
parser.setStatementsRecovery(true);
parser.setBindingsRecovery(true);
parser.setIgnoreMethodBodies(false);
parser.setProject(project);
parser.createASTs(rawSources.values().toArray(new ICompilationUnit[0]), new String[0], this, deltaAnalyzer.progressMonitor);
或者,我可以通过这种方式执行解析,并且不会出现此类问题:
for (ICompilationUnit source : rawSources.values())
{
parser.setResolveBindings(true);
parser.setStatementsRecovery(true);
parser.setBindingsRecovery(true);
parser.setIgnoreMethodBodies(false);
parser.setProject(project);
parser.setSource(source);
CompilationUnit ast = (CompilationUnit)parser.createAST(deltaAnalyzer.progressMonitor);
parsedSources.add(deltaAnalyzer.createParsedSource(source, ast));
}
这个问题在 Helios 和 Indigo 中都出现(最最新版本)。我在 Eclipse Bugzilla 中提交了一个错误,但如果有人知道解决此问题的方法——或者如果我错误地使用了 API——我将非常感谢您的帮助。
拜伦
I am working on an Eclipse JDT plugin that requires parsing large numbers of source files,
so I am hoping to use the batch method ASTParser.createASTs(). The parsing executes without errors, but within the CompilationUnit instances it produces, many of the org.eclipse.jdt.internal.compiler.lookup.SourceTypeBinding
instances have had their scope
field set to null
. This setting to null is occurring in the CompilationUnitDeclaration.cleanUp()
methods, which are invoked on a worker thread that is unrelated to my plugin's code (i.e., my plugin's classes do not appear on the cleanUp()
method call stack).
My parsing code looks like this (all rawSources
are within the same project):
ASTParser parser = ASTParser.newParser(AST.JLS3);
parser.setResolveBindings(true);
parser.setStatementsRecovery(true);
parser.setBindingsRecovery(true);
parser.setIgnoreMethodBodies(false);
parser.setProject(project);
parser.createASTs(rawSources.values().toArray(new ICompilationUnit[0]), new String[0], this, deltaAnalyzer.progressMonitor);
Alternatively, I can execute the parsing this way, and no such problems occur:
for (ICompilationUnit source : rawSources.values())
{
parser.setResolveBindings(true);
parser.setStatementsRecovery(true);
parser.setBindingsRecovery(true);
parser.setIgnoreMethodBodies(false);
parser.setProject(project);
parser.setSource(source);
CompilationUnit ast = (CompilationUnit)parser.createAST(deltaAnalyzer.progressMonitor);
parsedSources.add(deltaAnalyzer.createParsedSource(source, ast));
}
This issue occurs in both Helios and Indigo (the very latest release build). I filed a bug in Eclipse Bugzilla, but if anyone knows of a way to work around this--or if I am using the API wrong--I would greatly appreciate your help.
Byron
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在不确切知道您的异常是什么的情况下,我仍然可以提供 2 个建议:
Without knowing exactly what your exception is, I can still offer 2 suggestions:
org.eclipse.jdt.ui.SharedASTProvider
. If you are not making any changes to ASTs, this class may provide a more robust way of getting the ASTs.