< prev index next > src/java.base/share/classes/java/util/Arrays.java
Print this page
*/
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;
* 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();
+ Object tmp = null;
+ if (original.getClass() == newType && componentType.isValue()) {
+ tmp = ValueClass.copyOfSpecialArray((Object[])original, 0, newLength);
+ } else {
+ tmp = ((Object)newType == (Object)Object[].class)
+ ? new Object[newLength]
+ : Array.newInstance(componentType, newLength);
+ System.arraycopy(original, 0, tmp, 0,
+ Math.min(original.length, newLength));
+ }
@SuppressWarnings("unchecked")
- T[] copy = ((Object)newType == (Object)Object[].class)
- ? (T[]) new Object[newLength]
- : (T[]) Array.newInstance(newType.getComponentType(), newLength);
- System.arraycopy(original, 0, copy, 0,
- Math.min(original.length, newLength));
+ T[] copy = (T[])tmp;
return copy;
}
/**
* Copies the specified array, truncating or padding with zeros (if necessary)
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();
+ Object tmp = null;
+ if (original.getClass() == newType && componentType.isValue()) {
+ tmp = ValueClass.copyOfSpecialArray((Object[])original, from, to);
+ } else {
+ tmp = ((Object)newType == (Object)Object[].class)
+ ? new Object[newLength]
+ : Array.newInstance(componentType, newLength);
+ System.arraycopy(original, from, tmp, 0,
+ Math.min(original.length - from, newLength));
+ }
@SuppressWarnings("unchecked")
- T[] copy = ((Object)newType == (Object)Object[].class)
- ? (T[]) new Object[newLength]
- : (T[]) Array.newInstance(newType.getComponentType(), newLength);
- System.arraycopy(original, from, copy, 0,
- Math.min(original.length - from, newLength));
+ T[] copy = (T[])tmp;
return copy;
}
/**
* Copies the specified range of the specified array into a new array.
< prev index next >