jaroslav@475: /* jaroslav@475: * Copyright (c) 1996, 2006, Oracle and/or its affiliates. All rights reserved. jaroslav@475: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. jaroslav@475: * jaroslav@475: * This code is free software; you can redistribute it and/or modify it jaroslav@475: * under the terms of the GNU General Public License version 2 only, as jaroslav@475: * published by the Free Software Foundation. Oracle designates this jaroslav@475: * particular file as subject to the "Classpath" exception as provided jaroslav@475: * by Oracle in the LICENSE file that accompanied this code. jaroslav@475: * jaroslav@475: * This code is distributed in the hope that it will be useful, but WITHOUT jaroslav@475: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or jaroslav@475: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License jaroslav@475: * version 2 for more details (a copy is included in the LICENSE file that jaroslav@475: * accompanied this code). jaroslav@475: * jaroslav@475: * You should have received a copy of the GNU General Public License version jaroslav@475: * 2 along with this work; if not, write to the Free Software Foundation, jaroslav@475: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. jaroslav@475: * jaroslav@475: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA jaroslav@475: * or visit www.oracle.com if you need additional information or have any jaroslav@475: * questions. jaroslav@475: */ jaroslav@475: jaroslav@475: package java.lang.reflect; jaroslav@475: jaroslav@477: import org.apidesign.bck2brwsr.core.JavaScriptBody; jaroslav@479: import org.apidesign.bck2brwsr.core.JavaScriptPrototype; jaroslav@477: jaroslav@475: /** jaroslav@475: * The {@code Array} class provides static methods to dynamically create and jaroslav@475: * access Java arrays. jaroslav@475: * jaroslav@475: *

{@code Array} permits widening conversions to occur during a get or set jaroslav@475: * operation, but throws an {@code IllegalArgumentException} if a narrowing jaroslav@475: * conversion would occur. jaroslav@475: * jaroslav@475: * @author Nakul Saraiya jaroslav@475: */ jaroslav@479: @JavaScriptPrototype(prototype = "new Array", container = "Array.prototype") jaroslav@475: public final jaroslav@475: class Array { jaroslav@475: jaroslav@475: /** jaroslav@475: * Constructor. Class Array is not instantiable. jaroslav@475: */ jaroslav@475: private Array() {} jaroslav@475: jaroslav@475: /** jaroslav@475: * Creates a new array with the specified component type and jaroslav@475: * length. jaroslav@475: * Invoking this method is equivalent to creating an array jaroslav@475: * as follows: jaroslav@475: *

jaroslav@475: *
jaroslav@475:      * int[] x = {length};
jaroslav@475:      * Array.newInstance(componentType, x);
jaroslav@475:      * 
jaroslav@475: *
jaroslav@475: * jaroslav@475: * @param componentType the {@code Class} object representing the jaroslav@475: * component type of the new array jaroslav@475: * @param length the length of the new array jaroslav@475: * @return the new array jaroslav@475: * @exception NullPointerException if the specified jaroslav@475: * {@code componentType} parameter is null jaroslav@475: * @exception IllegalArgumentException if componentType is {@link Void#TYPE} jaroslav@475: * @exception NegativeArraySizeException if the specified {@code length} jaroslav@475: * is negative jaroslav@475: */ jaroslav@475: public static Object newInstance(Class componentType, int length) jaroslav@477: throws NegativeArraySizeException { jaroslav@477: if (length < 0) { jaroslav@477: throw new NegativeArraySizeException(); jaroslav@477: } jaroslav@477: String sig = findSignature(componentType); jaroslav@477: return newArray(componentType.isPrimitive(), sig, length); jaroslav@475: } jaroslav@477: jaroslav@477: private static String findSignature(Class type) { jaroslav@477: if (type == Integer.TYPE) { jaroslav@477: return "[I"; jaroslav@477: } jaroslav@477: if (type == Long.TYPE) { jaroslav@477: return "[J"; jaroslav@477: } jaroslav@477: if (type == Double.TYPE) { jaroslav@477: return "[D"; jaroslav@477: } jaroslav@477: if (type == Float.TYPE) { jaroslav@477: return "[F"; jaroslav@477: } jaroslav@477: if (type == Byte.TYPE) { jaroslav@477: return "[B"; jaroslav@477: } jaroslav@477: if (type == Boolean.TYPE) { jaroslav@477: return "[Z"; jaroslav@477: } jaroslav@477: if (type == Short.TYPE) { jaroslav@477: return "[S"; jaroslav@477: } jaroslav@477: if (type == Character.TYPE) { jaroslav@477: return "[C"; jaroslav@477: } jaroslav@477: if (type.getName().equals("void")) { jaroslav@477: throw new IllegalStateException("Can't create array for " + type); jaroslav@477: } jaroslav@477: return "[L" + type.getName() + ";"; jaroslav@477: } jaroslav@475: /** jaroslav@475: * Creates a new array jaroslav@475: * with the specified component type and dimensions. jaroslav@475: * If {@code componentType} jaroslav@475: * represents a non-array class or interface, the new array jaroslav@475: * has {@code dimensions.length} dimensions and jaroslav@475: * {@code componentType} as its component type. If jaroslav@475: * {@code componentType} represents an array class, the jaroslav@475: * number of dimensions of the new array is equal to the sum jaroslav@475: * of {@code dimensions.length} and the number of jaroslav@475: * dimensions of {@code componentType}. In this case, the jaroslav@475: * component type of the new array is the component type of jaroslav@475: * {@code componentType}. jaroslav@475: * jaroslav@475: *

The number of dimensions of the new array must not jaroslav@475: * exceed the number of array dimensions supported by the jaroslav@475: * implementation (typically 255). jaroslav@475: * jaroslav@475: * @param componentType the {@code Class} object representing the component jaroslav@475: * type of the new array jaroslav@475: * @param dimensions an array of {@code int} representing the dimensions of jaroslav@475: * the new array jaroslav@475: * @return the new array jaroslav@475: * @exception NullPointerException if the specified jaroslav@475: * {@code componentType} argument is null jaroslav@475: * @exception IllegalArgumentException if the specified {@code dimensions} jaroslav@475: * argument is a zero-dimensional array, or if the number of jaroslav@475: * requested dimensions exceeds the limit on the number of array dimensions jaroslav@475: * supported by the implementation (typically 255), or if componentType jaroslav@475: * is {@link Void#TYPE}. jaroslav@475: * @exception NegativeArraySizeException if any of the components in jaroslav@475: * the specified {@code dimensions} argument is negative. jaroslav@475: */ jaroslav@475: public static Object newInstance(Class componentType, int... dimensions) jaroslav@475: throws IllegalArgumentException, NegativeArraySizeException { jaroslav@480: StringBuilder sig = new StringBuilder(); jaroslav@480: for (int i = 1; i < dimensions.length; i++) { jaroslav@480: sig.append('['); jaroslav@480: } jaroslav@480: sig.append(findSignature(componentType)); jaroslav@480: return multiNewArray(sig.toString(), dimensions, 0); jaroslav@475: } jaroslav@475: jaroslav@475: /** jaroslav@475: * Returns the length of the specified array object, as an {@code int}. jaroslav@475: * jaroslav@475: * @param array the array jaroslav@475: * @return the length of the array jaroslav@475: * @exception IllegalArgumentException if the object argument is not jaroslav@475: * an array jaroslav@475: */ jaroslav@482: public static int getLength(Object array) jaroslav@482: throws IllegalArgumentException { jaroslav@482: if (!array.getClass().isArray()) { jaroslav@483: throw new IllegalArgumentException("Argument is not an array"); jaroslav@482: } jaroslav@482: return length(array); jaroslav@482: } jaroslav@482: jaroslav@482: @JavaScriptBody(args = { "arr" }, body = "return arr.length;") jaroslav@482: private static native int length(Object arr); jaroslav@475: jaroslav@475: /** jaroslav@475: * Returns the value of the indexed component in the specified jaroslav@475: * array object. The value is automatically wrapped in an object jaroslav@475: * if it has a primitive type. jaroslav@475: * jaroslav@475: * @param array the array jaroslav@475: * @param index the index jaroslav@475: * @return the (possibly wrapped) value of the indexed component in jaroslav@475: * the specified array jaroslav@475: * @exception NullPointerException If the specified object is null jaroslav@475: * @exception IllegalArgumentException If the specified object is not jaroslav@475: * an array jaroslav@475: * @exception ArrayIndexOutOfBoundsException If the specified {@code index} jaroslav@475: * argument is negative, or if it is greater than or equal to the jaroslav@475: * length of the specified array jaroslav@475: */ jaroslav@475: public static native Object get(Object array, int index) jaroslav@475: throws IllegalArgumentException, ArrayIndexOutOfBoundsException; jaroslav@475: jaroslav@475: /** jaroslav@475: * Returns the value of the indexed component in the specified jaroslav@475: * array object, as a {@code boolean}. jaroslav@475: * jaroslav@475: * @param array the array jaroslav@475: * @param index the index jaroslav@475: * @return the value of the indexed component in the specified array jaroslav@475: * @exception NullPointerException If the specified object is null jaroslav@475: * @exception IllegalArgumentException If the specified object is not jaroslav@475: * an array, or if the indexed element cannot be converted to the jaroslav@475: * return type by an identity or widening conversion jaroslav@475: * @exception ArrayIndexOutOfBoundsException If the specified {@code index} jaroslav@475: * argument is negative, or if it is greater than or equal to the jaroslav@475: * length of the specified array jaroslav@475: * @see Array#get jaroslav@475: */ jaroslav@475: public static native boolean getBoolean(Object array, int index) jaroslav@475: throws IllegalArgumentException, ArrayIndexOutOfBoundsException; jaroslav@475: jaroslav@475: /** jaroslav@475: * Returns the value of the indexed component in the specified jaroslav@475: * array object, as a {@code byte}. jaroslav@475: * jaroslav@475: * @param array the array jaroslav@475: * @param index the index jaroslav@475: * @return the value of the indexed component in the specified array jaroslav@475: * @exception NullPointerException If the specified object is null jaroslav@475: * @exception IllegalArgumentException If the specified object is not jaroslav@475: * an array, or if the indexed element cannot be converted to the jaroslav@475: * return type by an identity or widening conversion jaroslav@475: * @exception ArrayIndexOutOfBoundsException If the specified {@code index} jaroslav@475: * argument is negative, or if it is greater than or equal to the jaroslav@475: * length of the specified array jaroslav@475: * @see Array#get jaroslav@475: */ jaroslav@483: public static byte getByte(Object array, int index) jaroslav@483: throws IllegalArgumentException, ArrayIndexOutOfBoundsException { jaroslav@483: if (array.getClass().getComponentType() != Byte.TYPE) { jaroslav@483: throw new IllegalArgumentException(); jaroslav@483: } jaroslav@483: byte[] arr = (byte[]) array; jaroslav@483: return arr[index]; jaroslav@483: } jaroslav@475: jaroslav@475: /** jaroslav@475: * Returns the value of the indexed component in the specified jaroslav@475: * array object, as a {@code char}. jaroslav@475: * jaroslav@475: * @param array the array jaroslav@475: * @param index the index jaroslav@475: * @return the value of the indexed component in the specified array jaroslav@475: * @exception NullPointerException If the specified object is null jaroslav@475: * @exception IllegalArgumentException If the specified object is not jaroslav@475: * an array, or if the indexed element cannot be converted to the jaroslav@475: * return type by an identity or widening conversion jaroslav@475: * @exception ArrayIndexOutOfBoundsException If the specified {@code index} jaroslav@475: * argument is negative, or if it is greater than or equal to the jaroslav@475: * length of the specified array jaroslav@475: * @see Array#get jaroslav@475: */ jaroslav@475: public static native char getChar(Object array, int index) jaroslav@475: throws IllegalArgumentException, ArrayIndexOutOfBoundsException; jaroslav@475: jaroslav@475: /** jaroslav@475: * Returns the value of the indexed component in the specified jaroslav@475: * array object, as a {@code short}. jaroslav@475: * jaroslav@475: * @param array the array jaroslav@475: * @param index the index jaroslav@475: * @return the value of the indexed component in the specified array jaroslav@475: * @exception NullPointerException If the specified object is null jaroslav@475: * @exception IllegalArgumentException If the specified object is not jaroslav@475: * an array, or if the indexed element cannot be converted to the jaroslav@475: * return type by an identity or widening conversion jaroslav@475: * @exception ArrayIndexOutOfBoundsException If the specified {@code index} jaroslav@475: * argument is negative, or if it is greater than or equal to the jaroslav@475: * length of the specified array jaroslav@475: * @see Array#get jaroslav@475: */ jaroslav@483: public static short getShort(Object array, int index) jaroslav@483: throws IllegalArgumentException, ArrayIndexOutOfBoundsException { jaroslav@483: final Class t = array.getClass().getComponentType(); jaroslav@483: if (t == Short.TYPE) { jaroslav@483: short[] arr = (short[]) array; jaroslav@483: return arr[index]; jaroslav@483: } jaroslav@483: return getByte(array, index); jaroslav@483: } jaroslav@475: jaroslav@475: /** jaroslav@475: * Returns the value of the indexed component in the specified jaroslav@475: * array object, as an {@code int}. jaroslav@475: * jaroslav@475: * @param array the array jaroslav@475: * @param index the index jaroslav@475: * @return the value of the indexed component in the specified array jaroslav@475: * @exception NullPointerException If the specified object is null jaroslav@475: * @exception IllegalArgumentException If the specified object is not jaroslav@475: * an array, or if the indexed element cannot be converted to the jaroslav@475: * return type by an identity or widening conversion jaroslav@475: * @exception ArrayIndexOutOfBoundsException If the specified {@code index} jaroslav@475: * argument is negative, or if it is greater than or equal to the jaroslav@475: * length of the specified array jaroslav@475: * @see Array#get jaroslav@475: */ jaroslav@483: public static int getInt(Object array, int index) jaroslav@483: throws IllegalArgumentException, ArrayIndexOutOfBoundsException { jaroslav@483: final Class t = array.getClass().getComponentType(); jaroslav@483: if (t == Integer.TYPE) { jaroslav@483: int[] arr = (int[]) array; jaroslav@483: return arr[index]; jaroslav@483: } jaroslav@483: return getShort(array, index); jaroslav@483: } jaroslav@475: jaroslav@475: /** jaroslav@475: * Returns the value of the indexed component in the specified jaroslav@475: * array object, as a {@code long}. jaroslav@475: * jaroslav@475: * @param array the array jaroslav@475: * @param index the index jaroslav@475: * @return the value of the indexed component in the specified array jaroslav@475: * @exception NullPointerException If the specified object is null jaroslav@475: * @exception IllegalArgumentException If the specified object is not jaroslav@475: * an array, or if the indexed element cannot be converted to the jaroslav@475: * return type by an identity or widening conversion jaroslav@475: * @exception ArrayIndexOutOfBoundsException If the specified {@code index} jaroslav@475: * argument is negative, or if it is greater than or equal to the jaroslav@475: * length of the specified array jaroslav@475: * @see Array#get jaroslav@475: */ jaroslav@483: public static long getLong(Object array, int index) jaroslav@483: throws IllegalArgumentException, ArrayIndexOutOfBoundsException { jaroslav@483: final Class t = array.getClass().getComponentType(); jaroslav@483: if (t == Long.TYPE) { jaroslav@483: long[] arr = (long[]) array; jaroslav@483: return arr[index]; jaroslav@483: } jaroslav@483: return getInt(array, index); jaroslav@483: } jaroslav@475: jaroslav@475: /** jaroslav@475: * Returns the value of the indexed component in the specified jaroslav@475: * array object, as a {@code float}. jaroslav@475: * jaroslav@475: * @param array the array jaroslav@475: * @param index the index jaroslav@475: * @return the value of the indexed component in the specified array jaroslav@475: * @exception NullPointerException If the specified object is null jaroslav@475: * @exception IllegalArgumentException If the specified object is not jaroslav@475: * an array, or if the indexed element cannot be converted to the jaroslav@475: * return type by an identity or widening conversion jaroslav@475: * @exception ArrayIndexOutOfBoundsException If the specified {@code index} jaroslav@475: * argument is negative, or if it is greater than or equal to the jaroslav@475: * length of the specified array jaroslav@475: * @see Array#get jaroslav@475: */ jaroslav@483: public static float getFloat(Object array, int index) jaroslav@483: throws IllegalArgumentException, ArrayIndexOutOfBoundsException { jaroslav@483: final Class t = array.getClass().getComponentType(); jaroslav@483: if (t == Float.TYPE) { jaroslav@483: float[] arr = (float[]) array; jaroslav@483: return arr[index]; jaroslav@483: } jaroslav@483: return getLong(array, index); jaroslav@483: } jaroslav@475: jaroslav@475: /** jaroslav@475: * Returns the value of the indexed component in the specified jaroslav@475: * array object, as a {@code double}. jaroslav@475: * jaroslav@475: * @param array the array jaroslav@475: * @param index the index jaroslav@475: * @return the value of the indexed component in the specified array jaroslav@475: * @exception NullPointerException If the specified object is null jaroslav@475: * @exception IllegalArgumentException If the specified object is not jaroslav@475: * an array, or if the indexed element cannot be converted to the jaroslav@475: * return type by an identity or widening conversion jaroslav@475: * @exception ArrayIndexOutOfBoundsException If the specified {@code index} jaroslav@475: * argument is negative, or if it is greater than or equal to the jaroslav@475: * length of the specified array jaroslav@475: * @see Array#get jaroslav@475: */ jaroslav@483: public static double getDouble(Object array, int index) jaroslav@483: throws IllegalArgumentException, ArrayIndexOutOfBoundsException { jaroslav@483: final Class t = array.getClass().getComponentType(); jaroslav@483: if (t == Double.TYPE) { jaroslav@483: double[] arr = (double[]) array; jaroslav@483: return arr[index]; jaroslav@483: } jaroslav@483: return getFloat(array, index); jaroslav@483: } jaroslav@475: jaroslav@475: /** jaroslav@475: * Sets the value of the indexed component of the specified array jaroslav@475: * object to the specified new value. The new value is first jaroslav@475: * automatically unwrapped if the array has a primitive component jaroslav@475: * type. jaroslav@475: * @param array the array jaroslav@475: * @param index the index into the array jaroslav@475: * @param value the new value of the indexed component jaroslav@475: * @exception NullPointerException If the specified object argument jaroslav@475: * is null jaroslav@475: * @exception IllegalArgumentException If the specified object argument jaroslav@475: * is not an array, or if the array component type is primitive and jaroslav@475: * an unwrapping conversion fails jaroslav@475: * @exception ArrayIndexOutOfBoundsException If the specified {@code index} jaroslav@475: * argument is negative, or if it is greater than or equal to jaroslav@475: * the length of the specified array jaroslav@475: */ jaroslav@475: public static native void set(Object array, int index, Object value) jaroslav@475: throws IllegalArgumentException, ArrayIndexOutOfBoundsException; jaroslav@475: jaroslav@475: /** jaroslav@475: * Sets the value of the indexed component of the specified array jaroslav@475: * object to the specified {@code boolean} value. jaroslav@475: * @param array the array jaroslav@475: * @param index the index into the array jaroslav@475: * @param z the new value of the indexed component jaroslav@475: * @exception NullPointerException If the specified object argument jaroslav@475: * is null jaroslav@475: * @exception IllegalArgumentException If the specified object argument jaroslav@475: * is not an array, or if the specified value cannot be converted jaroslav@475: * to the underlying array's component type by an identity or a jaroslav@475: * primitive widening conversion jaroslav@475: * @exception ArrayIndexOutOfBoundsException If the specified {@code index} jaroslav@475: * argument is negative, or if it is greater than or equal to jaroslav@475: * the length of the specified array jaroslav@475: * @see Array#set jaroslav@475: */ jaroslav@475: public static native void setBoolean(Object array, int index, boolean z) jaroslav@475: throws IllegalArgumentException, ArrayIndexOutOfBoundsException; jaroslav@475: jaroslav@475: /** jaroslav@475: * Sets the value of the indexed component of the specified array jaroslav@475: * object to the specified {@code byte} value. jaroslav@475: * @param array the array jaroslav@475: * @param index the index into the array jaroslav@475: * @param b the new value of the indexed component jaroslav@475: * @exception NullPointerException If the specified object argument jaroslav@475: * is null jaroslav@475: * @exception IllegalArgumentException If the specified object argument jaroslav@475: * is not an array, or if the specified value cannot be converted jaroslav@475: * to the underlying array's component type by an identity or a jaroslav@475: * primitive widening conversion jaroslav@475: * @exception ArrayIndexOutOfBoundsException If the specified {@code index} jaroslav@475: * argument is negative, or if it is greater than or equal to jaroslav@475: * the length of the specified array jaroslav@475: * @see Array#set jaroslav@475: */ jaroslav@484: public static void setByte(Object array, int index, byte b) jaroslav@484: throws IllegalArgumentException, ArrayIndexOutOfBoundsException { jaroslav@484: Class t = array.getClass().getComponentType(); jaroslav@484: if (t == Byte.TYPE) { jaroslav@484: byte[] arr = (byte[]) array; jaroslav@484: arr[index] = b; jaroslav@484: } else { jaroslav@484: setShort(array, index, b); jaroslav@484: } jaroslav@484: } jaroslav@475: jaroslav@475: /** jaroslav@475: * Sets the value of the indexed component of the specified array jaroslav@475: * object to the specified {@code char} value. jaroslav@475: * @param array the array jaroslav@475: * @param index the index into the array jaroslav@475: * @param c the new value of the indexed component jaroslav@475: * @exception NullPointerException If the specified object argument jaroslav@475: * is null jaroslav@475: * @exception IllegalArgumentException If the specified object argument jaroslav@475: * is not an array, or if the specified value cannot be converted jaroslav@475: * to the underlying array's component type by an identity or a jaroslav@475: * primitive widening conversion jaroslav@475: * @exception ArrayIndexOutOfBoundsException If the specified {@code index} jaroslav@475: * argument is negative, or if it is greater than or equal to jaroslav@475: * the length of the specified array jaroslav@475: * @see Array#set jaroslav@475: */ jaroslav@475: public static native void setChar(Object array, int index, char c) jaroslav@475: throws IllegalArgumentException, ArrayIndexOutOfBoundsException; jaroslav@475: jaroslav@475: /** jaroslav@475: * Sets the value of the indexed component of the specified array jaroslav@475: * object to the specified {@code short} value. jaroslav@475: * @param array the array jaroslav@475: * @param index the index into the array jaroslav@475: * @param s the new value of the indexed component jaroslav@475: * @exception NullPointerException If the specified object argument jaroslav@475: * is null jaroslav@475: * @exception IllegalArgumentException If the specified object argument jaroslav@475: * is not an array, or if the specified value cannot be converted jaroslav@475: * to the underlying array's component type by an identity or a jaroslav@475: * primitive widening conversion jaroslav@475: * @exception ArrayIndexOutOfBoundsException If the specified {@code index} jaroslav@475: * argument is negative, or if it is greater than or equal to jaroslav@475: * the length of the specified array jaroslav@475: * @see Array#set jaroslav@475: */ jaroslav@484: public static void setShort(Object array, int index, short s) jaroslav@484: throws IllegalArgumentException, ArrayIndexOutOfBoundsException { jaroslav@484: Class t = array.getClass().getComponentType(); jaroslav@484: if (t == Short.TYPE) { jaroslav@484: short[] arr = (short[]) array; jaroslav@484: arr[index] = s; jaroslav@484: } else { jaroslav@484: setInt(array, index, s); jaroslav@484: } jaroslav@484: jaroslav@484: } jaroslav@475: jaroslav@475: /** jaroslav@475: * Sets the value of the indexed component of the specified array jaroslav@475: * object to the specified {@code int} value. jaroslav@475: * @param array the array jaroslav@475: * @param index the index into the array jaroslav@475: * @param i the new value of the indexed component jaroslav@475: * @exception NullPointerException If the specified object argument jaroslav@475: * is null jaroslav@475: * @exception IllegalArgumentException If the specified object argument jaroslav@475: * is not an array, or if the specified value cannot be converted jaroslav@475: * to the underlying array's component type by an identity or a jaroslav@475: * primitive widening conversion jaroslav@475: * @exception ArrayIndexOutOfBoundsException If the specified {@code index} jaroslav@475: * argument is negative, or if it is greater than or equal to jaroslav@475: * the length of the specified array jaroslav@475: * @see Array#set jaroslav@475: */ jaroslav@484: public static void setInt(Object array, int index, int i) jaroslav@484: throws IllegalArgumentException, ArrayIndexOutOfBoundsException { jaroslav@484: Class t = array.getClass().getComponentType(); jaroslav@484: if (t == Integer.TYPE) { jaroslav@484: long[] arr = (long[]) array; jaroslav@484: arr[index] = i; jaroslav@484: } else { jaroslav@484: setLong(array, index, i); jaroslav@484: } jaroslav@484: } jaroslav@475: jaroslav@475: /** jaroslav@475: * Sets the value of the indexed component of the specified array jaroslav@475: * object to the specified {@code long} value. jaroslav@475: * @param array the array jaroslav@475: * @param index the index into the array jaroslav@475: * @param l the new value of the indexed component jaroslav@475: * @exception NullPointerException If the specified object argument jaroslav@475: * is null jaroslav@475: * @exception IllegalArgumentException If the specified object argument jaroslav@475: * is not an array, or if the specified value cannot be converted jaroslav@475: * to the underlying array's component type by an identity or a jaroslav@475: * primitive widening conversion jaroslav@475: * @exception ArrayIndexOutOfBoundsException If the specified {@code index} jaroslav@475: * argument is negative, or if it is greater than or equal to jaroslav@475: * the length of the specified array jaroslav@475: * @see Array#set jaroslav@475: */ jaroslav@484: public static void setLong(Object array, int index, long l) jaroslav@484: throws IllegalArgumentException, ArrayIndexOutOfBoundsException { jaroslav@484: Class t = array.getClass().getComponentType(); jaroslav@484: if (t == Long.TYPE) { jaroslav@484: long[] arr = (long[]) array; jaroslav@484: arr[index] = l; jaroslav@484: } else { jaroslav@484: setFloat(array, index, l); jaroslav@484: } jaroslav@484: } jaroslav@475: jaroslav@475: /** jaroslav@475: * Sets the value of the indexed component of the specified array jaroslav@475: * object to the specified {@code float} value. jaroslav@475: * @param array the array jaroslav@475: * @param index the index into the array jaroslav@475: * @param f the new value of the indexed component jaroslav@475: * @exception NullPointerException If the specified object argument jaroslav@475: * is null jaroslav@475: * @exception IllegalArgumentException If the specified object argument jaroslav@475: * is not an array, or if the specified value cannot be converted jaroslav@475: * to the underlying array's component type by an identity or a jaroslav@475: * primitive widening conversion jaroslav@475: * @exception ArrayIndexOutOfBoundsException If the specified {@code index} jaroslav@475: * argument is negative, or if it is greater than or equal to jaroslav@475: * the length of the specified array jaroslav@475: * @see Array#set jaroslav@475: */ jaroslav@484: public static void setFloat(Object array, int index, float f) jaroslav@484: throws IllegalArgumentException, ArrayIndexOutOfBoundsException { jaroslav@484: Class t = array.getClass().getComponentType(); jaroslav@484: if (t == Float.TYPE) { jaroslav@484: float[] arr = (float[])array; jaroslav@484: arr[index] = f; jaroslav@484: } else { jaroslav@484: setDouble(array, index, f); jaroslav@484: } jaroslav@484: } jaroslav@475: jaroslav@475: /** jaroslav@475: * Sets the value of the indexed component of the specified array jaroslav@475: * object to the specified {@code double} value. jaroslav@475: * @param array the array jaroslav@475: * @param index the index into the array jaroslav@475: * @param d the new value of the indexed component jaroslav@475: * @exception NullPointerException If the specified object argument jaroslav@475: * is null jaroslav@475: * @exception IllegalArgumentException If the specified object argument jaroslav@475: * is not an array, or if the specified value cannot be converted jaroslav@475: * to the underlying array's component type by an identity or a jaroslav@475: * primitive widening conversion jaroslav@475: * @exception ArrayIndexOutOfBoundsException If the specified {@code index} jaroslav@475: * argument is negative, or if it is greater than or equal to jaroslav@475: * the length of the specified array jaroslav@475: * @see Array#set jaroslav@475: */ jaroslav@484: public static void setDouble(Object array, int index, double d) jaroslav@484: throws IllegalArgumentException, ArrayIndexOutOfBoundsException { jaroslav@484: Class t = array.getClass().getComponentType(); jaroslav@484: if (t == Double.TYPE) { jaroslav@484: double[] arr = (double[])array; jaroslav@484: arr[index] = d; jaroslav@484: } else { jaroslav@484: throw new IllegalArgumentException("argument type mismatch"); jaroslav@484: } jaroslav@484: } jaroslav@475: jaroslav@475: /* jaroslav@475: * Private jaroslav@475: */ jaroslav@475: jaroslav@477: @JavaScriptBody(args = { "primitive", "sig", "length" }, body = jaroslav@477: "var arr = new Array(length);\n" jaroslav@477: + "var value = primitive ? 0 : null;\n" jaroslav@477: + "for(var i = 0; i < length; i++) arr[i] = value;\n" jaroslav@477: + "arr.jvmName = sig;\n" jaroslav@477: + "return arr;" jaroslav@477: ) jaroslav@477: private static native Object newArray(boolean primitive, String sig, int length); jaroslav@475: jaroslav@480: private static Object multiNewArray(String sig, int[] dims, int index) jaroslav@480: throws IllegalArgumentException, NegativeArraySizeException { jaroslav@480: if (dims.length == index + 1) { jaroslav@480: return newArray(sig.length() == 2, sig, dims[index]); jaroslav@480: } jaroslav@480: Object[] arr = (Object[]) newArray(false, sig, dims[index]); jaroslav@480: String compsig = sig.substring(1); jaroslav@480: for (int i = 0; i < arr.length; i++) { jaroslav@480: arr[i] = multiNewArray(compsig, dims, index + 1); jaroslav@480: } jaroslav@480: return arr; jaroslav@480: } jaroslav@475: }