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
.
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.