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

Create an internal utility method for creating VarHandle instances

XMLWordPrintable

    • Icon: Enhancement Enhancement
    • Resolution: Fixed
    • Icon: P5 P5
    • 24
    • 24
    • core-libs
    • b17

      In over 70 cases in the JDK, we obtain a static VarHandle for some variables and need to go through tedious static initialization blocks. It would be better to provide a utility method that captures any exceptions and wraps them into an Error.

      Old:

          private static final VarHandle STATE;
          static {
              try {
                  MethodHandles.Lookup l = MethodHandles.lookup();
                  STATE = l.findVarHandle(Phaser.class, "state", long.class);
              } catch (ReflectiveOperationException e) {
                  throw new ExceptionInInitializerError(e);
              }


      New:

          private static final VarHandle STATE =
              XUtil.findVarHandleOrThrow(MethodHandles.lookup(), Phaser.class, "state", long.class);

      Old:

          private static final VarHandle RESULT;
          private static final VarHandle STACK;
          private static final VarHandle NEXT;
          static {
              try {
                  MethodHandles.Lookup l = MethodHandles.lookup();
                  RESULT = l.findVarHandle(CompletableFuture.class, "result", Object.class);
                  STACK = l.findVarHandle(CompletableFuture.class, "stack", Completion.class);
                  NEXT = l.findVarHandle(Completion.class, "next", Completion.class);
              } catch (ReflectiveOperationException e) {
                  throw new ExceptionInInitializerError(e);
              }
          }
      New:

          private static final VarHandle RESULT;
          private static final VarHandle STACK;
          private static final VarHandle NEXT;
          static {
              MethodHandles.Lookup l = MethodHandles.lookup();
              RESULT = XUtil.findVarHandleOrThrow(l, CompletableFuture.class, "result", Object.class);
              STACK = XUtil.findVarHandleOrThrow(l, CompletableFuture.class, "stack", Completion.class);
              NEXT = XUtil.findVarHandleOrThrow(l, Completion.class, "next", Completion.class);
          }


      The use of these handles is covered extensively by existing tests so I've marked this issue "noreg-other".

            pminborg Per-Ake Minborg
            pminborg Per-Ake Minborg
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: