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?
Tucked away at the bottom of the overview of ASTParser.setKind()
, carefully hidden from people troubleshooting resolveBinding()
and setResolveBindings()
, is the statement
Binding information is only computed when
kind
isK_COMPILATION_UNIT
.
(from the online Javadoc)
I don't understand offhand why this would be the case, but it does seem to point pretty clearly at what needs to be different!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With