我如何找出哪个类/方法调用了实际方法?
最佳答案
您可以尝试创建异常以获取其堆栈跟踪。
Throwable t = new Throwable();
StackTraceElement[] stackTraceElements = t.getStackTrace();
现在 stackTraceElement[0]
包含当前方法的调用者。
但要注意(来自 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.
关于java - 我如何找出调用类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1007268/