< prev index next >

src/java.base/share/classes/java/util/Arrays.java

Print this page
@@ -24,10 +24,11 @@
   */
  
  package java.util;
  
  import jdk.internal.util.ArraysSupport;
+ import jdk.internal.value.ValueClass;
  import jdk.internal.vm.annotation.IntrinsicCandidate;
  
  import java.io.Serializable;
  import java.lang.reflect.Array;
  import java.util.concurrent.ForkJoinPool;

@@ -3505,14 +3506,17 @@
       *     an array of class {@code newType}
       * @since 1.6
       */
      @IntrinsicCandidate
      public static <T,U> T[] copyOf(U[] original, int newLength, Class<? extends T[]> newType) {
+         Class<?> componentType = newType.getComponentType();
          @SuppressWarnings("unchecked")
          T[] copy = ((Object)newType == (Object)Object[].class)
              ? (T[]) new Object[newLength]
-             : (T[]) Array.newInstance(newType.getComponentType(), newLength);
+             : (original.getClass() == newType && componentType.isValue() && ValueClass.isNullRestrictedArray(original)
+                     ? (T[]) ValueClass.newNullRestrictedArray(newType.getComponentType(), newLength)
+                     : (T[]) Array.newInstance(componentType, newLength));
          System.arraycopy(original, 0, copy, 0,
                           Math.min(original.length, newLength));
          return copy;
      }
  

@@ -3804,14 +3808,17 @@
      public static <T,U> T[] copyOfRange(U[] original, int from, int to, Class<? extends T[]> newType) {
          int newLength = to - from;
          if (newLength < 0) {
              throw new IllegalArgumentException(from + " > " + to);
          }
+         Class<?> componentType = newType.getComponentType();
          @SuppressWarnings("unchecked")
          T[] copy = ((Object)newType == (Object)Object[].class)
              ? (T[]) new Object[newLength]
-             : (T[]) Array.newInstance(newType.getComponentType(), newLength);
+             : (original.getClass() == newType && componentType.isValue() && ValueClass.isNullRestrictedArray(original)
+                     ? (T[]) ValueClass.newNullRestrictedArray(componentType, newLength)
+                     : (T[]) Array.newInstance(componentType, newLength));
          System.arraycopy(original, from, copy, 0,
                           Math.min(original.length - from, newLength));
          return copy;
      }
  
< prev index next >