getCallerClass(): This method is available in the java.lang.StackWalker class of Java.
Syntax:
Class<?> java.lang.StackWalker.getCallerClass()
This method gets the Class object of the caller who invoked the method that invoked getCallerClass.
This method filters reflection frames, java.lang.invoke.MethodHandle, and hidden frames regardless of the SHOW_REFLECT_FRAMESand SHOW_HIDDEN_FRAMES options this StackWalker has been configured with.
This method should be called when a caller frame is present. If it is called from the bottom most frame on the stack, IllegalCallerException will be thrown.
This method throws UnsupportedOperationExceptionif this StackWalker is not configured with the RETAIN_CLASS_REFERENCE option.
Parameters: NA
Returns: Class object of the caller's caller invoking this method.
Throws:
1. UnsupportedOperationException - if this StackWalkeris not configured with Option.RETAIN_CLASS_REFERENCE.
2. IllegalCallerException - if there is no caller frame, i.e.when this getCallerClass method is called from a method which is the last frame on the stack.
Approach 1: UnsupportedOperationException
Java
package com.StackWalker;import java.lang.StackWalker.Option;import java.util.HashSet;import java.util.Set;public class StackWalkergetCallerClass {public static void main(String[] args) {Set<Option> options = new HashSet<>();options.add(Option.SHOW_HIDDEN_FRAMES);StackWalker stackWalker =StackWalker.getInstance(options);System.out.println(stackWalker.getCallerClass());}}
Output:
Exception in thread "main" java.lang.UnsupportedOperationException: This stack walker does not have RETAIN_CLASS_REFERENCE access at java.base/java.lang.StackWalker.getCallerClass(StackWalker.java:596) at com.StackWalker.StackWalkergetCallerClass.main(StackWalkergetCallerClass.java:14)
Approach 2: IllegalCallerException
Java
package com.StackWalker;import java.lang.StackWalker.Option;import java.util.HashSet;import java.util.Set;public class StackWalkergetCallerClass {public static void main(String[] args) {Set<Option> options = new HashSet<>();options.add(Option.RETAIN_CLASS_REFERENCE);options.add(Option.SHOW_HIDDEN_FRAMES);StackWalker stackWalker =StackWalker.getInstance(options);System.out.println(stackWalker.getCallerClass());}}
Output:
Exception in thread "main" java.lang.IllegalCallerException: no caller frame at java.base/java.lang.StackStreamFactory$CallerClassFinder.consumeFrames(StackStreamFactory.java:689) at java.base/java.lang.StackStreamFactory$CallerClassFinder.consumeFrames(StackStreamFactory.java:612) at java.base/java.lang.StackStreamFactory$AbstractStackWalker.doStackWalk(StackStreamFactory.java:306) at java.base/java.lang.StackStreamFactory$AbstractStackWalker.callStackWalk(Native Method) at java.base/java.lang.StackStreamFactory$AbstractStackWalker.beginStackWalk(StackStreamFactory.java:370) at java.base/java.lang.StackStreamFactory$AbstractStackWalker.walk(StackStreamFactory.java:243) at java.base/java.lang.StackStreamFactory$CallerClassFinder.findCaller(StackStreamFactory.java:670) at java.base/java.lang.StackWalker.getCallerClass(StackWalker.java:600) at com.StackWalker.StackWalkergetCallerClass.main(StackWalkergetCallerClass.java:15)
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