venerdì 4 giugno 2010

Retrieve java call Stack in 1.3 e 1.4+

Is possible to retrieve java callstack,
to determine which programm is calling from.

       Throwable e = new Throwable();
//       In java 1.4+
//        e.fillInStackTrace();
//        StackTraceElement[] elements = e.getStackTrace();
//        String callerClass=(elements.length > 1 ? elements[1].toString() : "(no caller)");
//       

        In java   1.3
        StringWriter sw = new StringWriter();
        PrintWriter pw = new PrintWriter(sw);
        e.printStackTrace(pw);
        String callerClass=sw.toString();
       

Nessun commento:

Posta un commento