[英]Why am I getting NullPointerException in the CompilationUnit instances returned from ASTParser.createASTs()
我正在開發一個需要解析大量源文件的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錯誤-我將非常感謝您的幫助。
拜倫
在不確切知道您的例外情況的情況下,我仍然可以提供2條建議:
org.eclipse.jdt.ui.SharedASTProvider
。 如果您不對AST進行任何更改,則此類可能提供一種更強大的獲取AST的方法。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.