JDT resolveBinding not returning null even when environment is set

27 Views Asked by At

I'm using JDT to resolve java projects.

//in function genASTPerser(List<String> paths)
ASTParser parser = ASTParser.newParser(AST.JLS8);  // Change the JLS version if needed
parser.setResolveBindings(true);  // Resolve bindings for better semantic analysis
parser.setKind(ASTParser.K_COMPILATION_UNIT);  // Parse as compilation unit
String[] sourceFiles = paths.toArray(new String[paths.size()]);  // Add all file paths
parser.setEnvironment(null, sourceFiles, null, true);  // Set the source files and their encodings

Then I start to anaylze a set of files in the same project.

        var astParser = Utils.genASTParser(this.filePaths);
        for(int i = 0; i < filePaths.size(); i++){
            
            String path = this.filePaths.get(i);
            File f = this.recordJavaFile.get(i);

            System.out.println(path);

            String str = Utils.readFieToString(path);
            astParser.setSource(str.toCharArray());
            CompilationUnit compilationUnit = (CompilationUnit) astParser.createAST(null);

Then I use a visitor to anaylze method invocations. I wrote a test program as follows:

//in package p.p1
package p.p1;

public class A1 {
    /**
     * f1
     */
    public static void f1(){

    }

    /**
     * put
     */
    public void put(){

    }
}

//in package p.p1.p11
package p.p1.p11;

import p.p1.A1;

public class A2 {
    /**
     * f2
     */
    public void f2(){
        A1.f1();
    }
}

When resolving file A2.java. What I expect is that when I call resolveBinding of A1.f1(), it should return the binding of A1.f1. However, I get null instead.

0

There are 0 best solutions below