Parthi P
Parthi P

Reputation: 65

Find MethodInvocation method bindings in JDT ASTVisitor

I have a java file which uses java.sql.Statement.execute as below.

public class Dummy
{
    public void execute(String q) throws SQLException
    {
        ...
        Statement stmt = conn.createStatement();
        ...
        stmt.execute(q);
        ...
    }
}

My use case is I want to identify what are all the classes and their method names which use "Statement.execute(String)" using JDT ASTVisitor. Is this possible?

I found below entry using eclipse ASTView plugin.

method binding: Statement.execute(String)

How can I get this method binding value in my ASTVisitor.

I tried this.

@Override
public boolean visit(MethodInvocation node)
{
    IMethodBinding iMethod = (IMethodBinding) node.resolveMethodBinding();
    if(iMethod != null)
    System.out.println("Binding "+iMethod.getName());
    return super.visit(node);
}

but node.resolveMethodBinding() always returns null.

Upvotes: 0

Views: 728

Answers (1)

Stephan Herrmann
Stephan Herrmann

Reputation: 8178

... i want to identify what are all the classes and its method names which using "Statement.execute(String)"

This sounds like a job for the org.eclipse.jdt.core.search.SearchEngine, which will produce the results much faster than traversing all your source files using a visitor.

... node.resolveMethodBinding() always returns null

This depends on how you obtained the AST. See, e.g., org.eclipse.jdt.core.dom.ASTParser.setResolveBindings(boolean)

Upvotes: 1

Related Questions