Saiyine
Punto Com

Calling hierarchy in Java

2007-10-20 06:45:13

With the next code you'll be able to know the calling hierarchy at runtime, or, in layman's words, who the heck is calling your code while the program is running. It has a problem, tough: it only works for Java 5 or better.

public class Llamadas
{
        static public void fin()
        {
                StackTraceElement trace[] = Thread.currentThread().getStackTrace();
                for (StackTraceElement St : trace)
                {
                        System.out.println(St);
                }
        }
       
        static public void salto()
        {
                fin();
        }
       
        public static void main(String[] args)
        {
                salto();
        }
}

Should print something like...

java.lang.Thread.getStackTrace(Unknown Source)
pruebas.Llamadas.fin(Llamadas.java:7)
pruebas.Llamadas.salto(Llamadas.java:16)
pruebas.Llamadas.main(Llamadas.java:21)

The code is extremely simple: the main class calls salto, who itself calls the fin method. This fin method contains a foreach wich prints the return from a call to getStackTrace, the subroutine call stack.

Rollos antiguos

2007-10-20 06:37:48 - Jerarquia de llamada en Java.

2007-10-20 05:47:16 - The time of your life.

2007-10-02 07:24:23 - What if people bought cars like they buy computers?.

2007-09-29 07:08:11 - Alien vs Predator: Requiem.

2007-08-28 09:13:29 - El puto amo.

Saiyine

Selfie of meHi! Welcome to Saiyine Punto Com where I talk about anything that goes through my mind!

Puedo prometer y prometo que a la mayor brevedad aquí irá un menú o algo asín.