我是靠谱客的博主 积极哑铃,这篇文章主要介绍如何在java中获取一个方法的调用者,现在分享给大家,希望可以做个参考。

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

public class TestClass {
	public static void main(String[] args)
	{
		TestClass tc = new TestClass();
		tc.test();
	}

	public void test()
	{
		MethodTest mt = new MethodTest();
		mt.getCaller();
	}

}
class MethodTest{
	public void getCaller()
	{
		StackTraceElement stack[] = (new Throwable()).getStackTrace();
		for (int i = 0; i < stack.length; i++)
		{
			StackTraceElement s = stack[i];
			System.out.format(" ClassName:%dt%sn", i, s.getClassName());
			System.out.format("MethodName:%dt%sn", i, s.getMethodName());
			System.out.format("  FileName:%dt%sn", i, s.getFileName());
			System.out.format("LineNumber:%dt%snn", i, s.getLineNumber());
		}
	}
}

 

转载于:https://my.oschina.net/lizongyue/blog/1631397

最后

以上就是积极哑铃最近收集整理的关于如何在java中获取一个方法的调用者的全部内容,更多相关如何在java中获取一个方法内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(113)

评论列表共有 0 条评论

立即
投稿
返回
顶部