Take the tour ×

I have to find all the methods in a class that use a particular member variable. (like "References" in eclipse but I want to implement using code...)I use AST visitor pattern that visits FieldDeclaration to get the name and type of all the member variables. I also use visitor pattern that visits MethodDeclaration nodes to get the content of each method using getBody(). Now I have the field variable name, type and member method details. I thought I can use a string search on the content of each member method but for a variable name "a", search may return true for keywords like "class" and others too!!! Is there a way to find the usage of a particular variable corresponding to a fieldDeclaration?? (like Binding or something??) If so, what is the AST Node or Class?

Here's the code I used...

 SimpleNameVisitor simpleNameVisitor=new SimpleNameVisitor();
 //SimpleNameVisitor is the visitor pattern for SimpleName
 //mthd is the object that stores method details
 for(SimpleName simpName:simpleNameVisitor.getIdentifiers()){
        //varName is the field variable name
        System.out.println("MethodName: "+mthd.getName());

Here's the code that solved the problem(suggested by wjans;changed equals to contentEquals)

VariableDeclarationFragment fragment = ... ;
IBinding binding = fragment.getName().resolveBinding();

public boolean visitNode(SimpleName simpleName) throws Exception {
if (binding.toString().contentEquals(simpleName.resolveBinding().toString()) {


share|improve this question
So you're running within Eclipse, as a plugin? It would seem that the simplest solution is to look at the existing Eclipse code and mimic it. If that's not what you're doing, perhaps you should paste some code so that people will understand your question. –  Anon May 16 '11 at 12:07
add comment

2 Answers

up vote 2 down vote accepted

You can do something like this:

Keep a reference to the binding of your FieldDeclaration,

VariableDeclarationFragment fragment = ... ;
IBinding binding = fragment.getName().resolveBinding();

and use this to compare it with the bindings when visiting SimpleName's inside your MethodDeclaration

public boolean visitNode(SimpleName simpleName) throws Exception {
    if (binding.equals(simpleName.resolveBinding()) {
share|improve this answer
Thank you for your reference to SimpleName. Since the binding is in the class I can't find out the method that uses the SimpleName. So I used getFullyQualifiedName() of the SimpleName to compare it with the field Variable Name of FieldDeclaration. –  Niranjani S May 16 '11 at 16:29
However, I find that if there are two SimpleNames with the same name one appearing as a variable and other appearing as a method, both are counted as appearing in the method. I think binding will resolve the conflict, but how to find the method that contains the reference? –  Niranjani S May 16 '11 at 17:32
You can just compare the bindings of the SimpleName's, they won't match in case it's a method with the same name as your field. To know in which method you are, you should take advantage of the AST visiting. Just keep/delete a reference to the method in visitNode/endVisitNode of your MethodDeclaration. –  wjans May 17 '11 at 4:58
thank you for pointing out that this can be resloved using binding itself... –  Niranjani S May 18 '11 at 4:59
add comment

To implement something like Find References in code, use the JDT SearchEngine.

        SearchRequestor findMethod = new SearchRequestor() {
            public void acceptSearchMatch(SearchMatch match) throws CoreException {
                // analyze search match.
        SearchEngine engine = new SearchEngine();
        IJavaSearchScope workspaceScope = SearchEngine
        SearchPattern pattern = SearchPattern
        SearchParticipant[] participant = new SearchParticipant[] { SearchEngine
                .getDefaultSearchParticipant() };
        engine.search(pattern, participant, workspaceScope, findMethod,
                new NullProgressMonitor());

See http://help.eclipse.org/helios/index.jsp?topic=/org.eclipse.jdt.doc.isv/guide/jdt_int_core.htm for more details on java search, the java model, and the AST.

See https://bugs.eclipse.org/bugs/attachment.cgi?id=192205 for an example that uses search and then looks at the results by generating an AST.

share|improve this answer
Thanks for ur code...However, I would like to implement it using AST –  Niranjani S May 16 '11 at 16:29
add comment

Your Answer


