--- /home/tromey/gnu/Nightly/classpath/classpath/java/lang/reflect/Array.java 2004-03-29 02:23:00.000000000 -0700 +++ java/lang/reflect/Array.java 2004-01-08 02:18:05.000000000 -0700 @@ -104,29 +104,7 @@ * @throws NegativeArraySizeException when length is less than 0 * @throws OutOfMemoryError if memory allocation fails */ - public static Object newInstance(Class componentType, int length) - { - if (! componentType.isPrimitive()) - return createObjectArray(componentType, length); - if (componentType == boolean.class) - return new boolean[length]; - if (componentType == byte.class) - return new byte[length]; - if (componentType == char.class) - return new char[length]; - if (componentType == short.class) - return new short[length]; - if (componentType == int.class) - return new int[length]; - if (componentType == long.class) - return new long[length]; - if (componentType == float.class) - return new float[length]; - if (componentType == double.class) - return new double[length]; - // assert componentType == void.class - throw new IllegalArgumentException(); - } + public static native Object newInstance(Class componentType, int length); /** * Creates a new multi-dimensioned array. The new array has the same @@ -152,13 +130,7 @@ * than 0 * @throws OutOfMemoryError if memory allocation fails */ - public static Object newInstance(Class componentType, int[] dimensions) - { - if (dimensions.length <= 0) - throw new IllegalArgumentException ("Empty dimensions array."); - return createMultiArray(componentType, dimensions, - dimensions.length - 1); - } + public static native Object newInstance(Class elementType, int[] dimensions); /** * Gets the array length. @@ -167,30 +139,7 @@ * @throws IllegalArgumentException if array is not an array * @throws NullPointerException if array is null */ - public static int getLength(Object array) - { - if (array instanceof Object[]) - return ((Object[]) array).length; - if (array instanceof boolean[]) - return ((boolean[]) array).length; - if (array instanceof byte[]) - return ((byte[]) array). length; - if (array instanceof char[]) - return ((char[]) array).length; - if (array instanceof short[]) - return ((short[]) array).length; - if (array instanceof int[]) - return ((int[]) array).length; - if (array instanceof long[]) - return ((long[]) array).length; - if (array instanceof float[]) - return ((float[]) array).length; - if (array instanceof double[]) - return ((double[]) array).length; - if (array == null) - throw new NullPointerException(); - throw new IllegalArgumentException(); - } + public static native int getLength(Object array); /** * Gets an element of an array. Primitive elements will be wrapped in @@ -212,30 +161,7 @@ * @see #getFloat(Object, int) * @see #getDouble(Object, int) */ - public static Object get(Object array, int index) - { - if (array instanceof Object[]) - return ((Object[]) array)[index]; - if (array instanceof boolean[]) - return ((boolean[]) array)[index] ? Boolean.TRUE : Boolean.FALSE; - if (array instanceof byte[]) - return new Byte(((byte[]) array)[index]); - if (array instanceof char[]) - return new Character(((char[]) array)[index]); - if (array instanceof short[]) - return new Short(((short[]) array)[index]); - if (array instanceof int[]) - return new Integer(((int[]) array)[index]); - if (array instanceof long[]) - return new Long(((long[]) array)[index]); - if (array instanceof float[]) - return new Float(((float[]) array)[index]); - if (array instanceof double[]) - return new Double(((double[]) array)[index]); - if (array == null) - throw new NullPointerException(); - throw new IllegalArgumentException(); - } + public static native Object get(Object array, int index); /** * Gets an element of a boolean array. @@ -250,14 +176,7 @@ * bounds * @see #get(Object, int) */ - public static boolean getBoolean(Object array, int index) - { - if (array instanceof boolean[]) - return ((boolean[]) array)[index]; - if (array == null) - throw new NullPointerException(); - throw new IllegalArgumentException(); - } + public static native boolean getBoolean(Object array, int index); /** * Gets an element of a byte array. @@ -272,14 +191,7 @@ * bounds * @see #get(Object, int) */ - public static byte getByte(Object array, int index) - { - if (array instanceof byte[]) - return ((byte[]) array)[index]; - if (array == null) - throw new NullPointerException(); - throw new IllegalArgumentException(); - } + public static native byte getByte(Object array, int index); /** * Gets an element of a char array. @@ -294,14 +206,7 @@ * bounds * @see #get(Object, int) */ - public static char getChar(Object array, int index) - { - if (array instanceof char[]) - return ((char[]) array)[index]; - if (array == null) - throw new NullPointerException(); - throw new IllegalArgumentException(); - } + public static native char getChar(Object array, int index); /** * Gets an element of a short array. @@ -316,12 +221,7 @@ * bounds * @see #get(Object, int) */ - public static short getShort(Object array, int index) - { - if (array instanceof short[]) - return ((short[]) array)[index]; - return getByte(array, index); - } + public static native short getShort(Object array, int index); /** * Gets an element of an int array. @@ -336,14 +236,7 @@ * bounds * @see #get(Object, int) */ - public static int getInt(Object array, int index) - { - if (array instanceof int[]) - return ((int[]) array)[index]; - if (array instanceof char[]) - return ((char[]) array)[index]; - return getShort(array, index); - } + public static native int getInt(Object array, int index); /** * Gets an element of a long array. @@ -358,12 +251,7 @@ * bounds * @see #get(Object, int) */ - public static long getLong(Object array, int index) - { - if (array instanceof long[]) - return ((long[]) array)[index]; - return getInt(array, index); - } + public static native long getLong(Object array, int index); /** * Gets an element of a float array. @@ -378,12 +266,7 @@ * bounds * @see #get(Object, int) */ - public static float getFloat(Object array, int index) - { - if (array instanceof float[]) - return ((float[]) array)[index]; - return getLong(array, index); - } + public static native float getFloat(Object array, int index); /** * Gets an element of a double array. @@ -398,12 +281,12 @@ * bounds * @see #get(Object, int) */ - public static double getDouble(Object array, int index) - { - if (array instanceof double[]) - return ((double[]) array)[index]; - return getFloat(array, index); - } + public static native double getDouble(Object array, int index); + + private static native Class getElementType(Object array, int index); + + private static native void set(Object array, int index, + Object value, Class elType); /** * Sets an element of an array. If the array is primitive, then the new @@ -430,14 +313,9 @@ */ public static void set(Object array, int index, Object value) { - if (array instanceof Object[]) - { - // Too bad the API won't let us throw the easier ArrayStoreException! - if (value != null - && ! array.getClass().getComponentType().isInstance(value)) - throw new IllegalArgumentException(); - ((Object[]) array)[index] = value; - } + Class elType = getElementType(array, index); + if (! elType.isPrimitive()) + set(array, index, value, elType); else if (value instanceof Byte) setByte(array, index, ((Byte) value).byteValue()); else if (value instanceof Short) @@ -454,8 +332,6 @@ setChar(array, index, ((Character) value).charValue()); else if (value instanceof Boolean) setBoolean(array, index, ((Boolean) value).booleanValue()); - else if (array == null) - throw new NullPointerException(); else throw new IllegalArgumentException(); } @@ -473,15 +349,7 @@ * bounds * @see #set(Object, int, Object) */ - public static void setBoolean(Object array, int index, boolean value) - { - if (array instanceof boolean[]) - ((boolean[]) array)[index] = value; - else if (array == null) - throw new NullPointerException(); - else - throw new IllegalArgumentException(); - } + public static native void setBoolean(Object array, int index, boolean value); /** * Sets an element of a byte array. @@ -496,13 +364,7 @@ * bounds * @see #set(Object, int, Object) */ - public static void setByte(Object array, int index, byte value) - { - if (array instanceof byte[]) - ((byte[]) array)[index] = value; - else - setShort(array, index, value); - } + public static native void setByte(Object array, int index, byte value); /** * Sets an element of a char array. @@ -517,13 +379,7 @@ * bounds * @see #set(Object, int, Object) */ - public static void setChar(Object array, int index, char value) - { - if (array instanceof char[]) - ((char[]) array)[index] = value; - else - setInt(array, index, value); - } + public static native void setChar(Object array, int index, char value); /** * Sets an element of a short array. @@ -538,13 +394,7 @@ * bounds * @see #set(Object, int, Object) */ - public static void setShort(Object array, int index, short value) - { - if (array instanceof short[]) - ((short[]) array)[index] = value; - else - setInt(array, index, value); - } + public static native void setShort(Object array, int index, short value); /** * Sets an element of an int array. @@ -559,13 +409,7 @@ * bounds * @see #set(Object, int, Object) */ - public static void setInt(Object array, int index, int value) - { - if (array instanceof int[]) - ((int[]) array)[index] = value; - else - setLong(array, index, value); - } + public static native void setInt(Object array, int index, int value); /** * Sets an element of a long array. @@ -580,13 +424,7 @@ * bounds * @see #set(Object, int, Object) */ - public static void setLong(Object array, int index, long value) - { - if (array instanceof long[]) - ((long[]) array)[index] = value; - else - setFloat(array, index, value); - } + public static native void setLong(Object array, int index, long value); /** * Sets an element of a float array. @@ -601,13 +439,7 @@ * bounds * @see #set(Object, int, Object) */ - public static void setFloat(Object array, int index, float value) - { - if (array instanceof float[]) - ((float[]) array)[index] = value; - else - setDouble(array, index, value); - } + public static native void setFloat(Object array, int index, float value); /** * Sets an element of a double array. @@ -622,54 +454,5 @@ * bounds * @see #set(Object, int, Object) */ - public static void setDouble(Object array, int index, double value) - { - if (array instanceof double[]) - ((double[]) array)[index] = value; - else if (array == null) - throw new NullPointerException(); - else - throw new IllegalArgumentException(); - } - - /** - * Dynamically and recursively create a multi-dimensioned array of objects. - * - * @param type guaranteed to be a valid object type - * @param dimensions the dimensions of the array - * @param index index of the current dimension to build - * @return the new multi-dimensioned array - * @throws NegativeArraySizeException if any entry of dimensions is negative - * @throws OutOfMemoryError if memory allocation fails - */ - // This would be faster if implemented natively, using the multianewarray - // bytecode instead of this recursive call - private static Object createMultiArray(Class type, int[] dimensions, - int index) - { - if (index == 0) - return newInstance(type, dimensions[0]); - - Object toAdd = createMultiArray(type, dimensions, index - 1); - Class thisType = toAdd.getClass(); - Object[] retval - = (Object[]) createObjectArray(thisType, dimensions[index]); - if (dimensions[index] > 0) - retval[0] = toAdd; - int i = dimensions[index]; - while (--i > 0) - retval[i] = createMultiArray(type, dimensions, index - 1); - return retval; - } - - /** - * Dynamically create an array of objects. - * - * @param type guaranteed to be a valid object type - * @param dim the length of the array - * @return the new array - * @throws NegativeArraySizeException if dim is negative - * @throws OutOfMemoryError if memory allocation fails - */ - private static native Object createObjectArray(Class type, int dim); + public static native void setDouble(Object array, int index, double value); }