java代码获知该方法被哪个类、哪个方法、在哪一行调用

时间:2022-11-13 10:30:40
public class TestMain {
    public static void  main(String[] args){
        hello();
    }
    
    public static void hello() {
        String className = Thread.currentThread().getStackTrace()[2].getClassName();
        String methodName = Thread.currentThread().getStackTrace()[2].getMethodName();
        int lineNumber = Thread.currentThread().getStackTrace()[2].getLineNumber();
        
        System.out.println(className);
        System.out.println(methodName);
        System.out.println(lineNumber);
    }
}