Getting name of currently executing method is useful for handling exceptions and debugging purposes.
Below are different methods to get currently executing method :
- Using Throwable Stack Trace :
- Using Throwable Class : In Java, Throwable class is the superclass of all exceptions and errors in java.lang package. Java Throwable class provides several methods like addSuppressed(), getMessage(), getStackTrace(), getSuppressed(), toString(), printStackTrace() etc.
We can get an array of stack trace elements representing the stack trace pertaining to throwable by calling getStackTrace() on a Throwable instance. At 0th index element of the array represents the top of the stack, which is the latest method call in the sequence.// Java program to demonstrate
// Getting name of current method
// using Throwable Class
public
class
GFG {
public
static
void
foo()
{
// getStackTrace() method return
// current method name at 0th index
String nameofCurrMethod =
new
Throwable()
.getStackTrace()[
0
]
.getMethodName();
System.out.println(
"Name of current method: "
+ nameofCurrMethod);
}
public
static
void
main(String[] args)
{
// call function
foo();
}
}
Output:
Name of current method: foo
- Using Exception Class
We can also use Exception class which extends Throwable class.// Java program to demonstrate
// Getting name of current method
// using Throwable Class
public
class
GFG {
public
static
void
foo()
{
// getStackTrace() method return current
// method name at 0th index
String nameofCurrMethod =
new
Exception()
.getStackTrace()[
0
]
.getMethodName();
System.out.println(
"Name of current method: "
+ nameofCurrMethod);
}
public
static
void
main(String[] args)
{
// call function
foo();
}
}
Output:
Name of current method: foo
- Using Throwable Class : In Java, Throwable class is the superclass of all exceptions and errors in java.lang package. Java Throwable class provides several methods like addSuppressed(), getMessage(), getStackTrace(), getSuppressed(), toString(), printStackTrace() etc.
- Using getEnclosingMethod() method
- By Object Class : We can use Class.getEnclosingMethod(), this method returns a Method object representing the instantly enclosing method of the prime class. But this come with a expressive overhead as it involves creating a new anonymous inner class behind the scenes.
// Java program to demonstrate
// Getting name of current method
// using Object Class
public
class
GFG {
// create a static method foo
public
static
void
foo()
{
// this method return a Method object representing
// the instantly enclosing method of the method class
String nameofCurrMethod =
new
Object() {}
.getClass()
.getEnclosingMethod()
.getName();
System.out.println(
"Name of current method: "
+ nameofCurrMethod);
}
public
static
void
main(String[] args)
{
// call function
foo();
}
}
Output:
Name of current method: foo
- By Inner Class : We can also define a inner class within a method to get Class reference.
// Java program to demonstrate
// Getting name of current method
// using Inner Class
public
class
GFG {
// create a static method foo
public
static
void
foo()
{
// Local inner class
class
Inner {
};
// this method return a Method object representing
// the instantly enclosing method of the method class
String nameofCurrMethod = Inner.
class
.getEnclosingMethod()
.getName();
System.out.println(
"Name of current method: "
+ nameofCurrMethod);
}
public
static
void
main(String[] args)
{
// call function
foo();
}
}
Output:
Name of current method: foo //This java program on our machine because inner class // have some restriction for security purpose
- By Object Class : We can use Class.getEnclosingMethod(), this method returns a Method object representing the instantly enclosing method of the prime class. But this come with a expressive overhead as it involves creating a new anonymous inner class behind the scenes.
- Using Thread Stack Trace : The Thread.getStackTrace() method returns array of stack trace elements. The second element of the returned array of stack trace contains name of method of current thread.
// Java program to demonstrate
// Getting name of current method
// using Thread.getStackTrace()
public
class
GFG {
// create a static method foo
public
static
void
foo()
{
// Thread.currentThread() return current method name
// at 1st index in Stack Trace
String nameofCurrMethod = Thread.currentThread()
.getStackTrace()[
1
]
.getMethodName();
System.out.println(
"Name of current method: "
+ nameofCurrMethod);
}
public
static
void
main(String[] args)
{
// call function
foo();
}
}
Output:
Name of current method: foo