3

Say I have a method m in a class. I'm trying to find all the methods that call m in the whole project. Similar to eclipse's call hierarchy tool, except I need it to output a list/array of Methods. I need it statically so I dont think a stack trace works. Is there any way to implement this, maybe using reflection?

I attached link to an example of Eclipse's call hierarchy tool, finding all the methods that mention the method barking() from the class Dog. (Dog.hungry() and Cat.scratching() call barking()).

Image

Praveen E
  • 926
  • 8
  • 14
Cazs
  • 61
  • 2
  • 4
  • 1
    I think this has been already answered [here](http://stackoverflow.com/questions/930289/how-can-i-find-all-the-methods-that-call-a-given-method-in-java). – Muton Oct 24 '14 at 19:44

1 Answers1

1

The "standard" java reflection will probably not be of any help on this.

I would try using the open source Reflection library.

You can search for a method with via:

Set<Member> usages = reflections.getMethodUsages(Method.class)
Drejc
  • 14,196
  • 16
  • 71
  • 106
  • Im trying to clone the project into my eclipse so i can make a jar out of it but it keeps telling me its empty. I'm not very familiar with adding open source libs. If I use maven to install, will it be a plugin that opens me a new eclipse window at runtime? – Cazs Oct 25 '14 at 20:07
  • Try using maven ... it will add it as a dependency (jar) of its own. But I'm sure you can download it directly and add it as a local dependency to a Eclipse project. Have you googled it? ... like here: https://code.google.com/p/reflections/downloads/list – Drejc Oct 26 '14 at 16:49
  • Hey Drejc, so I downladed the latest jar from the link, and added it to my jars file, and went into my java build path and added it there as well. But when I use reflections it asks me if I want to add "import org.reflections.Reflections;" and not "import static org.reflections.ReflectionUtils.*;". When I do import either of them, "Set usages = reflections.getMethodUsages(Method.class)" gives me an error. Any idea why?? Thanks again – Cazs Oct 26 '14 at 19:44
  • Here are a couple screenshots of what I tried and failed with. Im also not sure if I am constructing reflections properly http://imgur.com/a/0YBJH – Cazs Oct 26 '14 at 19:51
  • Really hard to tell whats "wrong", but I would suggest to go with some easy examples first, if you take a look at the tests inside the Reflections project you will find plenty use cases. Just a quick glance and I found this: Reflections reflections = new Reflections(TestModel.class, new FieldAnnotationsScanner()); Probably the way to go ... using a different scanner of course. – Drejc Oct 27 '14 at 07:44