Reputation: 3919
I am trying to learn unnecessarily complicated source code. There is a method public void passObject(SomeObject someObject)
, and I want to know which class someObject
was instantiated in. The problem is, as we traverse up the call hierarchy, the branching factor is very large. For example, three methods can pass someObject
through passObject
, and three methods before these three methods can pass the same object respectively.
Is there some method that I can insert into passObject
such as:
public void passObject(SomeObject someObject) {
whereDidThisComeFrom(someObject); // Should print the class (perhaps) where someObject was instantiated.
//
//do the other stuff that passObject is supposed to do.
}
I use Eclipse
.
Upvotes: 0
Views: 1404
Reputation: 27958
The easiest / dirtiest way is to just put (new Exception("test")).printStackTrace()
in SomeObject
's constructor and run the code. You'll get a stack trace printed to the console every time SomeObject
is instantiated.
Eclipse also supports breakpoints in constructors so you could set a breakpoint and debug.
Upvotes: 4
Reputation: 9559
It's true that using 'Show Call Hierachy' can show too many results to really give you an idea of execution flow.
Instead, you could try using Eclipse in debug mode, and put a break-point on the first line of passObject(SomeObject someObject)
. You'll then see the current stack-trace. By inspecting the code on the stack-trace you can determine the call hierarchy which actually occurred.
Alternatively, if you can't debug, can you add another line to passObject(SomeObject someObject)
to print the current stack trace? See here for details.
Upvotes: 3