Trying to convert from IMethod to MethodDeclaration: findDeclaringNode always returns null [message #917072] |
Wed, 19 September 2012 19:50 |
Eiji Adachi Messages: 8 Registered: November 2011 |
Junior Member |
|
|
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 19:51] Report message to a moderator
|
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.03340 seconds