StackWalker (Java SE 9 & JDK 9 ) (original) (raw)

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.
The stack walking options of aStackWalker determines the information ofStackFrame objects to be returned. By default, stack frames of the reflection API and implementation classes are hidden and StackFrames have the class name and method name available but not the Class reference.
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 a known list of implementation class:

     StackWalker walker = StackWalker.getInstance(Option.RETAIN_CLASS_REFERENCE);  
     Optional<Class<?>> callerClass = walker.walk(s ->  
         s.map(StackFrame::getDeclaringClass)  
          .filter(interestingClasses::contains)  
          .findFirst());  
   

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

     List<StackFrame> stack = StackWalker.getInstance().walk(s ->  
         s.limit(10).collect(Collectors.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

Report a bug or suggest an enhancement
For further API reference and developer documentation see the Java SE Documentation, which contains more detailed, developer-targeted descriptions with conceptual overviews, definitions of terms, workarounds, and working code examples.
Java is a trademark or registered trademark of Oracle and/or its affiliates in the US and other countries.
Copyright © 1993, 2017, Oracle and/or its affiliates, 500 Oracle Parkway, Redwood Shores, CA 94065 USA.
All rights reserved. Use is subject to license terms and the documentation redistribution policy.