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 19:50 Go to next message
Eiji Adachi is currently offline Eiji AdachiFriend
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

Re: Trying to convert from IMethod to MethodDeclaration: findDeclaringNode always returns null [message #918906 is a reply to message #917072] Fri, 21 September 2012 13:18 Go to previous messageGo to next message
Eiji Adachi is currently offline Eiji AdachiFriend
Messages: 8
Registered: November 2011
Junior Member
I tried this terrible workaround and it worked:

public static MethodDeclaration astNode(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 String unitSource = compilationUnit.getSource();
		final String methodSource = method.getSource();

		final int indexOf = unitSource.indexOf( methodSource );

		final int length = methodSource.length();
		final ASTNode currentNode = NodeFinder.perform( rootNode, indexOf, length );

		final MethodDeclaration methodDeclarationParent = getMethodDeclarationParent( currentNode );

		return methodDeclarationParent;
	}


But I still have no clue why findDeclaringNode( key ) only returns null
Re: Trying to convert from IMethod to MethodDeclaration: findDeclaringNode always returns null [message #918907 is a reply to message #918906] Fri, 21 September 2012 13:19 Go to previous messageGo to next message
Eiji Adachi is currently offline Eiji AdachiFriend
Messages: 8
Registered: November 2011
Junior Member
BTW, I'm using JDT-Core 3.8.1
Re: Trying to convert from IMethod to MethodDeclaration: findDeclaringNode always returns null [message #920828 is a reply to message #918907] Sun, 23 September 2012 13:53 Go to previous message
Stephan Herrmann is currently offline Stephan HerrmannFriend
Messages: 1036
Registered: July 2009
Senior Member
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
Previous Topic:replcae default jre
Next Topic:Plugin Development getContent from editor
Goto Forum:
  


Current Time: Sat Nov 29 08:49:49 GMT 2014

Powered by FUDForum. Page generated in 0.01951 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software