Java – how to find the calling class?

How can I find out which class / method calls the actual method?

Solution

You can try to create an exception to get its stack trace

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

Stacktraceelement [0] now contains the caller of the current method

But note (from throwable. Getstacktrace()):

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>