我正在开发一个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中提交了一个bug,但是如果有人知道解决这个问题的方法--或者如果我错误地使用了API --我将非常感谢您的帮助。
拜伦
发布于 2011-10-15 00:39:56
在不知道你的例外情况的情况下,我仍然可以提供两个建议:
org.eclipse.jdt.ui.SharedASTProvider。如果您没有对AST进行任何更改,这个类可能会提供一种更强大的方法来获取AST。https://stackoverflow.com/questions/7603096
复制相似问题