How to get method call stack information in java code
Hi,
I have a method a() in class A. I want to get the name of method and class which called method a. Is there any way of doing it ?
I tried using StackIntrospector from org.apache.log.util package, and it solves my purpose. Is the same thing possible using standard java API's without using any external API's ?
# 1
Usually when I try something like this, I use a trick:
Throwable t = new Throwable();
StackTraceElement[] stackTrace = t.getStackTrace();
It's not nice but it works.
# 2
> Usually when I try something like this, I use a
> trick:
>
> > Throwable t = new Throwable();
> StackTraceElement[] stackTrace = t.getStackTrace();
>
>
> It's not nice but it works.
StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();
Slightly more elegant, and saves the overhead of creating a new Throwable
If your code depends on this to execute, it may well be poorly designed, though