Get name of current method being executed in Java
This post will discuss how to get the name of the current method being executed in Java.
1. Using Class.getEnclosingMethod() method
The idea is to call the Class.getEnclosingMethod() that returns a java.lang.reflect.Method object with almost every bit of information on the immediately enclosing method of the underlying class. This comes with a significant overhead as it involves creating a new anonymous inner class behind the scenes.
|
1 2 3 4 5 6 7 8 9 10 |
// Get the name of the current method being executed in Java public static void fun() { String methodName = new Object() {} .getClass() .getEnclosingMethod() .getName(); System.out.println("Current Method is " + methodName); } |
We can also define an inner class within a method to get Class reference, as shown below:
|
1 2 3 4 5 6 7 8 9 10 |
// Get the name of the current method being executed in Java public static void fun() { class Dummy {}; String methodName = Dummy.class .getEnclosingMethod() .getName(); System.out.println("Current Method is " + methodName); } |
2. Using Throwable Stack Trace – Throwable.getStackTrace() method
We know that the Throwable class is the superclass of all errors and exceptions in Java. We can get an array of stack trace elements representing the stack trace pertaining to a throwable by calling getStackTrace() on a Throwable instance. The first element of the array represents the top of the stack, which is the last method invocation in the sequence.
|
1 2 3 4 5 6 7 8 9 |
// Get the name of the current method being executed in Java public static void fun() { String methodName = new Throwable() .getStackTrace()[0] .getMethodName(); System.out.println("Current Method is " + methodName); } |
We can also use the Exception class, which extends Throwable.
|
1 2 3 4 5 6 7 8 9 |
// Get the name of the current method being executed in Java public static void fun() { String methodName = new Exception() .getStackTrace()[0] .getMethodName(); System.out.println("Current Method is " + methodName); } |
3. Using Thread Stack Trace – Thread.getStackTrace() method
We can also use the stack trace of a current thread, which returns an array of stack trace elements representing the stack dump of this thread. The second element of the returned array of stack trace contains the stack frame of the current method.
|
1 2 3 4 5 6 7 8 9 |
// Get the name of the current method being executed in Java public static void fun() { String methodName = Thread.currentThread() .getStackTrace()[1] .getMethodName(); System.out.println("Current Method is " + methodName); } |
The problem with using Throwable stack trace and Thread stack trace is that some virtual machines may skip one or more stack frames from the stack trace under some special circumstances.
That’s all about getting the name of the current method being executed in Java.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)