- java.lang.Object
-
- java.lang.StackWalker
-
public final class StackWalker extends Object
This provides a facility for iterating over the call stack of the current thread. A StackWalker object may be used multiple times by different threads, but it will represent the state of the current thread at the time the stack is walked. A StackWalker may be provided with one or more Option settings to include information and stack frames such as reflection methods, hidden frames, and Class objects.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
StackWalker.Option
Selects what type of stack and method information is provided by the StackWalkerstatic interface
StackWalker.StackFrame
Contains information about the StackWalker's current stack frame.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description void
forEach(Consumer<? super StackWalker.StackFrame> action)
Class<?>
getCallerClass()
Get the caller of the caller of this function, eliding any reflection or hidden frames.static StackWalker
getInstance()
Factory method to create a StackWalker instance with no options set.static StackWalker
getInstance(StackWalker.Option option)
Factory method to create a StackWalker with one option.static StackWalker
getInstance(Set<StackWalker.Option> options)
Factory method to create a StackWalker with any number of options.static StackWalker
getInstance(Set<StackWalker.Option> options, int estimatedDepth)
Factory method to create a StackWalker.<T> T
walk(Function<? super Stream<StackWalker.StackFrame>,? extends T> function)
Traverse the calling thread's stack at the time this method is called and applyfunction
to each stack frame.
-
-
-
Method Detail
-
getInstance
public static StackWalker getInstance()
Factory method to create a StackWalker instance with no options set.- Returns:
- StackWalker StackWalker object
-
getInstance
public static StackWalker getInstance(StackWalker.Option option)
Factory method to create a StackWalker with one option. This is provided for the case where only a single option is required.- Parameters:
option
- select the type of information to include- Returns:
- StackWalker instance configured with the value of option
- Throws:
SecurityException
- if option is RETAIN_CLASS_REFERENCE and the security manager check fails.
-
getInstance
public static StackWalker getInstance(Set<StackWalker.Option> options)
Factory method to create a StackWalker with any number of options.- Parameters:
options
- select the types of information to include.- Returns:
- StackWalker instance configured with the given options
- Throws:
SecurityException
- if RETAIN_CLASS_REFERENCE is requested and not permitted by the security manager
-
getInstance
public static StackWalker getInstance(Set<StackWalker.Option> options, int estimatedDepth)
Factory method to create a StackWalker.- Parameters:
options
- select the types of information to include.estimatedDepth
- Hint for the size of buffer to use. Must be 1 or greater.- Returns:
- StackWalker instance with the given options specifying the stack frame information it can access.
- Throws:
SecurityException
- if RETAIN_CLASS_REFERENCE is requested and not permitted by the security manager
-
forEach
public void forEach(Consumer<? super StackWalker.StackFrame> action)
- Parameters:
action
-Consumer
object Iterate over the stack from top to bottom and apply theConsumer
to eachStackWalker.StackFrame
-
getCallerClass
public Class<?> getCallerClass()
Get the caller of the caller of this function, eliding any reflection or hidden frames.- Returns:
- Class object for the method calling the current method.
- Throws:
UnsupportedOperationException
- if the StackWalker was not created withStackWalker.Option.RETAIN_CLASS_REFERENCE
IllegalStateException
- if the caller is at the bottom of the stack.
-
walk
public <T> T walk(Function<? super Stream<StackWalker.StackFrame>,? extends T> function)
Traverse the calling thread's stack at the time this method is called and applyfunction
to each stack frame.- Type Parameters:
T
- the type of the return value from applying function to the stream- Parameters:
function
- operation to apply to the stream- Returns:
- the value returned by
function
.
-
-