Java: Find all callers of a method – get all methods that call a particular method

This tutorial belongs to Eclipse JDT Tutorial Series.

I will explore how we can compute fan in i.e. find all the methods that call a particular method (all callers of a method).

In Eclipse, one can right click on a method and choose "open call hierarchy" and eclipse shows all the methods that call the selected method and all the methods that are called from the selected method (callers and callees of the selected method).

We can programtically find callers and callees and I will explain how I have done it.

There are a couple of different ways that I know:

  1. Use the eclipse JDT internal classes : these are the same classes used by eclipse to show the callers and callees of a method
  2. Use the eclipse ast visitor

In this post, I will discuss how we can use the eclipse JDT internal classes to find callers and callees of a method.

Firstly, import the following classes (as you can see they are in an "internal" package!)

import org.eclipse.jdt.internal.corext.callhierarchy.CallHierarchy;
import org.eclipse.jdt.internal.corext.callhierarchy.MethodWrapper;

Write a methodgetCallersOf that takes the selected method as an input [IMethod]. This will be your API type method, you should call this method from your program for any method whose callers you wish to find.

The crucial part of the code is to first get the default CallHierarchy object, use this CallHierarchy object to get the caller-roots of the input IMethod, and for each method-wrapper object that is returned, invoke the getCalls method. getCalls in turn returns an array of MethodWrapper objects and these are the actual callers. From each MethodWrapper object, we can get a corresponding IMethod.

public HashSet<IMethod> getCallersOf(IMethod m) {
 CallHierarchy callHierarchy = CallHierarchy.getDefault();
 IMember[] members = {m};
 MethodWrapper[] methodWrappers = callHierarchy.getCallerRoots(members);
  HashSet<IMethod> callers = new HashSet<IMethod>();
  for (MethodWrapper mw : methodWrappers) {
    MethodWrapper[] mw2 = mw.getCalls(new NullProgressMonitor());
    HashSet<IMethod> temp = getIMethods(mw2);
return callers;
 HashSet<IMethod> getIMethods(MethodWrapper[] methodWrappers) {
  HashSet<IMethod> c = new HashSet<IMethod>(); 
  for (MethodWrapper m : methodWrappers) {
   IMethod im = getIMethodFromMethodWrapper(m);
   if (im != null) {
  return c;
 IMethod getIMethodFromMethodWrapper(MethodWrapper m) {
  try {
   IMember im = m.getMember();
   if (im.getElementType() == IJavaElement.METHOD) {
    return (IMethod)m.getMember();
  } catch (Exception e) {
  return null;
Category >> Java Development Tools (JDT)  
If you want someone to read your code, please put the code inside <pre><code> and </code></pre> tags. For example:
String foo = "bar";

  1. ShinyGirl on 2013-4-21

    how I can use this code if I have a list of methods (Method [] and not IMethod []) because this code generates me the following error: Exception in thread “main” java.lang.Error: Unresolved compilation problem:
    The method getCallersOf(IMethod) in the type TestingCalls is not applicable for the arguments ()

  2. ryanlr on 2013-4-23

    Can you post your code here?

  3. ShinyGirl on 2013-4-23

    I used reflection to get all classes and methods from a given java project
    with the method : class.getDeclaredMethods();

    I want to get the callers of each method in this list or if I can do the same thing with the API org.eclipse.jdt.core or if I can find an IMethod from the method name (string) to finally use the code you proposed

    thank you very much for your reply ^_^

  4. ShinyGirl on 2013-5-2

    i’m trying to analyse the JHotDraw project to calculate the fan in metric, but i’m getting a java.lang.nullPointerException @ this line MethodWrapper[] mw2 = mw.getCalls(new NullProgressMonitor());

    org.eclipse.e4.core.di.InjectionException: java.lang.NullPointerException
    at org.eclipse.e4.core.internal.di.MethodRequestor.execute(
    at org.eclipse.e4.core.internal.di.InjectorImpl.invokeUsingClass(
    at org.eclipse.e4.core.internal.di.InjectorImpl.invoke(
    at org.eclipse.e4.core.contexts.ContextInjectionFactory.invoke(
    at org.eclipse.e4.core.commands.internal.HandlerServiceImpl.executeHandler(
    at org.eclipse.e4.ui.workbench.renderers.swt.HandledContributionItem.executeItem(
    at org.eclipse.e4.ui.workbench.renderers.swt.HandledContributionItem.handleWidgetSelection(
    at org.eclipse.e4.ui.workbench.renderers.swt.HandledContributionItem.access$7(
    at org.eclipse.e4.ui.workbench.renderers.swt.HandledContributionItem$4.handleEvent(
    at org.eclipse.swt.widgets.EventTable.sendEvent(
    at org.eclipse.swt.widgets.Widget.sendEvent(
    at org.eclipse.swt.widgets.Display.runDeferredEvents(
    at org.eclipse.swt.widgets.Display.readAndDispatch(
    at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine$
    at org.eclipse.core.databinding.observable.Realm.runWithDefault(
    at org.eclipse.e4.ui.internal.workbench.E4Workbench.createAndRunUI(
    at org.eclipse.ui.internal.Workbench$
    at org.eclipse.core.databinding.observable.Realm.runWithDefault(
    at org.eclipse.ui.internal.Workbench.createAndRunWorkbench(
    at org.eclipse.ui.PlatformUI.createAndRunWorkbench(
    at org.eclipse.ui.internal.ide.application.IDEApplication.start(
    at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.runApplication(
    at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.start(
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.eclipse.equinox.launcher.Main.invokeFramework(
    at org.eclipse.equinox.launcher.Main.basicRun(
    at org.eclipse.equinox.launcher.Main.main(
    Caused by: java.lang.NullPointerException
    at org.eclipse.jdt.internal.compiler.lookup.LookupEnvironment.askForType(
    at org.eclipse.jdt.internal.compiler.lookup.PackageBinding.getTypeOrPackage(
    at org.eclipse.jdt.internal.compiler.lookup.Scope.getTypeOrPackage(
    at org.eclipse.jdt.internal.compiler.lookup.Scope.getType(
    at org.eclipse.jdt.internal.corext.callhierarchy.CallerMethodWrapper.findChildren(
    at org.eclipse.jdt.internal.corext.callhierarchy.MethodWrapper.performSearch(
    at org.eclipse.jdt.internal.corext.callhierarchy.MethodWrapper.doFindChildren(
    at org.eclipse.jdt.internal.corext.callhierarchy.MethodWrapper.getCalls(
    at projet.aspectm.plug.handler.ExtractHandler.getCallersOf(
    at projet.aspectm.plug.handler.ExtractHandler.printIMethodDetails(
    at projet.aspectm.plug.handler.ExtractHandler.printIMethods(
    at projet.aspectm.plug.handler.ExtractHandler.printCompilationUnitDetails(
    at projet.aspectm.plug.handler.ExtractHandler.printICompilationUnitInfo(
    at projet.aspectm.plug.handler.ExtractHandler.printPackageInfos(
    at projet.aspectm.plug.handler.ExtractHandler.execute(
    at org.eclipse.ui.internal.handlers.HandlerProxy.execute(
    at org.eclipse.ui.internal.handlers.E4HandlerProxy.execute(
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.eclipse.e4.core.internal.di.MethodRequestor.execute(
    … 34 more

  5. ryanlr on 2013-5-2

    I don’t know. Maybe you can post this question on stackoverflow.

  6. Kadir on 2015-1-13

    Thank you for your helpful article .
    However , My project is written in J2SE 1.3 version and i cannot change the version because of hardware reasons.Do you have any idea or any technique for finding all callers of a method in java 1.3 version.Could you please to help me answer the following question?

  7. bniyaseen on 2015-2-28

    can you post the second way :
    Use the eclipse ast visitor

  8. Danilo Batista on 2016-2-4

    I have the same problem. You could solve?
    thank you

  9. s3 on 2016-6-22

    I want to get the reference on code which is not in compiling stage.

  10. s3 on 2016-6-22

    I want to get the reference on code which is not in compiling stage
    Can anyone suggest the way?

  11. Jiří Kačírek on 2017-3-31

    Why did you not mentioned where your dependencies come from???

Leave a comment