bindings not resolving with AST processing in eclipse

折月煮酒 提交于 2019-11-27 09:18:06

When you use: parser.setSource(source); What is the type of param "source"?

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 extablished explicitly by calling setProject(IJavaProject) and setUnitName(String).

This is from http://help.eclipse.org/galileo/index.jsp?topic=/org.eclipse.jdt.doc.isv/reference/api/org/eclipse/jdt/core/dom/ASTParser.html I think maybe you just use setSource(char[]) without calling setProject(IJavaProject) and setUnitName(String)

sateesh

The probable reason is that you should not be calling the method in Visitor instance directly. You should do something like:

unit.accept(visitor);

The parent class of CompilationUnit, ASTNode, has an accept method which takes a visitor that is of type ASTVisitor.

The Visitor you have written, GenericVisitor, does subclasses the abstarct class ASTVisitor and overrides implementation for the node types you are intersted in. So I think changing your code to do the invocation in above form would fix your problem.

ASTParser is just the parser: It builds an AST which is the first step in compilation. The actual compiler is doing more than that: it runs various visitor which enhance the tree with additional information. One of them is the binding resolution visitor.

In particular, take a look at the body of the public void process(CompilationUnitDeclaration unit, int i) method in the org.eclipse.jdt.internal.compiler.Compiler class.

Okay, this is my first answer on Stack Overflow. Nervous...

I got the same problem with you, and since you have done this:

parser.setResolveBindings(true);

Let's see whether it worked by checking this:

if (unit.getAST().hasResolvedBindings()) {
    System.out.println("Binding activated.");
}
else {
    Ststem.out.println("Binding is not activated.");
}

And I think the result is "Binding is not activated.". And that's why you got the null pointer all the time.

Then, I add this statement to my code:

parser.setEnvironment(null, null, null, true);

Magically, the problem is fixed!!! And I suppose you can try this too.

The problem is that your parser has not been provided with the necessary information to construct its Java Model which is required for resolving bindings.

If the parser's source is attained from setSource(ICompilationUnit) or setSource(IClassFile) this information is provided to the parser automatically.

However, if you use setSource(char[]) instead, you must provide this context for the parser. This can be done by either calling parser.setProject(IJavaProject) or setEnvironment(String[], String[], String[], boolean) and setUnitName(String)

Source: http://help.eclipse.org/kepler/topic/org.eclipse.jdt.doc.isv/reference/api/org/eclipse/jdt/core/dom/ASTParser.html#setResolveBindings(boolean)

If your bindings are null, I am not entirely sure that your problem is covered by the other explanations. In our code base, we do the following, and the bindings are always there:

public static ASTNode getASTNodeFromCompilationUnit(ICompilationUnit compUnit) {
    ASTParser parser = ASTParser.newParser(AST.JLS3);
    parser.setResolveBindings(true);
    parser.setSource(compUnit);
    return parser.createAST(/* passing in monitor messes up previous monitor state */ null);
}

So the only differences I can see are the order of the calls to resolve bindings, and the fact that we don't call setKind on the parser. Any chance you could try it with exactly this code and see what happens?

Sometimes, if you got errors in the referenced source files, then the bindings to these types are not resolved. For instance, make sure that you use the correct encoding and Java version of the source.

    ASTParser parser = ASTParser.newParser(AST.JLS3);
    parser.setKind(ASTParser.K_COMPILATION_UNIT);
    parser.setResolveBindings(true);
    parser.setBindingsRecovery(true);
    Hashtable<String, String> options = JavaCore.getDefaultOptions();
    options.put(JavaCore.COMPILER_SOURCE, JavaCore.VERSION_1_6);
    parser.setCompilerOptions(options);
    parser.setEnvironment(classpath, sources, new String[] { "UTF-8", "UTF-8" }, true);
    parser.setSource(fileContents.toCharArray());
    CompilationUnit compilationUnit = (CompilationUnit) parser.createAST(null);
    IProblem[] problems = compilationUnit.getProblems();
    if (problems != null && problems.length > 0) {
        logger.warn("Got {} problems compiling the source file: ", problems.length);
        for (IProblem problem : problems) {
            logger.warn("{}", problem);
        }
    }
    return compilationUnit;
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!