How to Get a Name of a Method Being Executed?

1. Overview

Sometimes we need to know the name of the current Java method being executed.

This quick article presents a couple of simple ways of getting hold of the method name in the current execution stack.

2. Java 9: Stack-Walking API

Java 9 introduced the Stack-Walking API to traverse the JVM stack frames in a lazy and efficient manner. In order to find the currently executing method with this API, we can write a simple test:

public void givenJava9_whenWalkingTheStack_thenFindMethod() {
    StackWalker walker = StackWalker.getInstance();
    Optional<String> methodName = walker.walk(frames -> frames
      .findFirst()
      .map(StackWalker.StackFrame::getMethodName));

    assertTrue(methodName.isPresent());
    assertEquals("givenJava9_whenWalkingTheStack_thenFindMethod", methodName.get());
}

First, we get a StackWalker instance using the getInstance() factory method. Then we use the walk() method to traverse the stack frames from the top to the bottom:

  • The walk() method can convert a stream of stack frames — Stream<StackFrame> — to anything
  • The first element in the given stream is the top frame on the stack
  • The top frame on the stack always represents the currently executing method

Therefore, if we get the first element from the stream, we’ll know the currently executing method details. More specifically, we can use StackFrame.getMethodName() to find the method name.

2.1. Advantages

Compared to other approaches (more on them later), the Stack-Walking API has a few advantages:

  • No need to create a dummy anonymous inner class instance — new Object().getClass() {}
  • No need to create a dummy exception — new Throwable()
  • No need to eagerly capture the whole stacktrace, which can be costly

On the contrary, the StackWalker only walks the stack one by one in a lazy fashion. In this case, it only fetches the top frame, as opposed to the stacktrace approach which captures all the frames eagerly.

The bottom line is, use the Stack-Walking API if you’re on Java 9+.

3. Using getEnclosingMethod

We can find the name of the method being executed by using the getEnclosingMethod() API:

public void givenObject_whenGetEnclosingMethod_thenFindMethod() {
    String methodName = new Object() {}
      .getClass()
      .getEnclosingMethod()
      .getName();
       
    assertEquals("givenObject_whenGetEnclosingMethod_thenFindMethod",
      methodName);
}

4. Using Throwable Stack Trace

Using a Throwable stack trace gives us stack trace with the method currently being executed:

public void givenThrowable_whenGetStacktrace_thenFindMethod() {
    StackTraceElement[] stackTrace = new Throwable().getStackTrace();
 
    assertEquals(
      "givenThrowable_whenGetStacktrace_thenFindMethod",
      stackTrace[0].getMethodName());
}

5. Using Thread Stack Trace

Also, the stack trace of a current thread (since JDK 1.5) usually includes the name of the method that is being executed:

public void givenCurrentThread_whenGetStackTrace_thenFindMethod() {
    StackTraceElement[] stackTrace = Thread.currentThread()
      .getStackTrace();
 
    assertEquals(
      "givenCurrentThread_whenGetStackTrace_thenFindMethod",
      stackTrace[1].getMethodName()); 
}

However, we need to remember that this solution has one significant drawback. Some virtual machines may skip one or more stack frames. Although this is not common, we should be aware that this can happen.

6. Conclusion

In this tutorial, we have presented some examples of how to get the name of the currently executed method. Examples are based on stack traces and the getEnclosingMethod().

As always, you can check out the examples provided in this article over on GitHub. Also, Java 9 examples are available in our Java 9 GitHub module.

Related posts:

Java Program to Implement Treap
LinkedList trong java
Copy a List to Another List in Java
Java Program to Find All Pairs Shortest Path
Intro to Spring Boot Starters
Mảng (Array) trong Java
Java Timer
Java Program to Implement Wagner and Fisher Algorithm for online String Matching
Java Program to Implement Cubic convergence 1/pi Algorithm
Cơ chế Upcasting và Downcasting trong java
Introduction to Spring MVC HandlerInterceptor
Generating Random Numbers in a Range in Java
Converting a Stack Trace to a String in Java
Java Web Services – Jersey JAX-RS – REST và sử dụng REST API testing tools với Postman
Java Streams vs Vavr Streams
Java Program to Implement First Fit Decreasing for 1-D Objects and M Bins
Java Program to Implement Aho-Corasick Algorithm for String Matching
Java Program to Test Using DFS Whether a Directed Graph is Strongly Connected or Not
Introduction to Spring Data REST
Java Program to Implement CopyOnWriteArraySet API
Java Program to Implement Find all Cross Edges in a Graph
Java Program to Generate All Subsets of a Given Set in the Gray Code Order
REST Web service: Basic Authentication trong Jersey 2.x
Logout in an OAuth Secured Application
Java Program to Implement Fisher-Yates Algorithm for Array Shuffling
Phân biệt JVM, JRE, JDK
Java Program to Implement a Binary Search Algorithm for a Specific Search Sequence
How to Read a File in Java
Java Program to Perform the Sorting Using Counting Sort
A Quick Guide to Using Keycloak with Spring Boot
Java Program to Find the Number of Ways to Write a Number as the Sum of Numbers Smaller than Itself
Converting Between Byte Arrays and Hexadecimal Strings in Java