Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Language IDEs » Java Development Tools (JDT) » Trying to convert from IMethod to MethodDeclaration: findDeclaringNode always returns null
Trying to convert from IMethod to MethodDeclaration: findDeclaringNode always returns null [message #917072] Wed, 19 September 2012 15:50 Go to previous message
Eiji Adachi is currently offline 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 15:51]

Report message to a moderator

 
Read Message
Read Message
Read Message
Read Message
Previous Topic:replcae default jre
Next Topic:Plugin Development getContent from editor
Goto Forum:
  


Current Time: Sun May 19 13:08:12 EDT 2013

Powered by FUDForum. Page generated in 0.01743 seconds