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

byteArrayViewVarHandle should throw ArrayIndexOutOfBoundsException

XMLWordPrintable

    • Icon: CSR CSR
    • Resolution: Approved
    • Icon: P4 P4
    • 17
    • core-libs
    • None
    • behavioral
    • minimal
    • Hide
      This specialize the exception thrown from a more generic type to a more specialized type, which means most code written to expect IndexOutOfBoundsException should work as long as not doing exact class matching (e.getClass() == IndexOutOfBoundsException.class)
      Show
      This specialize the exception thrown from a more generic type to a more specialized type, which means most code written to expect IndexOutOfBoundsException should work as long as not doing exact class matching (e.getClass() == IndexOutOfBoundsException.class)
    • Java API

      Summary

      Change so that out-of-bounds invocations of MethodHandles.byteArrayViewVarHandle throw ArrayIndexOutOfBoundsException rather than the more generic IndexOutOfBoundsException.

      Problem

      Code doing array operations in a way such that using MethodHandles.byteArrayViewVarHandle would be a good fit will currently have to accept that the specific AIOOBE thrown natively on out-of-bounds errors on arrays is replaced by the more generic IOOBE. This might be a challenge for picking up such VarHandles due creating a subtle behavioral mismatch, and adapting the code to throw the specific and more naturally fitting AIOOBE would ease migration.

      Solution

      Change the implementation internally so that AIOOBE is thrown when accessing an out-of-bounds index of a byte array via a MethodHandles.byteArrayViewVarHandle.

      Specification

      The specification of MethodHandles.byteArrayViewVarHandle is updated to specify that ArrayIndexOutOfBoundsException will be thrown:

           * <p>
           * Access of bytes at a given index will result in an
           * {@code ArrayIndexOutOfBoundsException} if the index is less than {@code 0}
           * or greater than the {@code byte[]} array length minus the size (in bytes)
           * of {@code T}.
           * <p>

            redestad Claes Redestad
            redestad Claes Redestad
            Jorn Vernee, Paul Sandoz
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: