bindings not resolving with AST processing in eclipse

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)


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)


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.


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;