我需要找到方法的调用者。是否可以使用堆栈跟踪或反射?
最佳答案
StackTraceElement[] stackTraceElements = Thread.currentThread().getStackTrace()
根据 Javadocs:
The last element of the array represents the bottom of the stack, which is the least recent method invocation in the sequence.
StackTraceElement
具有 getClassName()
、getFileName()
、getLineNumber()
和 getMethodName ()
.
您必须进行试验才能确定您想要哪个索引
(可能是 stackTraceElements[1]
或 [2]
)。
关于java - 如何使用堆栈跟踪或反射找到方法的调用者?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57080630/