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

Arrays.copyOf returns a value array where an Object[] is needed

XMLWordPrintable

    • Icon: Enhancement Enhancement
    • Resolution: Not an Issue
    • Icon: P3 P3
    • repo-valhalla
    • repo-valhalla
    • core-libs

      For the method Arrays.copyOf:

      static <T,U> T[] copyOf(U[] original, int newLength, Class<? extends T[]> newType)

      The erased type is

      static Object[] copyOf(Object[] original, int newLength, Class newType)

      However, the following code returns a value array, which is not supposed to be castable to an Object[].

      import java.util.Arrays;
      public class Test {
          static final value class V {
              public final int v1;
              private V () {v1 = 0;}
          }

          public static void main(String args[]) {
              Object oa[] = new Object[1];
              oa[0] = V.default;

              System.out.println(Arrays.copyOf(oa, oa.length, V[].class).getClass());
          }
      }

      $ java -XX:+EnableValhalla -cp . Test
      class [QTest$V;

            mchung Mandy Chung (Inactive)
            iklam Ioi Lam
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: