VariableDeclarationFragment节点resolveBindind()在eclipse / jdt / ast中返回null [英] VariableDeclarationFragment node resolveBindind() returns null in eclipse/jdt/ast

查看:228
本文介绍了VariableDeclarationFragment节点resolveBindind()在eclipse / jdt / ast中返回null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图尝试eclipse jdt / ast跟随 docs:


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


这意味着你可以使用这样的东西(从你的链接):

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

IJavaProject javaProject = JavaCore.create(project);

并添加 setProject call: p>

  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); //我们以后需要绑定
return(CompilationUnit)parser.createAST(null / * IProgressMonitor * /); // parse
}






第二个方法(没有 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);
//可选
// parser.setBindingsRecovery(true);
return(CompilationUnit)parser.createAST(null / * IProgressMonitor * /); // parse
}


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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆