JDT AstParser - How to get Assignment (parent) from MethodInvocation

189 views Asked by At

I'm trying to get Assignment which is parent for MethodInvocation Test code:

package test;

public class Test
{
    public void method()
    {
        String a = "123".substring(1);
    }
}

My code:

public static Assignment getAssignmentBlock(MethodInvocation inv)
{
    ASTNode node = inv.getParent();
    while(node != null)
    {
        if(node instanceof Assignment)
            return (Assignment) node;
        node = node.getParent();
    }
    return null;
}

and visitor

cu.accept(new ASTVisitor()
{
    public boolean visit(MethodInvocation inv)
    {               
        System.out.println(inv.getName().getFullyQualifiedName());
        Assignment a = MethodCall.getAssignmentBlock(inv);
        System.out.println(a != null);
        return true;
    }
});

but all the time I see false meaning the Assignment is null What's the problem? Or if this is a wrong way, please advise the right way. Thanks

0

There are 0 answers