获取当前方法名,行号,类名,所在java文件第几行

时间:2021-09-23 10:47:08
public class Demo {

    public static void main(String[] args) {
Demo demo = new Demo();
demo.go();
} public void go(){
StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();
for(StackTraceElement s : stackTrace){
System.out.println("类名:" + s.getClassName() + " , java文件名:" + s.getFileName() + ", 当前方法名字:" + s.getMethodName() + ""
+ " , 当前代码是第几行:" + s.getLineNumber() + ", " );
}
} }