StackWalker.getInstance(Set, int): This method is available in the java.lang.StackWalker class of Java.
Syntax:
StackWalker java.lang.StackWalker.getInstance(Set<Option> options, int estimateDepth)
This method takes two arguments. This method returns a StackWalker instance with the given options specifying the stack frame information it can access.
If the given options is empty, this StackWalker is configured to skip all hidden frames and no class reference is retained.
Parameters: Two parameters are required for this method.
options: stack walking options.
estimateDepth: Estimate number of stack frames to be traversed.
Returns: a StackWalker configured with the given options.
Throws:
1. IllegalArgumentException - if estimateDepth <= 0.
2. SecurityException - if a security manager exists and its checkPermission method denies access.
Approach 1: When no exception
Java
package com.StackWalker;import java.lang.StackWalker.Option;import java.util.HashSet;import java.util.Set;public class StackWalkergetInstance4 {public static void main(String[] args) {Set<Option> options = new HashSet<>();options.add(Option.RETAIN_CLASS_REFERENCE);options.add(Option.SHOW_HIDDEN_FRAMES);int estimateDepth = 5;StackWalker stackWalker =StackWalker.getInstance(options, estimateDepth);System.out.println(stackWalker);}}
Output:
java.lang.StackWalker@123772c4
Approach 2: IllegalArgumentException
Java
package com.StackWalker;import java.lang.StackWalker.Option;import java.util.HashSet;import java.util.Set;public class StackWalkergetInstance4 {public static void main(String[] args) {Set<Option> options = new HashSet<>();options.add(Option.RETAIN_CLASS_REFERENCE);options.add(Option.SHOW_HIDDEN_FRAMES);int estimateDepth = -5;StackWalker stackWalker =StackWalker.getInstance(options, estimateDepth);System.out.println(stackWalker);}}
Output:
Exception in thread "main" java.lang.IllegalArgumentException: estimateDepth must be > 0 at java.base/java.lang.StackWalker.getInstance(StackWalker.java:398) at com.StackWalker.StackWalkergetInstance4.main(StackWalkergetInstance4.java:14)
Some other methods of StackWalker
forEach(Consumer): This method performs the given action on each element of the StackFrame stream of the current thread, traversing from the top frame of the stack, which is the method calling this forEach method.
getCallerClass(): This method gets the Class object of the caller who invoked the method that invoked getCallerClass.
StackWalker.getInstance(): This method returns a StackWalker instance.
StackWalker.getInstance(Set): This method returns a StackWalker instance with the given options specifying the stack frame information it can access.
StackWalker.getInstance(Option): This method returns a StackWalker instance with the given option specifying the stack frame information it can access.
StackWalker.getInstance(Set, int): This method returns a StackWalker instance with the given options specifying the stack frame information it can access.
walk(Function): This method applies the given function to the stream of StackFramesfor the current thread, traversing from the top frame of the stack, which is the method calling this walk method.
No comments:
Post a Comment