jdk1.5以前:
(new Exception()).getStackTrace()[2].getMethodName()
(new Exception()).getStackTrace()[2].getClassName()()
(new Exception()).getStackTrace()[2].getLineNumber()
jdk1.5之后
Thread.currentThread() .getStackTrace()[2].getMethodName()
Thread.currentThread() .getStackTrace()[2].getClassName()()
Thread.currentThread() .getStackTrace()[2].getLineNumber()
其实看看源代码
Thread.currentThread() .getStackTrace()中就是用的 (new Exception()).getStackTrace()
如下:
本文介绍了在Java中如何获取当前线程的调用堆栈信息,并对比了JDK 1.5之前和之后的不同方法。通过使用`getStackTrace()`方法,可以获取到包括方法名、类名及行号等详细信息。

295

被折叠的 条评论
为什么被折叠?



