Skip to main content

Posts

Showing posts from 2018

Get current executing method name in Java

//Get method name using Java.lang.Class.getEnclosingMethod()
    public static void getMethodNameUsingClassInstance() {         String methodNameUsingClassInstance =             new Object() {}.getClass().getEnclosingMethod().getName();         System.out.println("Current Method Execution Name Using Class Instance - " +             methodNameUsingClassInstance);     }

//Get method name using StackTraceElement.getMethodName()
    public static void getMethodNameUsingStackTraceElement() {         StackTraceElement stackTraceElements[] = (new Throwable()).getStackTrace();         System.out.println("Current Method Execution Name Using StackTraceElement - " +             stackTraceElements[0].getMethodName());     }

//Get method name using Thread.currentThread().getStackTrace()
    public static void getMethodNameUsingCurrentThread() {         System.out.println("Current Method Execution Name using Current Thread - " +             Thread.currentThread().getStackTrace()[1].ge…