How to print different stack frames using StackWalker API in Java?
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>
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 = <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())); } } }
Output
<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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Java's classloading involves loading, linking, and initializing classes using a hierarchical system with Bootstrap, Extension, and Application classloaders. The parent delegation model ensures core classes are loaded first, affecting custom class loa

The article discusses implementing multi-level caching in Java using Caffeine and Guava Cache to enhance application performance. It covers setup, integration, and performance benefits, along with configuration and eviction policy management best pra

The article discusses using JPA for object-relational mapping with advanced features like caching and lazy loading. It covers setup, entity mapping, and best practices for optimizing performance while highlighting potential pitfalls.[159 characters]

The article discusses using Maven and Gradle for Java project management, build automation, and dependency resolution, comparing their approaches and optimization strategies.

The article discusses creating and using custom Java libraries (JAR files) with proper versioning and dependency management, using tools like Maven and Gradle.
