为什么Eclipse-JDT无法从java项目中获取绑定信息?

时间:2017-11-14 03:36:11

标签: java eclipse eclipse-jdt

首先,这不是Eclipse plugin项目。我的testing文件夹中有三个文件。

//A
import dd.B;
public class A {
    public void run() {
        B b = new B();
    }
}
//B
package dd;
public class B {
    String name;
    int id;
    private C c = new C();
}
//C
package dd;
public class C {
    int x;
    int y;
}

然后我使用ASTParser来获取A.java的AST。

private static final String path = "F:/testing/A.java";
    public static void main(String[] args) throws IOException {
        ASTParser parser = ASTParser.newParser(AST.JLS8);
        String content = FileUtils.readFileToString(new File(path));
        parser.setSource(content.toCharArray());
        parser.setKind(ASTParser.K_COMPILATION_UNIT);
        parser.setUnitName("A");
        String[] sourcepathEntries = {"F:/testing"};
        String[] encodings = {"UTF-8"};
        String[] classPath = {"C:/Program Files/Java/jre1.8.0_151/lib"};
        parser.setEnvironment(classPath, sourcepathEntries, encodings, true);
        parser.setResolveBindings(true);
        parser.setBindingsRecovery(true);
        CompilationUnit cu = (CompilationUnit)parser.createAST(null);
        DemoVisitor dv = new DemoVisitor();
        cu.accept(dv);
    }

这是我的DemoVisitor

public boolean visit(ClassInstanceCreation node) {
    ITypeBinding tb = node.resolveTypeBinding();
    System.out.println(tb);
    return super.visit(node);
}

我的控制台输出是

public class dd.B
    extends java.lang.Object
/*   fields   */
[unresolved] <no type> name
[unresolved] <no type> id
private [unresolved] <no type> c
/*   methods   */
public void <init>() 

虽然我之前设置了unresolved环境,但是B类中的字段是parser。因此可以获得java项目的完整绑定映射信息。

0 个答案:

没有答案