为什么我在从astparser . createast()返回的CompilationUnit实例中得到NullPoin



我正在开发一个需要解析大量源文件的Eclipse JDT插件,所以我希望使用批处理方法astparser . createast()。解析执行时没有错误,但是在它生成的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中提交了一个bug,但是如果有人知道解决这个问题的方法——或者如果我错误地使用了API——我将非常感谢您的帮助。

拜伦

不知道你的异常是什么,我仍然可以提供2个建议:

  1. 看看org.eclipse.jdt.ui.SharedASTProvider。如果您没有对ast进行任何更改,则该类可以提供一种更健壮的方式来获取ast。
  2. 玩一些你正在使用的设置。您真的需要将bindingsRecovery设置为true吗?那么statementRecovery呢?

最新更新