How to access MethodInvocation that is inside a VariableDeclarationStatement

868 views Asked by At

I am checking for the invocation of a method using JDT and performing a check on its argument. I am using an AST Visitor class which visits MethodInvocation nodes and performs this operation. I use the below method in the Visitor class.

    public boolean visit(MethodInvocation node) {

        if (node.getName().toString().equals("createQuery")) {

            String argument= node.arguments().get(0).toString();

            // process the argument here

        }

        return true;
    }

But the invocation which are part of a Variable Declaration are not being visted.

eg: If i am looking for the invocation of a method 'createQuery', the below invocation would be visited.

    session.createQuery("some query here");

Bu this one is not visited.

    Query query = session.createQuery("another query here");

How can i visit such statements and fetch the arguments in a proper way?

Please help.

1

There are 1 answers

0
Unni Kris On BEST ANSWER

Found it.. Have to visit the VariableDeclarationStatement node in the AST and return a true.

public boolean visit(VariableDeclarationStatement node) {

    return true;
}

This enables the visit to the childs of the VariableDeclarationStatement in the AST. The particular type of Method invocation comes as a child of the VariableDeclarationStatement node, thus they are also visited.