Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8311303

StackWalker.getCallerClass() can return false results if running with -XX:+ShowHiddenFrames

    XMLWordPrintable

Details

    • Bug
    • Resolution: Unresolved
    • P4
    • None
    • 11, 17
    • core-libs

    Description

      The following small test program will a false result for `StackWalker.getCallerClass()` if running with `-XX:+ShowHiddenFrames` (i.e. `class InjectedInvoker/0x0000000800062440` instead of `class GetCallerClass`):

      ```
      import java.lang.invoke.MethodHandle;
      import java.lang.invoke.MethodHandles;
      import java.lang.invoke.MethodType;
      import java.lang.reflect.Method;
      import java.util.Set;

      public class GetCallerClass {
          private static Method gcc;

          static {
              try {
                  gcc = GetCallerClass.class.getDeclaredMethod("getCallerClass");
              } catch (Exception ex) {
                  System.out.println(ex);
              }
          }

          private static final StackWalker WALKER = StackWalker.getInstance(Set.of(StackWalker.Option.RETAIN_CLASS_REFERENCE), 3);
          public static void getCallerClass() {
              System.out.println(WALKER.getCallerClass());
          }

          public static void main(String[] args) throws Throwable {
              MethodType mt = MethodType.methodType(Object.class, Object.class, Object[].class);
              MethodHandle mh =
                  MethodHandles.
                  lookup().
                  findVirtual(Method.class, "invoke", mt).
                  bindTo(gcc);
              mh.invoke(null, null);
          }
      }
      ```

      Attachments

        Activity

          People

            simonis Volker Simonis
            simonis Volker Simonis
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated: