Search code examples
javaeclipseparsingeclipse-jdt

Eclipse JDT AST parsing returns null when method has a generic type


When parsing a method of signature:

<T> T get(int index);

called with:

c.<Object>get(1);

The MethodInvocation.resolveMethodBinding() and resolveTypeBinding(), return null.

However, it returns a binding if called with

c.get(1);

I need to find a way to have the binding with all scnearios.

The complete case:

public class MyTest {

    @Test
    public void test() throws IOException {
        Path path = Paths.get("src/main/java/ab/MyClass.java");

        TypeDeclaration typeDeclaration = getTypeDeclaration(path);
        typeDeclaration.accept(new ASTVisitor() {

            @Override
            public boolean visit(MethodInvocation node) {
                System.out.println(node.resolveMethodBinding());
                System.out.println(node.resolveTypeBinding());
                return true;
            }
        });
    }

    public static TypeDeclaration getTypeDeclaration(Path path) throws IOException {
        ASTParser parser = ASTParser.newParser(AST.JLS9);
        String value = new String(Files.readAllBytes(path), UTF_8);
        parser.setResolveBindings(true);
        parser.setBindingsRecovery(true);
        parser.setSource(value.toCharArray());
        parser.setKind(ASTParser.K_COMPILATION_UNIT);

        parser.setCompilerOptions(JavaCore.getOptions());

        parser.setUnitName(path.getFileName().toString());

        String[] classpathEntries = {};
        String[] sourcepathEntries = { "C:\\test\\src\\main\\java" };

        parser.setEnvironment(classpathEntries, sourcepathEntries,
                new String[] { UTF_8.name() }, true);

        final CompilationUnit cu = (CompilationUnit) parser.createAST(null);

        return (TypeDeclaration) cu.types().get(0);
    }
}
public interface SomeInterface {

    <T> T get(int index);
}
public abstract class AbstractClass implements SomeInterface {

}
public class MyClass {

    AbstractClass c;

    public MyClass() {
        c.<Object>get(1);
    }
}

Similar questions (2017945, 2631981, 12755640) don't have a relevant answer.


Solution

  • Actually, it is because COMPILER_SOURCE needs to be specified:

    Hashtable<String, String> options = JavaCore.getOptions();
    options.put(JavaCore.COMPILER_SOURCE, "1.8");
    parser.setCompilerOptions(options);