Search code examples
javaeclipseeclipse-jdt

How to get the enclosing method node with JDT?


When I have a method foo() that calls bar(), how can I get the foo() AST node from MethodInvocation node (or whatever statements/expressions in the method)? For example, I need to know the IMethod foo from b.bar().

public void foo()
{
    b.bar();
}

Solution

  • In JDT/UI we have a helper method to do this. Take a look at org.eclipse.jdt.internal.corext.dom.ASTNodes.getParent(ASTNode, int)