I am writing an Eclipse plug-in that uses JDT AST's ASTParser
to parse a method. I am looking within that method for the creation of a particular type of object.
When I find a ClassInstanceCreation
, I call getType()
on it to see what type is being instantiated. I want to be sure that the fully-resolved type being dealt with there is the one I think it is, so I tell the resultant Type
object to resolveBinding()
. I get null
back even though there are no compilation errors and even though I called setResolveBindings(true)
on my ASTParser
. I gave my ASTParser
(via setSource()
) the ICompilationUnit
that contains my method, so the parser has access to the entire workspace context.
final IMethod method = ...;
final ASTParser parser = ASTParser.newParser(AST.JLS3);
parser.setResolveBindings(true);
parser.setSource(method.getCompilationUnit());
parser.setSourceRange(method.getSourceRange().getOffset(), method.getSourceRange().getLength());
parser.setKind(ASTParser.K_CLASS_BODY_DECLARATIONS);
final TypeDeclaration astRoot = (TypeDeclaration) parser.createAST(null);
final ClassInstanceCreation classInstanceCreation = walkAstAndFindMyExpression(astRoot);
final Type instantiatedType = classInstanceCreation.getType();
System.out.println("BINDING: " + instantiatedType.resolveBinding());
Why does resolveBinding()
return null
? How can I get the binding information?