Monday

Getting the name of the current executing method

public class Test {

       public static void main(String[] args) {
              lessTalkMoreCode1();
       }
      
       private static void lessTalkMoreCode1(){
              lessTalkMoreCode2();
       }
      
       private static void lessTalkMoreCode2(){
              lessTalkMoreCode3();
       }
      
       private static void lessTalkMoreCode3(){
              String className= Thread.currentThread().getStackTrace()[1].getClassName();
              System.out.println("Current executing class: " + className);
             
              System.out.println("Executing class list buttom up:");
              String method= Thread.currentThread().getStackTrace()[1].getMethodName();
              System.out.println(method);
      
              method= Thread.currentThread().getStackTrace()[2].getMethodName();
              System.out.println(method);
             
              method= Thread.currentThread().getStackTrace()[3].getMethodName();
              System.out.println(method);
             
              method= Thread.currentThread().getStackTrace()[4].getMethodName();
              System.out.println(method);
       }

}

output:
Current executing class: Test
Executing class list buttom up:
lessTalkMoreCode3
lessTalkMoreCode2
lessTalkMoreCode1
main


No comments:

Post a Comment