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

Calling VarHandle.{access-mode} methods reflectively throws wrong exception

XMLWordPrintable

    • Icon: CSR CSR
    • Resolution: Approved
    • Icon: P3 P3
    • 24
    • core-libs
    • None
    • behavioral
    • minimal
    • The reflective invocations always terminate abnormally with an exception, so users likely rarely used it.
    • Java API
    • SE

      Summary

      Adjust the exception thrown by invoking VarHandle.{access-mode} methods reflectively to match the specification.

      Problem

      According to the API specification, invoking VarHandle.{access-mode} methods reflectively throws an UnsupportedOperationException (https://github.com/openjdk/jdk/blob/d7b941640638b35f9ac1ef11cd6bf6ccb795c29a/src/java.base/share/classes/java/lang/invoke/VarHandle.java#L435-L442). However, the currently thrown exception is an UnsatisfiedLinkError instead.

      Solution

      MethodHandle.invoke/invokeExact methods are equally special-cased in the specification. We can extend the existing solution that implements the behavior for MethodHandle to also cover VarHandle. This way, invoking the methods reflectively will throw an UnsupportedOperationException as specified.

      Specification

      No specification changes.

            hgreule Hannes Greule
            hgreule Hannes Greule
            Chen Liang, Jorn Vernee
            Votes:
            1 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: