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

时间:2022-05-27 10:30:29
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() + ", " );
        }
    }
    
}