jdk/src/java.base/share/classes/java/lang/StackWalker.java
changeset 43712 5dfd0950317c
parent 38784 c0a88deb692a
child 43713 2a4f42ec3ceb
equal deleted inserted replaced
43619:dc9102c475f3 43712:5dfd0950317c
   463             return null;
   463             return null;
   464         }).walk();
   464         }).walk();
   465     }
   465     }
   466 
   466 
   467     /**
   467     /**
   468      * Gets the {@code Class} object of the caller invoking the method
   468      * Gets the {@code Class} object of the caller who invoked the method
   469      * that calls this {@code getCallerClass} method.
   469      * that invoked {@code getCallerClass}.
   470      *
   470      *
   471      * <p> Reflection frames, {@link java.lang.invoke.MethodHandle}, and
   471      * <p> Reflection frames, {@link java.lang.invoke.MethodHandle}, and
   472      * hidden frames are filtered regardless of the
   472      * hidden frames are filtered regardless of the
   473      * {@link Option#SHOW_REFLECT_FRAMES SHOW_REFLECT_FRAMES}
   473      * {@link Option#SHOW_REFLECT_FRAMES SHOW_REFLECT_FRAMES}
   474      * and {@link Option#SHOW_HIDDEN_FRAMES SHOW_HIDDEN_FRAMES} options
   474      * and {@link Option#SHOW_HIDDEN_FRAMES SHOW_HIDDEN_FRAMES} options
   475      * this {@code StackWalker} has been configured with.
   475      * this {@code StackWalker} has been configured with.
   476      *
   476      *
       
   477      * <p> This method should be called when a caller frame is present.  If
       
   478      * it is called from the bottom most frame on the stack,
       
   479      * {@code IllegalCallerException} will be thrown.
       
   480      *
   477      * <p> This method throws {@code UnsupportedOperationException}
   481      * <p> This method throws {@code UnsupportedOperationException}
   478      * if this {@code StackWalker} is not configured with the
   482      * if this {@code StackWalker} is not configured with the
   479      * {@link Option#RETAIN_CLASS_REFERENCE RETAIN_CLASS_REFERENCE} option.
   483      * {@link Option#RETAIN_CLASS_REFERENCE RETAIN_CLASS_REFERENCE} option.
   480      * This method should be called when a caller frame is present.  If
       
   481      * it is called from the last frame on the stack,
       
   482      * {@code IllegalStateException} will be thrown.
       
   483      *
   484      *
   484      * @apiNote
   485      * @apiNote
   485      * For example, {@code Util::getResourceBundle} loads a resource bundle
   486      * For example, {@code Util::getResourceBundle} loads a resource bundle
   486      * on behalf of the caller.  It calls this {@code getCallerClass} method
   487      * on behalf of the caller.  It invokes {@code getCallerClass} to identify
   487      * to find the method calling {@code Util::getResourceBundle} and uses the caller's
   488      * the class whose method called {@code Util::getResourceBundle}.
   488      * class loader to load the resource bundle. The caller class in this example
   489      * Then, it obtains the class loader of that class, and uses
   489      * is the {@code MyTool} class.
   490      * the class loader to load the resource bundle. The caller class
       
   491      * in this example is {@code MyTool}.
   490      *
   492      *
   491      * <pre>{@code
   493      * <pre>{@code
   492      * class Util {
   494      * class Util {
   493      *     private final StackWalker walker = StackWalker.getInstance(Option.RETAIN_CLASS_REFERENCE);
   495      *     private final StackWalker walker = StackWalker.getInstance(Option.RETAIN_CLASS_REFERENCE);
   494      *     public ResourceBundle getResourceBundle(String bundleName) {
   496      *     public ResourceBundle getResourceBundle(String bundleName) {
   515      *          .skip(2)
   517      *          .skip(2)
   516      *          .findFirst());
   518      *          .findFirst());
   517      * }</pre>
   519      * }</pre>
   518      *
   520      *
   519      * When the {@code getCallerClass} method is called from a method that
   521      * When the {@code getCallerClass} method is called from a method that
   520      * is the last frame on the stack,
   522      * is the bottom most frame on the stack,
   521      * for example, {@code static public void main} method launched by the
   523      * for example, {@code static public void main} method launched by the
   522      * {@code java} launcher, or a method invoked from a JNI attached thread,
   524      * {@code java} launcher, or a method invoked from a JNI attached thread,
   523      * {@code IllegalStateException} is thrown.
   525      * {@code IllegalCallerException} is thrown.
   524      *
   526      *
   525      * @return {@code Class} object of the caller's caller invoking this method.
   527      * @return {@code Class} object of the caller's caller invoking this method.
   526      *
   528      *
   527      * @throws UnsupportedOperationException if this {@code StackWalker}
   529      * @throws UnsupportedOperationException if this {@code StackWalker}
   528      *         is not configured with {@link Option#RETAIN_CLASS_REFERENCE
   530      *         is not configured with {@link Option#RETAIN_CLASS_REFERENCE
   529      *         Option.RETAIN_CLASS_REFERENCE}.
   531      *         Option.RETAIN_CLASS_REFERENCE}.
   530      * @throws IllegalStateException if there is no caller frame, i.e.
   532      * @throws IllegalCallerException if there is no caller frame, i.e.
   531      *         when this {@code getCallerClass} method is called from a method
   533      *         when this {@code getCallerClass} method is called from a method
   532      *         which is the last frame on the stack.
   534      *         which is the last frame on the stack.
   533      */
   535      */
   534     @CallerSensitive
   536     @CallerSensitive
   535     public Class<?> getCallerClass() {
   537     public Class<?> getCallerClass() {