获得调用者信息

    技术2022-05-11  122

    package test.io; public class CallerTest {          public static String getCaller() {         String info = null;         StackTraceElement stack[] = (new Throwable()).getStackTrace();         for (int i=0; i < stack.length; i++) {           StackTraceElement ste=stack[i];           StringBuffer buf = new StringBuffer()                   .append("Level:").append(i)                   .append("/tLine:").append(ste.getLineNumber())                   .append("/tMethod:").append(ste.getMethodName())                   .append("/tClass:").append(ste.getClassName())                   .append("/tFile:").append(ste.getFileName());           info = buf.toString();           System.out.println(info);         }         return info;     }          private void a() {         getCaller();     }          private void b() {         a();     }     public void c() {         b();     }     public static void main(String[] args) {         new CallerTest().c();     } }

    最新回复(0)