Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting a call hierarchy in java

I am having real trouble tracking down a bug and it would help be a lot to know which method called a certain method. Is there an easy way to get a call hierarchy from java? Java is a small part of the app so I cannot compile and run the whole app in eclipse/net beans so I don't have access to an IDE debugger's call hierarchy.

like image 249
Mike2012 Avatar asked Jul 28 '09 23:07

Mike2012


People also ask

How do I find my call hierarchy?

Use the Call Hierarchy window To display the Call Hierarchy window, right-click in the code editor on the name of a method, property, or constructor call, and then select View Call Hierarchy.

What is call hierarchy in Java?

Call Hierarchy allows you to quickly see all the places where a function or method is used. How to use: Press Ctrl + Alt + H on Windows/Linux, ⌥ + ^ + H on macOS, and the Call Hierarchy tool window will open up with all the places that the function is called inside.

How do I get call hierarchy in eclipse?

To find the references of a method, eclipse has a plugin called Open Call Hierarchy(Ctrl+Alt+H).

How do you create a hierarchy in Java?

The class named File of the java.io package represents a file or directory (path names) in the system. This class provides various methods to perform various operations on files/directories. The mkdir() method of this class creates a directory with the path represented by the current object.


2 Answers

Thread.currentThread().getStackTrace();

or

Exception ex = new Exception();
ex.printStackTrace();

It's fairly slow, but fine for debugging purposes. API docs here.

like image 129
Nelson Avatar answered Oct 19 '22 23:10

Nelson


Java is a small part of the app so I cannot compile and run the whole app in eclipse/net beans so I don't have access to an IDE debugger's call hierarchy.

You dont need to run the app at all. If you make a project in Eclipse, you can use its built-in Call Hierarchy tool to find all of the possible places that call a method.

There is a trade off: The Call Hierarchy tool will give you ALL of the places from where a method is called. This is a good if you want/need to know all the possibilities. Neslson's suggestion of Thread.currentThread().getStackTrace() will give you the places from where a method is invoked during the process of you program for this invocation. The nuance is that you might not run through every code path during this invocation. If you are seeing specific behavior and want to know how you got there, use the getStackTrace() option.

like image 40
akf Avatar answered Oct 20 '22 00:10

akf