加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java – 如何找到调用类?

发布时间:2020-12-15 05:15:25 所属栏目:Java 来源:网络整理
导读:我怎样才能找出哪个类/方法调用了实际方法? 解决方法 您可以尝试创建一个异常来获取其堆栈跟踪. Throwable t = new Throwable();StackTraceElement[] stackTraceElements = t.getStackTrace(); 现在stackTraceElement [0]包含当前方法的调用者. 但要注意(从
我怎样才能找出哪个类/方法调用了实际方法?

解决方法

您可以尝试创建一个异常来获取其堆栈跟踪.

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.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读