Tutorial :How do I find out the calling class?



Question:

how can i find out which class/method has called the actual method?


Solution:1

You could try to create an exception to get its stacktrace.

Throwable t = new Throwable();  StackTraceElement[] stackTraceElements = t.getStackTrace();  

Now stackTraceElement[0] contains the caller of the current method.

But beware (from Throwable.getStackTrace()):

Some virtual machines may, under some circumstances, omit one or more stack frames from the stack trace. In the extreme case, a virtual machine that has no stack trace information concerning this throwable is permitted to return a zero-length array from this method. Generally speaking, the array returned by this method will contain one element for every frame that would be printed by printStackTrace.


Solution:2

Here's one way that I've used:

StackTraceElement element=Thread.currentThread().getStackTrace()[3];  String className=element.getClassName();  String methodName=element.getMethodName();  

[3] is hardcoded because:

[0] is Thread.dumpThreads()

[1] is Thread.getStackTrace()

[2] is the current method

[3] is the one before the current method


Solution:3

A faster but non-portable solution is to use the following. It does not create a stack trace and just gives you the information you need. However, not all JVMs will have this and future version of Java might not either.

Class callerClass = sun.reflect.Reflection.getCallerClass(2);  


Solution:4

You can print a stack trace to do this.

If you want to do this dynamically, I'm not really sure if this is possible (aside from printing and parsing a stack trace dynamically).


Solution:5

You could use a debugger, or a profiler. Netbeans has both, but a lot of other options exists.

Else, if you can modify the code you can throw a new exception() and have a stacktrace printed in the console.


Solution:6

To echo and elaborate on matt b and yishai's comments:

If you are doing this because you are writing a logger or maintaining trace information or some such, okay, cool. I've used stack traces in production code exactly once, and even that was really a debugging issue: We had a problem with database connections not being properly closed, so I modified the "get database connection" function to save the identity of the caller, and then had a periodic sweep to look for dead connections and see where they had been created.

Java's built-in logging function does stack traces so it can write who called the logger to the log file. I worry about the overhead of this as I understand that stack traces are expensive, but whatever.

But if you're doing this because your function is going to behave differently depending on where it was called from, like "if called from class X update customer data else if called from class Y update employee data" or something like that: Really really bad idea. Pass a parameter or write separate functions.


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »