Module java.base
Package java.lang

Class StackWalker

java.lang.Object
java.lang.StackWalker

public final class StackWalker extends Object
A stack walker.

The walk method opens a sequential stream of StackFrames for the current thread and then applies the given function to walk the StackFrame stream. The stream reports stack frame elements in order, from the top most frame that represents the execution point at which the stack was generated to the bottom most frame. The StackFrame stream is closed when the walk method returns. If an attempt is made to reuse the closed stream, IllegalStateException will be thrown.

Stack walker options configure the stack frame information obtained by a StackWalker. By default, the class name and method information are collected but not the Class reference. The method information can be dropped via the DROP_METHOD_INFO option. The Class object can be retained for access via the RETAIN_CLASS_REFERENCE option. Stack frames of the reflection API and implementation classes are hidden by default.

StackWalker is thread-safe. Multiple threads can share a single StackWalker object to traverse its own stack. A permission check is performed when a StackWalker is created, according to the options it requests. No further permission check is done at stack walking time.

API Note:
Examples

1. To find the first caller filtering out a known list of implementation class:

    StackWalker walker = StackWalker.getInstance(Set.of(Option.DROP_METHOD_INFO, Option.RETAIN_CLASS_REFERENCE));
    Optional<Class<?>> callerClass = walker.walk(s ->
            s.map(StackFrame::getDeclaringClass)
             .filter(Predicate.not(implClasses::contains))
             .findFirst());

2. To snapshot the top 10 stack frames of the current thread,

    List<StackFrame> stack = StackWalker.getInstance().walk(s -> s.limit(10).toList());
Unless otherwise noted, passing a null argument to a constructor or method in this StackWalker class will cause a NullPointerException to be thrown.
Since:
9