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()
如下: