如何使用堆栈跟踪或反射找到方法的调用者? [英] How do I find the caller of a method using stacktrace or reflection?

查看:98
本文介绍了如何使用堆栈跟踪或反射找到方法的调用者?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I need to find the caller of a method. Is it possible using stacktrace or reflection?

推荐答案

StackTraceElement[] stackTraceElements = Thread.currentThread().getStackTrace()

根据Javadocs:

According to the Javadocs:


数组的最后一个元素表示堆栈的底部,这是序列中最近的方法调用。

The last element of the array represents the bottom of the stack, which is the least recent method invocation in the sequence.

A StackTraceElement getClassName() getFileName() getLineNumber() getMethodName()

你必须尝试确定你想要哪个索引
(可能 stackTraceElements [1] [2] )。

You will have to experiment to determine which index you want (probably stackTraceElements[1] or [2]).

这篇关于如何使用堆栈跟踪或反射找到方法的调用者?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆