VariableDeclarationFragment 节点resolveBindind() 在eclipse/jdt/ast 中返回null

pro*_*eek 4 java eclipse abstract-syntax-tree eclipse-jdt

我正在尝试按照这篇文章尝试 eclipse jdt/ast 。

这是作为输入的java代码:

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;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

用ASTView,显示VariableDeclarationFragment有对应的绑定。 在此处输入图片说明

然而,在这个访问者代码中VariableDeclarationFragment node,我总是得到 4 个局部变量 (a,b,z,i) 的空值作为resolveBinding()返回值。

这有什么问题?我使用日食靛蓝。

在此处输入图片说明

这是获取 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
}
Run Code Online (Sandbox Code Playgroud)

sev*_*rce 5

发生这种情况是因为setResolveBindings文档中的以下内容:

绑定信息是从 Java 模型中获得的。这意味着编译单元必须相对于 Java 模型定位。当源代码来自 setSource(ICompilationUnit) 或 setSource(IClassFile) 时,这会自动发生。当源由 setSource(char[]) 提供时,必须通过使用 setProject(IJavaProject)setEnvironment(String[], String[], String[], boolean) 和单元名称 setUnitName(字符串)。请注意,影响文档注释检查的编译器选项也可能影响文档注释中的节点是否解析任何绑定。

这意味着您可以使用类似的东西(来自您的链接):

IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
IProject project = root.getProject("someJavaProject");
project.open(null /* IProgressMonitor */);

IJavaProject javaProject = JavaCore.create(project);
Run Code Online (Sandbox Code Playgroud)

并添加setProject电话:

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
}
Run Code Online (Sandbox Code Playgroud)

第二种方法(没有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
}
Run Code Online (Sandbox Code Playgroud)