VariableDeclarationFragment 节点resolveBindind() 在eclipse/jdt/ast 中返回null [英] VariableDeclarationFragment node resolveBindind() returns null in eclipse/jdt/ast
问题描述
我正在尝试按照 文档:
<块引用>绑定信息是从 Java 模型中获取的.这意味着编译单元必须相对于 Java 模型定位.当源代码来自 setSource(ICompilationUnit) 或 setSource(IClassFile) 时,这会自动发生.当由 setSource(char[]) 提供源时,必须通过使用 setProject(IJavaProject) 或 setEnvironment(String[], String[], String[], boolean) 和单位名称 setUnitName(String).请注意,影响文档注释检查的编译器选项也可能影响文档注释中的节点是否解析任何绑定.
这意味着你可以使用类似的东西(来自你的链接):
IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();IProject project = root.getProject("someJavaProject");project.open(null/* IProgressMonitor */);IJavaProject javaProject = JavaCore.create(project);
并添加 setProject
调用:
private static CompilationUnit createCompilationUnit(String sourceFile,IJavaProject javaProject) {字符串源 = readWithStringBuilder(sourceFile);ASTParser 解析器 = ASTParser.newParser(AST.JLS3);parser.setKind(ASTParser.K_COMPILATION_UNIT);parser.setSource(source.toCharArray());//设置源parser.setProject(javaProject);parser.setResolveBindings(true);//我们稍后需要绑定return (CompilationUnit) parser.createAST(null/* IProgressMonitor */);//解析}
<小时>
第二种方法(没有setProject
):
private static CompilationUnit createCompilationUnit(String sourceFile,字符串单位名称) {字符串源 = readWithStringBuilder(sourceFile);ASTParser 解析器 = ASTParser.newParser(AST.JLS3);parser.setKind(ASTParser.K_COMPILATION_UNIT);parser.setSource(source.toCharArray());//设置源String[] classpathEntries = ...;String[] sourcepathEntries = ...;parser.setEnvironment(classpathEntries, sourcepathEntries, null, true);parser.setUnitName(unitName);parser.setResolveBindings(true);//可选的//parser.setBindingsRecovery(true);return (CompilationUnit) parser.createAST(null/* IProgressMonitor */);//解析}
I'm trying to try out eclipse jdt/ast following this article.
This is the java code as an input:
class Hello
{
int hello()
{
int a = 0, b = 3;
/* hello */
{
b = a * 3;
}
return a;
}
public static void main(String[] args)
{
int z = 0, i = 3;
/* hello */
{
i = z * 3;
}
}
}
With ASTView, it shows that VariableDeclarationFragment
has corresponding binding.
However, in this visitor code for VariableDeclarationFragment node
, I always get null value for 4 local variables (a,b,z,i) as resolveBinding()
return value.
What's wrong with this? I use eclipse indigo.
This is the code to get the AST
private static CompilationUnit createCompilationUnit(String sourceFile) {
String source = readWithStringBuilder(sourceFile);
ASTParser parser = ASTParser.newParser(AST.JLS3);
parser.setKind(ASTParser.K_COMPILATION_UNIT);
parser.setSource(source.toCharArray()); // set source
parser.setResolveBindings(true); // we need bindings later on
return (CompilationUnit) parser.createAST(null /* IProgressMonitor */); // parse
}
This happens because of the following from the setResolveBindings docs:
Binding information is obtained from the Java model. This means that the compilation unit must be located relative to the Java model. This happens automatically when the source code comes from either setSource(ICompilationUnit) or setSource(IClassFile). When source is supplied by setSource(char[]), the location must be established explicitly by setting an environment using setProject(IJavaProject) or setEnvironment(String[], String[], String[], boolean) and a unit name setUnitName(String). Note that the compiler options that affect doc comment checking may also affect whether any bindings are resolved for nodes within doc comments.
That means you could use something like that (from your link):
IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
IProject project = root.getProject("someJavaProject");
project.open(null /* IProgressMonitor */);
IJavaProject javaProject = JavaCore.create(project);
and add the setProject
call:
private static CompilationUnit createCompilationUnit(String sourceFile,
IJavaProject javaProject) {
String source = readWithStringBuilder(sourceFile);
ASTParser parser = ASTParser.newParser(AST.JLS3);
parser.setKind(ASTParser.K_COMPILATION_UNIT);
parser.setSource(source.toCharArray()); // set source
parser.setProject(javaProject);
parser.setResolveBindings(true); // we need bindings later on
return (CompilationUnit) parser.createAST(null /* IProgressMonitor */); // parse
}
The second approach (without setProject
):
private static CompilationUnit createCompilationUnit(String sourceFile,
String unitName) {
String source = readWithStringBuilder(sourceFile);
ASTParser parser = ASTParser.newParser(AST.JLS3);
parser.setKind(ASTParser.K_COMPILATION_UNIT);
parser.setSource(source.toCharArray()); // set source
String[] classpathEntries = ...;
String[] sourcepathEntries = ...;
parser.setEnvironment(classpathEntries, sourcepathEntries, null, true);
parser.setUnitName(unitName);
parser.setResolveBindings(true);
// optional
// parser.setBindingsRecovery(true);
return (CompilationUnit) parser.createAST(null /* IProgressMonitor */); // parse
}
这篇关于VariableDeclarationFragment 节点resolveBindind() 在eclipse/jdt/ast 中返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!