How to print different stack frames using StackWalker API in Java?

JavaObject Oriented ProgrammingProgramming

Java 9 defines a StackWalker API that provides laziness and frame filtering. An object of StackWalker allows us to traverse and access stacks and contains one useful method: walk(). This method opens a StackFrame stream for the current thread, then applies the function with that StackFrame stream. We need to get StackWalker object, then use StackWalker.getInstance() method.

In the below example, we can print different stack frames: all stack frames, skip some stack frames and limit stack frames by using StackWalker API.

Example

import java.lang.StackWalker.StackFrame;
import java.util.*;
import java.util.stream.*;

public class StackWalkerTest {
   public static void main(String args[]) {
      new StackWalkerTest().walk();
   }
   private void walk() {
      new Walker1().walk();
   }
   private class Walker1 {
      public void walk() {
         new Walker2().walk();
      }
   }
   private class Walker2 {
      public void walk() {
         Method1();
      }
      void Method1() {
         Method2();
      }
      void Method2() {
         StackWalker stackWalker = StackWalker.getInstance(Set.of(StackWalker.Option.RETAIN_CLASS_REFERENCE, StackWalker.Option.SHOW_HIDDEN_FRAMES), 16);
         Stream<StackFrame> stackStream = StackWalker.getInstance().walk(f -> f);

         System.out.println("--- Walk all StackFrames ---");
         List<String> stacks = walkAllStackframes();
         System.out.println(stacks);

         System.out.println("--- Skip some StackFrames ---");
         List<String> stacksAfterSkip = walkSomeStackframes(3);
         System.out.println(stacksAfterSkip);

         System.out.println("--- Limit StackFrames ---");
         List<String> stacksByLimit = walkLimitStackframes(3);
         System.out.println(stacksByLimit);
      }
      private List<String> walkAllStackframes() {
         return StackWalker.getInstance().walk(s -> s.map(frame -> "\n" + frame.getClassName() + "/" + frame.getMethodName()).collect(Collectors.toList()));
      }
      private List<String> walkSomeStackframes(int numberOfFrames) {
         return StackWalker.getInstance().walk(s -> s.map(frame -> "\n" + frame.getClassName() + "/" + frame.getMethodName()).skip(numberOfFrames).collect(Collectors.toList()));
      }
      private List<String> walkLimitStackframes(int numberOfFrames) {
         return StackWalker.getInstance().walk(s -> s.map(frame -> "\n" + frame.getClassName() + "/" + frame.getMethodName()).limit(numberOfFrames).collect(Collectors.toList())); 
      }
   }
}

Output

--- Walk all StackFrames ---
[
StackWalkerTest$Walker2/walkAllStackframes,
StackWalkerTest$Walker2/Method2,
StackWalkerTest$Walker2/Method1,
StackWalkerTest$Walker2/walk,
StackWalkerTest$Walker1/walk,
StackWalkerTest/walk,
StackWalkerTest/main
]
--- Skip some StackFrames ---
[
StackWalkerTest$Walker2/walk,
StackWalkerTest$Walker1/walk,
StackWalkerTest/walk,
StackWalkerTest/main
]
--- Limit StackFrames ---
[
StackWalkerTest$Walker2/walkLimitStackframes,
StackWalkerTest$Walker2/Method2,
StackWalkerTest$Walker2/Method1
]
raja
Published on 11-Mar-2020 07:19:48
Advertisements