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.
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());
null argument to a
constructor or method in this StackWalker class
will cause a NullPointerException
to be thrown.
| Modifier and Type | Class and Description |
|---|---|
| pack-priv static enum | |
| public static enum | StackWalker.
Stack walker option to configure the stack frame
information obtained by a |
| public static interface | StackWalker.
A |
| Modifier and Type | Field and Description |
|---|---|
| private final Continuation | |
| private final ContinuationScope | |
| pack-priv static final EnumSet | |
| private static final StackWalker | |
| private final int | |
| private final StackWalker. | |
| private final Set | |
| pack-priv final boolean |
| Access | Constructor and Description |
|---|---|
| private | |
| private | |
| private | StackWalker(EnumSet<StackWalker.
|
| Modifier and Type | Method and Description |
|---|---|
| pack-priv int | |
| public void | forEach(Consumer<? super StackWalker.
an action to be performed on each action)StackFrame
of the stack of the current threadPerforms the given action on each element of |
| public Class | Returns: Class object of the caller's caller invoking this method.Gets the |
| pack-priv Continuation | |
| pack-priv ContinuationScope | |
| public static StackWalker | |
| public static StackWalker | Returns: aStackWalker configured with the given optionReturns a |
| public static StackWalker | Returns: aStackWalker configured with the given optionsReturns a |
| public static StackWalker | Returns: aStackWalker configured with the given optionsEstimate number of stack frames to be traversed. estimateDepth)Returns a |
| pack-priv boolean | |
| pack-priv boolean | |
| pack-priv static StackWalker | |
| pack-priv static StackWalker | |
| pack-priv static StackWalker | newInstance(Set<StackWalker.
|
| pack-priv static StackWalker | newInstance(Set<StackWalker.
|
| private static EnumSet | |
| public < The type of the result of applying the function to the
stream of stack frame. T> T | Returns: the result of applying the function to the stream of stack frame.a function that takes a stream of
stack frames and returns a result. function)Applies the given function to the stream of |
| continuation | back to summary |
|---|---|
| private final Continuation continuation | |
| contScope | back to summary |
|---|---|
| private final ContinuationScope contScope | |
| DEFAULT_EMPTY_OPTION | back to summary |
|---|---|
| pack-priv static final EnumSet<StackWalker. | |
| DEFAULT_WALKER | back to summary |
|---|---|
| private static final StackWalker DEFAULT_WALKER | |
| estimateDepth | back to summary |
|---|---|
| private final int estimateDepth | |
| extendedOption | back to summary |
|---|---|
| private final StackWalker. | |
| options | back to summary |
|---|---|
| private final Set<StackWalker. | |
| retainClassRef | back to summary |
|---|---|
| pack-priv final boolean retainClassRef | |
| StackWalker | back to summary |
|---|---|
| private StackWalker(EnumSet<StackWalker. | |
| StackWalker | back to summary |
|---|---|
| private StackWalker(EnumSet<StackWalker. | |
| StackWalker | back to summary |
|---|---|
| private StackWalker(EnumSet<StackWalker. | |
| estimateDepth | back to summary |
|---|---|
| pack-priv int estimateDepth() | |
| forEach | back to summary |
|---|---|
| public void forEach(Consumer<? super StackWalker. Performs the given action on each element of This method is equivalent to calling
| |
| getCallerClass | back to summary |
|---|---|
| public Class Gets the This method filters reflection
frames, This method should be called when a caller frame is present. If
it is called from the bottom most frame on the stack,
This method throws API Note For example, class Util { private final StackWalker walker = StackWalker.getInstance(Set.of(Option.DROP_METHOD_INFO, Option.RETAIN_CLASS_REFERENCE)); public ResourceBundle getResourceBundle(String bundleName) { Class<?> caller = walker.getCallerClass(); return ResourceBundle.getBundle(bundleName, Locale.getDefault(), caller.getClassLoader()); } } class MyTool { private final Util util = new Util(); private void init() { ResourceBundle rb = util.getResourceBundle("mybundle"); } } walk method is as follows
(filtering the reflection frames, MethodHandle and hidden frames
not shown below):
Optional<Class<?>> caller = walker.walk(s -> s.map(StackFrame::getDeclaringClass) .skip(2) .findFirst()); getCallerClass method is called from a method that
is the bottom most frame on the stack,
for example, static public void main method launched by the
java launcher, or a method invoked from a JNI attached thread,
IllegalCallerException is thrown.
| |
| getContinuation | back to summary |
|---|---|
| pack-priv Continuation getContinuation() | |
| getContScope | back to summary |
|---|---|
| pack-priv ContinuationScope getContScope() | |
| getInstance | back to summary |
|---|---|
| public static StackWalker getInstance() Returns a This
| |
| getInstance | back to summary |
|---|---|
| public static StackWalker getInstance(StackWalker. Returns a
| |
| getInstance | back to summary |
|---|---|
| public static StackWalker getInstance(Set<StackWalker. Returns a
If the given
| |
| getInstance | back to summary |
|---|---|
| public static StackWalker getInstance(Set<StackWalker. Returns a
If the given
The
| |
| hasLocalsOperandsOption | back to summary |
|---|---|
| pack-priv boolean hasLocalsOperandsOption() | |
| hasOption | back to summary |
|---|---|
| pack-priv boolean hasOption(StackWalker. | |
| newInstance | back to summary |
|---|---|
| pack-priv static StackWalker newInstance(Set<StackWalker. | |
| newInstance | back to summary |
|---|---|
| pack-priv static StackWalker newInstance(Set<StackWalker. | |
| newInstance | back to summary |
|---|---|
| pack-priv static StackWalker newInstance(Set<StackWalker. | |
| newInstance | back to summary |
|---|---|
| pack-priv static StackWalker newInstance(Set<StackWalker. | |
| toEnumSet | back to summary |
|---|---|
| private static EnumSet | |
| walk | back to summary |
|---|---|
| public <T> T walk(Function<? super Stream<StackWalker. Applies the given function to the stream of The API Note For example, to find the first 10 calling frames, first skipping those frames
whose declaring class is in package List<StackFrame> frames = StackWalker.getInstance().walk(s -> s.dropWhile(f -> f.getClassName().startsWith("com.foo.")) .limit(10) .toList()); This method takes a Parallel execution is effectively disabled and stream pipeline execution will only occur on the current thread. Implementation Note The implementation stabilizes the stack by anchoring a frame
specific to the stack walking and ensures that the stack walking is
performed above the anchored frame. When the stream object is closed or
being reused,
| |
| Modifier and Type | Field and Description |
|---|---|
| public static final StackWalker. | LOCALS_AND_OPERANDS
Obtain monitors, locals and operands. |
| Access | Constructor and Description |
|---|---|
| private |
| Modifier and Type | Method and Description |
|---|---|
| public static StackWalker. | |
| public static StackWalker. |
| LOCALS_AND_OPERANDS | back to summary |
|---|---|
| public static final StackWalker. Obtain monitors, locals and operands. | |
| ExtendedOption | back to summary |
|---|---|
| private ExtendedOption() | |
| valueOf | back to summary |
|---|---|
| public static StackWalker. | |
| values | back to summary |
|---|---|
| public static StackWalker. | |
StackWalker.
| Modifier and Type | Field and Description |
|---|---|
| public static final StackWalker. | DROP_METHOD_INFO
Drops the method information from |
| public static final StackWalker. | RETAIN_CLASS_REFERENCE
Retains |
| public static final StackWalker. | SHOW_HIDDEN_FRAMES
Shows all hidden frames. |
| public static final StackWalker. | SHOW_REFLECT_FRAMES
Shows all reflection frames. |
| Access | Constructor and Description |
|---|---|
| private |
| Modifier and Type | Method and Description |
|---|---|
| public static StackWalker. | |
| public static StackWalker. |
| DROP_METHOD_INFO | back to summary |
|---|---|
| public static final StackWalker. Drops the method information from A
| |
| RETAIN_CLASS_REFERENCE | back to summary |
|---|---|
| public static final StackWalker. Retains A | |
| SHOW_HIDDEN_FRAMES | back to summary |
|---|---|
| public static final StackWalker. Shows all hidden frames. A Java Virtual Machine implementation may hide implementation
specific frames in addition to reflection frames. A | |
| SHOW_REFLECT_FRAMES | back to summary |
|---|---|
| public static final StackWalker. Shows all reflection frames. By default, reflection frames are hidden. A The API Note This option includes the stack frames representing the invocation of
| |
| Option | back to summary |
|---|---|
| private Option() | |
| valueOf | back to summary |
|---|---|
| public static StackWalker. | |
| values | back to summary |
|---|---|
| public static StackWalker. | |
StackFrame object represents a method invocation returned by
StackWalker.
Stack walker options configure the stack
frame information obtained by a StackWalker.
If the stack walker is configured with DROP_METHOD_INFO option, method information such as
the method name,
the line number,
the bytecode index, etc
will be dropped.
If the stack walker is configured with RETAIN_CLASS_REFERENCE option, the Class object
will be retained for access.
| Modifier and Type | Method and Description |
|---|---|
| public int | Returns: the index to the code array of theCode attribute
containing the execution point represented by this stack frame,
or a negative number if the method is native.Returns the index to the code array of the |
| public String | Returns: the binary name of the declaring class of the method represented by this stack frameReturns the binary name of the declaring class of the method represented by this stack frame. |
| public Class | Returns: the declaringClass for the method represented by
this stack frameReturns the declaring |
| public default String | Returns: the descriptor of the method represented by this stack frameReturns the descriptor of the method represented by this stack frame as defined by The Java Virtual Machine Specification. |
| public String | Returns: the name of the file containing the execution point represented by this stack frame, ornull if
this information is unavailable.Returns the name of the source file containing the execution point represented by this stack frame. |
| public int | Returns: the line number of the source line containing the execution point represented by this stack frame, or a negative number if this information is unavailable.Returns the line number of the source line containing the execution point represented by this stack frame. |
| public String | Returns: the name of the method represented by this stack frameReturns the name of the method represented by this stack frame. |
| public default MethodType | Returns: theMethodType of the method represented by this stack frameReturns the |
| public boolean | Returns: true if the method containing the execution point
represented by this stack frame is a native methodReturns |
| public StackTraceElement | Returns: StackTraceElement for this stack frameReturns |
| getByteCodeIndex | back to summary |
|---|---|
| public int getByteCodeIndex() Returns the index to the code array of the
| |
| getClassName | back to summary |
|---|---|
| public String getClassName() Returns the binary name of the declaring class of the method represented by this stack frame.
| |
| getDeclaringClass | back to summary |
|---|---|
| public Class Returns the declaring
| |
| getDescriptor | back to summary |
|---|---|
| public default String getDescriptor() Returns the descriptor of the method represented by this stack frame as defined by The Java Virtual Machine Specification. Implementation Specification The default implementation throws
| |
| getFileName | back to summary |
|---|---|
| public String getFileName() Returns the name of the source file containing the execution point
represented by this stack frame. Generally, this corresponds
to the
| |
| getLineNumber | back to summary |
|---|---|
| public int getLineNumber() Returns the line number of the source line containing the execution
point represented by this stack frame. Generally, this is
derived from the
| |
| getMethodName | back to summary |
|---|---|
| public String getMethodName() Returns the name of the method represented by this stack frame.
| |
| getMethodType | back to summary |
|---|---|
| public default MethodType getMethodType() Returns the Implementation Specification The default implementation throws
| |
| isNativeMethod | back to summary |
|---|---|
| public boolean isNativeMethod() Returns
| |
| toStackTraceElement | back to summary |
|---|---|
| public StackTraceElement toStackTraceElement() Returns
| |