Java 9 defines a StackWalkerAPI which provides laziness and frame filtering. The StackWalker object allows us to walk and access the stack and contains a useful method: walk(). This method opens a StackFramestream for the current thread and then applies the function to that StackFramestream. We need to get the StackWalker object and then use the StackWalker.getInstance() method.
In the example below, we can print different content stack frames: All stack frames, Skip some stack frames and using StackWalkerAPI Limit Stack frames. p>
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 = <strong>StackWalker.getInstance</strong>(Set.of(StackWalker.Option.RETAIN_CLASS_REFERENCE, StackWalker.Option.SHOW_HIDDEN_FRAMES), 16); <strong>Stream<StackFrame></strong> stackStream = <strong>StackWalker.getInstance().walk</strong>(f -> f); System.out.println("--- Walk all StackFrames ---"); <strong>List<String></strong> stacks = walkAllStackframes(); System.out.println(stacks); System.out.println("--- Skip some StackFrames ---"); <strong>List<String></strong> stacksAfterSkip = walkSomeStackframes(3); System.out.println(stacksAfterSkip); System.out.println("--- Limit StackFrames ---"); <strong>List<String></strong> stacksByLimit = walkLimitStackframes(3); System.out.println(stacksByLimit); } private List<String> walkAllStackframes() { return <strong>StackWalker.getInstance().walk</strong>(s -> s.map(frame -> "\n" + frame.getClassName() + "/" + frame.getMethodName()).<strong>collect</strong>(Collectors.toList())); } private List<String> walkSomeStackframes(int numberOfFrames) { return <strong>StackWalker.getInstance().walk</strong>(s -> s.map(frame -> "\n" + frame.getClassName() + "/" + frame.getMethodName()).<strong>skip</strong>(numberOfFrames).<strong>collect</strong>(Collectors.toList())); } private List<String> walkLimitStackframes(int numberOfFrames) { return <strong>StackWalker.getInstance().walk</strong>(s -> s.map(frame -> "\n" + frame.getClassName() + "/" + frame.getMethodName()).<strong>limit</strong>(numberOfFrames).<strong>collect</strong>(Collectors.toList())); } } }
<strong>--- Walk all StackFrames --- [ StackWalkerTest$Walker2/walkAllStackframes, StackWalkerTest$Walker2/Method2, StackWalkerTest$Walker2/Method1, StackWalkerTest$Walker2/walk, StackWalkerTest$Walker1/walk, StackWalkerTest/walk, StackWalkerTest/main</strong> <strong>] --- Skip some StackFrames --- [ StackWalkerTest$Walker2/walk, StackWalkerTest$Walker1/walk, StackWalkerTest/walk, StackWalkerTest/main</strong> <strong>] --- Limit StackFrames --- [ StackWalkerTest$Walker2/walkLimitStackframes, StackWalkerTest$Walker2/Method2, StackWalkerTest$Walker2/Method1</strong> <strong>]</strong>
The above is the detailed content of How to print different stack frames using StackWalker API in Java?. For more information, please follow other related articles on the PHP Chinese website!