gpt4 book ai didi

java - 如何使用堆栈跟踪或反射找到方法的调用者?

转载 作者:行者123 更新时间:2023-12-02 02:55:18 26 4
gpt4 key购买 nike

我需要找到方法的调用者。是否可以使用堆栈跟踪或反射?

最佳答案

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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com