Eclipse JDT Tutorial: Find All References of a Method

When using JDT, you may want to find all references of a given method and then do something with the references. This can be done by using method JDTSearchProvider.searchMethodReference().

The following is sample code:

// First of all, get all the type declaration of the class.
IType [] typeDeclarationList = unit.getTypes();
 
for (IType typeDeclaration : typeDeclarationList) {
     // get methods list
     IMethod [] methodList = typeDeclaration.getMethods();
 
     for (IMethod method : methodList) {
          final List<String> referenceList = new ArrayList<String>();
          // check each method.
          String methodName = method.getElementName();
          if (!method.isConstructor()) {
              // Finds the references of the method and record references of the method to referenceList parameter.
              JDTSearchProvider.searchMethodReference(referenceList, method, scope, iJavaProject);
          }
     }
}

8 thoughts on “Eclipse JDT Tutorial: Find All References of a Method”

  1. U can use your own implementation to replace some original dependences . U can use java model in this way.

  2. All the jar can be found in MavenRepository, but problem is after doing all one will get error like –
    Exception in thread “main” java.lang.IllegalStateException: Workspace is closed.
    So one needs to load Eclipse workspace first, which I found need lot of effort.

Leave a Comment