`

java 中获得当前方法名及行号.....---可以做日志记录及跟踪

 
阅读更多

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()

如下:


分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics