Trying to convert from IMethod to MethodDeclaration: findDeclaringNode always returns null [message #917072] |
Wed, 19 September 2012 15:50  |
Eclipse User |
|
|
|
I read this article from Eclipse wiki (http://wiki.eclipse.org/JDT/FAQ#From_an_IJavaElement_to_its_declaring_ASTNode) but I still can not convert from a IMethod to its corresponding MethodDeclaration.
I have an extension point which adds a popup menu to IMethod objects. Possessing this IMethod object, I want to visit it with an ASTVisitor.
Here is how I'm trying to convert from IMethod to MethodDeclaration
public static MethodDeclaration convertToAstNode(final IMethod method) throws JavaModelException
{
final ICompilationUnit compilationUnit = method.getCompilationUnit();
final ASTParser astParser = ASTParser.newParser( AST.JLS4 );
astParser.setSource( compilationUnit );
astParser.setKind( ASTParser.K_COMPILATION_UNIT );
astParser.setResolveBindings( true );
astParser.setBindingsRecovery( true );
final ASTNode rootNode = astParser.createAST( null );
final CompilationUnit compilationUnitNode = (CompilationUnit) rootNode;
final String key = method.getKey();
final ASTNode javaElement = compilationUnitNode.findDeclaringNode( key );
final MethodDeclaration methodDeclarationNode = (MethodDeclaration) javaElement;
return methodDeclarationNode;
}
compilationUnitNode.findDeclaringNode always returns null. Am I missing something? What is it?
[Updated on: Wed, 19 September 2012 15:51] by Moderator
|
|
|
|
|
Re: Trying to convert from IMethod to MethodDeclaration: findDeclaringNode always returns null [message #920828 is a reply to message #918907] |
Sun, 23 September 2012 09:53  |
Eclipse User |
|
|
|
Your code looks fine at a first glance, however, you might get caught by this little Caveat in IMethod.getKey():
Quote:If the given method is not resolved, the returned key is simply the java element's key.
You may want to check IMethod.isResolved() on your method handle. If that says 'false' this might be a dead end, but you could probably still improve your workaround by using
NodeFinder.perform(rootNode, method.getSourceRange());
HTH,
Stephan
|
|
|
Powered by
FUDForum. Page generated in 0.03776 seconds