|
From: <ls...@us...> - 2008-12-08 14:51:53
|
Revision: 4783
http://jnode.svn.sourceforge.net/jnode/?rev=4783&view=rev
Author: lsantha
Date: 2008-12-08 14:51:34 +0000 (Mon, 08 Dec 2008)
Log Message:
-----------
Integrated java.lang.reflect.Array from OpenJDK.
Added Paths:
-----------
trunk/core/src/classpath/java/java/lang/reflect/CPArray.java
trunk/core/src/openjdk/java/java/lang/reflect/Array.java
trunk/core/src/openjdk/vm/java/lang/reflect/NativeArray.java
Added: trunk/core/src/classpath/java/java/lang/reflect/CPArray.java
===================================================================
--- trunk/core/src/classpath/java/java/lang/reflect/CPArray.java (rev 0)
+++ trunk/core/src/classpath/java/java/lang/reflect/CPArray.java 2008-12-08 14:51:34 UTC (rev 4783)
@@ -0,0 +1,656 @@
+/* java.lang.reflect.Array - manipulate arrays by reflection
+ Copyright (C) 1998, 1999, 2001, 2003, 2005 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING. If not, write to the
+Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library. Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module. An independent module is a module which is not derived from
+or based on this library. If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so. If you do not wish to do so, delete this
+exception statement from your version. */
+
+
+package java.lang.reflect;
+
+/**
+ * Array holds static helper functions that allow you to create and
+ * manipulate arrays by reflection. Operations know how to perform widening
+ * conversions, but throw {@link IllegalArgumentException} if you attempt
+ * a narrowing conversion. Also, when accessing primitive arrays, this
+ * class performs object wrapping and unwrapping as necessary.<p>
+ *
+ * <B>Note:</B> This class returns and accepts types as Classes, even
+ * primitive types; there are Class types defined that represent each
+ * different primitive type. They are <code>java.lang.Boolean.TYPE,
+ * java.lang.Byte.TYPE,</code>, also available as <code>boolean.class,
+ * byte.class</code>, etc. These are not to be confused with the
+ * classes <code>java.lang.Boolean, java.lang.Byte</code>, etc., which are
+ * real classes. Note also that the shorthand <code>Object[].class</code>
+ * is a convenient way to get array Classes.<p>
+ *
+ * <B>Performance note:</B> This class performs best when it does not have
+ * to convert primitive types. The further along the chain it has to convert,
+ * the worse performance will be. You're best off using the array as whatever
+ * type it already is, and then converting the result. You will do even
+ * worse if you do this and use the generic set() function.
+ *
+ * @author John Keiser
+ * @author Eric Blake (eb...@em...)
+ * @author Per Bothner (bo...@cy...)
+ * @see java.lang.Boolean#TYPE
+ * @see java.lang.Byte#TYPE
+ * @see java.lang.Short#TYPE
+ * @see java.lang.Character#TYPE
+ * @see java.lang.Integer#TYPE
+ * @see java.lang.Long#TYPE
+ * @see java.lang.Float#TYPE
+ * @see java.lang.Double#TYPE
+ * @since 1.1
+ * @status updated to 1.4
+ */
+public final class CPArray
+{
+
+ /**
+ * This class is uninstantiable.
+ */
+ private CPArray()
+ {
+ }
+
+ /**
+ * Creates a new single-dimensioned array.
+ * @param componentType the type of the array to create
+ * @param length the length of the array to create
+ * @return the created array, cast to an Object
+ * @throws NullPointerException if <code>componentType</code> is null
+ * @throws IllegalArgumentException if <code>componentType</code> is
+ * <code>Void.TYPE</code>
+ * @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 VMArray.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();
+ }
+
+ /**
+ * Creates a new multi-dimensioned array. The new array has the same
+ * component type as the argument class, and the number of dimensions
+ * in the new array is the sum of the dimensions of the argument class
+ * and the length of the argument dimensions. Virtual Machine limitations
+ * forbid too many dimensions (usually 255 is the maximum); but even
+ * 50 dimensions of 2 elements in each dimension would exceed your memory
+ * long beforehand!
+ *
+ * @param componentType the type of the array to create.
+ * @param dimensions the dimensions of the array to create. Each element
+ * in <code>dimensions</code> makes another dimension of the new
+ * array. Thus, <code>Array.newInstance(java.lang.Boolean,
+ * new int[]{1,2,3})</code> is the same as
+ * <code>new java.lang.Boolean[1][2][3]</code>
+ * @return the created array, cast to an Object
+ * @throws NullPointerException if componentType or dimension is null
+ * @throws IllegalArgumentException if the the size of
+ * <code>dimensions</code> is 0 or exceeds the maximum number of
+ * array dimensions in the VM; or if componentType is Void.TYPE
+ * @throws NegativeArraySizeException when any of the dimensions is less
+ * 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);
+ }
+
+ /**
+ * Gets the array length.
+ * @param array the array
+ * @return the length of the array
+ * @throws IllegalArgumentException if <code>array</code> is not an array
+ * @throws NullPointerException if <code>array</code> 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();
+ }
+
+ /**
+ * Gets an element of an array. Primitive elements will be wrapped in
+ * the corresponding class type.
+ *
+ * @param array the array to access
+ * @param index the array index to access
+ * @return the element at <code>array[index]</code>
+ * @throws IllegalArgumentException if <code>array</code> is not an array
+ * @throws NullPointerException if <code>array</code> is null
+ * @throws ArrayIndexOutOfBoundsException if <code>index</code> is out of
+ * bounds
+ * @see #getBoolean(Object, int)
+ * @see #getByte(Object, int)
+ * @see #getChar(Object, int)
+ * @see #getShort(Object, int)
+ * @see #getInt(Object, int)
+ * @see #getLong(Object, int)
+ * @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();
+ }
+
+ /**
+ * Gets an element of a boolean array.
+ *
+ * @param array the array to access
+ * @param index the array index to access
+ * @return the boolean element at <code>array[index]</code>
+ * @throws IllegalArgumentException if <code>array</code> is not a boolean
+ * array
+ * @throws NullPointerException if <code>array</code> is null
+ * @throws ArrayIndexOutOfBoundsException if <code>index</code> is out of
+ * 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();
+ }
+
+ /**
+ * Gets an element of a byte array.
+ *
+ * @param array the array to access
+ * @param index the array index to access
+ * @return the byte element at <code>array[index]</code>
+ * @throws IllegalArgumentException if <code>array</code> is not a byte
+ * array
+ * @throws NullPointerException if <code>array</code> is null
+ * @throws ArrayIndexOutOfBoundsException if <code>index</code> is out of
+ * 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();
+ }
+
+ /**
+ * Gets an element of a char array.
+ *
+ * @param array the array to access
+ * @param index the array index to access
+ * @return the char element at <code>array[index]</code>
+ * @throws IllegalArgumentException if <code>array</code> is not a char
+ * array
+ * @throws NullPointerException if <code>array</code> is null
+ * @throws ArrayIndexOutOfBoundsException if <code>index</code> is out of
+ * 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();
+ }
+
+ /**
+ * Gets an element of a short array.
+ *
+ * @param array the array to access
+ * @param index the array index to access
+ * @return the short element at <code>array[index]</code>
+ * @throws IllegalArgumentException if <code>array</code> is not a byte
+ * or char array
+ * @throws NullPointerException if <code>array</code> is null
+ * @throws ArrayIndexOutOfBoundsException if <code>index</code> is out of
+ * 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);
+ }
+
+ /**
+ * Gets an element of an int array.
+ *
+ * @param array the array to access
+ * @param index the array index to access
+ * @return the int element at <code>array[index]</code>
+ * @throws IllegalArgumentException if <code>array</code> is not a byte,
+ * char, short, or int array
+ * @throws NullPointerException if <code>array</code> is null
+ * @throws ArrayIndexOutOfBoundsException if <code>index</code> is out of
+ * 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);
+ }
+
+ /**
+ * Gets an element of a long array.
+ *
+ * @param array the array to access
+ * @param index the array index to access
+ * @return the long element at <code>array[index]</code>
+ * @throws IllegalArgumentException if <code>array</code> is not a byte,
+ * char, short, int, or long array
+ * @throws NullPointerException if <code>array</code> is null
+ * @throws ArrayIndexOutOfBoundsException if <code>index</code> is out of
+ * 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);
+ }
+
+ /**
+ * Gets an element of a float array.
+ *
+ * @param array the array to access
+ * @param index the array index to access
+ * @return the float element at <code>array[index]</code>
+ * @throws IllegalArgumentException if <code>array</code> is not a byte,
+ * char, short, int, long, or float array
+ * @throws NullPointerException if <code>array</code> is null
+ * @throws ArrayIndexOutOfBoundsException if <code>index</code> is out of
+ * 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);
+ }
+
+ /**
+ * Gets an element of a double array.
+ *
+ * @param array the array to access
+ * @param index the array index to access
+ * @return the double element at <code>array[index]</code>
+ * @throws IllegalArgumentException if <code>array</code> is not a byte,
+ * char, short, int, long, float, or double array
+ * @throws NullPointerException if <code>array</code> is null
+ * @throws ArrayIndexOutOfBoundsException if <code>index</code> is out of
+ * 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);
+ }
+
+ /**
+ * Sets an element of an array. If the array is primitive, then the new
+ * value is unwrapped and widened.
+ *
+ * @param array the array to set a value of
+ * @param index the array index to set the value to
+ * @param value the value to set
+ * @throws IllegalArgumentException if <code>array</code> is not an array,
+ * or the array is primitive and unwrapping value fails, or the
+ * value is not assignable to the array component type
+ * @throws NullPointerException if array is null, or if array is primitive
+ * and value is null
+ * @throws ArrayIndexOutOfBoundsException if <code>index</code> is out of
+ * bounds
+ * @see #setBoolean(Object, int, boolean)
+ * @see #setByte(Object, int, byte)
+ * @see #setChar(Object, int, char)
+ * @see #setShort(Object, int, short)
+ * @see #setInt(Object, int, int)
+ * @see #setLong(Object, int, long)
+ * @see #setFloat(Object, int, float)
+ * @see #setDouble(Object, int, double)
+ */
+ 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;
+ }
+ else if (value instanceof Byte)
+ setByte(array, index, ((Byte) value).byteValue());
+ else if (value instanceof Short)
+ setShort(array, index, ((Short) value).shortValue());
+ else if (value instanceof Integer)
+ setInt(array, index, ((Integer) value).intValue());
+ else if (value instanceof Long)
+ setLong(array, index, ((Long) value).longValue());
+ else if (value instanceof Float)
+ setFloat(array, index, ((Float) value).floatValue());
+ else if (value instanceof Double)
+ setDouble(array, index, ((Double) value).doubleValue());
+ else if (value instanceof Character)
+ 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();
+ }
+
+ /**
+ * Sets an element of a boolean array.
+ *
+ * @param array the array to set a value of
+ * @param index the array index to set the value to
+ * @param value the value to set
+ * @throws IllegalArgumentException if <code>array</code> is not a boolean
+ * array
+ * @throws NullPointerException if <code>array</code> is null
+ * @throws ArrayIndexOutOfBoundsException if <code>index</code> is out of
+ * 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();
+ }
+
+ /**
+ * Sets an element of a byte array.
+ *
+ * @param array the array to set a value of
+ * @param index the array index to set the value to
+ * @param value the value to set
+ * @throws IllegalArgumentException if <code>array</code> is not a byte,
+ * short, int, long, float, or double array
+ * @throws NullPointerException if <code>array</code> is null
+ * @throws ArrayIndexOutOfBoundsException if <code>index</code> is out of
+ * 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);
+ }
+
+ /**
+ * Sets an element of a char array.
+ *
+ * @param array the array to set a value of
+ * @param index the array index to set the value to
+ * @param value the value to set
+ * @throws IllegalArgumentException if <code>array</code> is not a char,
+ * int, long, float, or double array
+ * @throws NullPointerException if <code>array</code> is null
+ * @throws ArrayIndexOutOfBoundsException if <code>index</code> is out of
+ * 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);
+ }
+
+ /**
+ * Sets an element of a short array.
+ *
+ * @param array the array to set a value of
+ * @param index the array index to set the value to
+ * @param value the value to set
+ * @throws IllegalArgumentException if <code>array</code> is not a short,
+ * int, long, float, or double array
+ * @throws NullPointerException if <code>array</code> is null
+ * @throws ArrayIndexOutOfBoundsException if <code>index</code> is out of
+ * 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);
+ }
+
+ /**
+ * Sets an element of an int array.
+ *
+ * @param array the array to set a value of
+ * @param index the array index to set the value to
+ * @param value the value to set
+ * @throws IllegalArgumentException if <code>array</code> is not an int,
+ * long, float, or double array
+ * @throws NullPointerException if <code>array</code> is null
+ * @throws ArrayIndexOutOfBoundsException if <code>index</code> is out of
+ * 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);
+ }
+
+ /**
+ * Sets an element of a long array.
+ *
+ * @param array the array to set a value of
+ * @param index the array index to set the value to
+ * @param value the value to set
+ * @throws IllegalArgumentException if <code>array</code> is not a long,
+ * float, or double array
+ * @throws NullPointerException if <code>array</code> is null
+ * @throws ArrayIndexOutOfBoundsException if <code>index</code> is out of
+ * 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);
+ }
+
+ /**
+ * Sets an element of a float array.
+ *
+ * @param array the array to set a value of
+ * @param index the array index to set the value to
+ * @param value the value to set
+ * @throws IllegalArgumentException if <code>array</code> is not a float
+ * or double array
+ * @throws NullPointerException if <code>array</code> is null
+ * @throws ArrayIndexOutOfBoundsException if <code>index</code> is out of
+ * 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);
+ }
+
+ /**
+ * Sets an element of a double array.
+ *
+ * @param array the array to set a value of
+ * @param index the array index to set the value to
+ * @param value the value to set
+ * @throws IllegalArgumentException if <code>array</code> is not a double
+ * array
+ * @throws NullPointerException if <code>array</code> is null
+ * @throws ArrayIndexOutOfBoundsException if <code>index</code> is out of
+ * 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[]) VMArray.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;
+ }
+
+}
Added: trunk/core/src/openjdk/java/java/lang/reflect/Array.java
===================================================================
--- trunk/core/src/openjdk/java/java/lang/reflect/Array.java (rev 0)
+++ trunk/core/src/openjdk/java/java/lang/reflect/Array.java 2008-12-08 14:51:34 UTC (rev 4783)
@@ -0,0 +1,485 @@
+/*
+ * Copyright 1996-2006 Sun Microsystems, Inc. All Rights Reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation. Sun designates this
+ * particular file as subject to the "Classpath" exception as provided
+ * by Sun in the LICENSE file that accompanied this code.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
+ * CA 95054 USA or visit www.sun.com if you need additional information or
+ * have any questions.
+ */
+
+package java.lang.reflect;
+
+/**
+ * The {@code Array} class provides static methods to dynamically create and
+ * access Java arrays.
+ *
+ * <p>{@code Array} permits widening conversions to occur during a get or set
+ * operation, but throws an {@code IllegalArgumentException} if a narrowing
+ * conversion would occur.
+ *
+ * @author Nakul Saraiya
+ */
+public final
+class Array {
+
+ /**
+ * Constructor. Class Array is not instantiable.
+ */
+ private Array() {}
+
+ /**
+ * Creates a new array with the specified component type and
+ * length.
+ * Invoking this method is equivalent to creating an array
+ * as follows:
+ * <blockquote>
+ * <pre>
+ * int[] x = {length};
+ * Array.newInstance(componentType, x);
+ * </pre>
+ * </blockquote>
+ *
+ * @param componentType the {@code Class} object representing the
+ * component type of the new array
+ * @param length the length of the new array
+ * @return the new array
+ * @exception NullPointerException if the specified
+ * {@code componentType} parameter is null
+ * @exception IllegalArgumentException if componentType is {@link Void#TYPE}
+ * @exception NegativeArraySizeException if the specified {@code length}
+ * is negative
+ */
+ public static Object newInstance(Class<?> componentType, int length)
+ throws NegativeArraySizeException {
+ return newArray(componentType, length);
+ }
+
+ /**
+ * Creates a new array
+ * with the specified component type and dimensions.
+ * If {@code componentType}
+ * represents a non-array class or interface, the new array
+ * has {@code dimensions.length} dimensions and
+ * {@code componentType} as its component type. If
+ * {@code componentType} represents an array class, the
+ * number of dimensions of the new array is equal to the sum
+ * of {@code dimensions.length} and the number of
+ * dimensions of {@code componentType}. In this case, the
+ * component type of the new array is the component type of
+ * {@code componentType}.
+ *
+ * <p>The number of dimensions of the new array must not
+ * exceed the number of array dimensions supported by the
+ * implementation (typically 255).
+ *
+ * @param componentType the {@code Class} object representing the component
+ * type of the new array
+ * @param dimensions an array of {@code int} representing the dimensions of
+ * the new array
+ * @return the new array
+ * @exception NullPointerException if the specified
+ * {@code componentType} argument is null
+ * @exception IllegalArgumentException if the specified {@code dimensions}
+ * argument is a zero-dimensional array, or if the number of
+ * requested dimensions exceeds the limit on the number of array dimensions
+ * supported by the implementation (typically 255), or if componentType
+ * is {@link Void#TYPE}.
+ * @exception NegativeArraySizeException if any of the components in
+ * the specified {@code dimensions} argument is negative.
+ */
+ public static Object newInstance(Class<?> componentType, int... dimensions)
+ throws IllegalArgumentException, NegativeArraySizeException {
+ return multiNewArray(componentType, dimensions);
+ }
+
+ /**
+ * Returns the length of the specified array object, as an {@code int}.
+ *
+ * @param array the array
+ * @return the length of the array
+ * @exception IllegalArgumentException if the object argument is not
+ * an array
+ */
+ public static native int getLength(Object array)
+ throws IllegalArgumentException;
+
+ /**
+ * Returns the value of the indexed component in the specified
+ * array object. The value is automatically wrapped in an object
+ * if it has a primitive type.
+ *
+ * @param array the array
+ * @param index the index
+ * @return the (possibly wrapped) value of the indexed component in
+ * the specified array
+ * @exception NullPointerException If the specified object is null
+ * @exception IllegalArgumentException If the specified object is not
+ * an array
+ * @exception ArrayIndexOutOfBoundsException If the specified {@code index}
+ * argument is negative, or if it is greater than or equal to the
+ * length of the specified array
+ */
+ public static native Object get(Object array, int index)
+ throws IllegalArgumentException, ArrayIndexOutOfBoundsException;
+
+ /**
+ * Returns the value of the indexed component in the specified
+ * array object, as a {@code boolean}.
+ *
+ * @param array the array
+ * @param index the index
+ * @return the value of the indexed component in the specified array
+ * @exception NullPointerException If the specified object is null
+ * @exception IllegalArgumentException If the specified object is not
+ * an array, or if the indexed element cannot be converted to the
+ * return type by an identity or widening conversion
+ * @exception ArrayIndexOutOfBoundsException If the specified {@code index}
+ * argument is negative, or if it is greater than or equal to the
+ * length of the specified array
+ * @see Array#get
+ */
+ public static native boolean getBoolean(Object array, int index)
+ throws IllegalArgumentException, ArrayIndexOutOfBoundsException;
+
+ /**
+ * Returns the value of the indexed component in the specified
+ * array object, as a {@code byte}.
+ *
+ * @param array the array
+ * @param index the index
+ * @return the value of the indexed component in the specified array
+ * @exception NullPointerException If the specified object is null
+ * @exception IllegalArgumentException If the specified object is not
+ * an array, or if the indexed element cannot be converted to the
+ * return type by an identity or widening conversion
+ * @exception ArrayIndexOutOfBoundsException If the specified {@code index}
+ * argument is negative, or if it is greater than or equal to the
+ * length of the specified array
+ * @see Array#get
+ */
+ public static native byte getByte(Object array, int index)
+ throws IllegalArgumentException, ArrayIndexOutOfBoundsException;
+
+ /**
+ * Returns the value of the indexed component in the specified
+ * array object, as a {@code char}.
+ *
+ * @param array the array
+ * @param index the index
+ * @return the value of the indexed component in the specified array
+ * @exception NullPointerException If the specified object is null
+ * @exception IllegalArgumentException If the specified object is not
+ * an array, or if the indexed element cannot be converted to the
+ * return type by an identity or widening conversion
+ * @exception ArrayIndexOutOfBoundsException If the specified {@code index}
+ * argument is negative, or if it is greater than or equal to the
+ * length of the specified array
+ * @see Array#get
+ */
+ public static native char getChar(Object array, int index)
+ throws IllegalArgumentException, ArrayIndexOutOfBoundsException;
+
+ /**
+ * Returns the value of the indexed component in the specified
+ * array object, as a {@code short}.
+ *
+ * @param array the array
+ * @param index the index
+ * @return the value of the indexed component in the specified array
+ * @exception NullPointerException If the specified object is null
+ * @exception IllegalArgumentException If the specified object is not
+ * an array, or if the indexed element cannot be converted to the
+ * return type by an identity or widening conversion
+ * @exception ArrayIndexOutOfBoundsException If the specified {@code index}
+ * argument is negative, or if it is greater than or equal to the
+ * length of the specified array
+ * @see Array#get
+ */
+ public static native short getShort(Object array, int index)
+ throws IllegalArgumentException, ArrayIndexOutOfBoundsException;
+
+ /**
+ * Returns the value of the indexed component in the specified
+ * array object, as an {@code int}.
+ *
+ * @param array the array
+ * @param index the index
+ * @return the value of the indexed component in the specified array
+ * @exception NullPointerException If the specified object is null
+ * @exception IllegalArgumentException If the specified object is not
+ * an array, or if the indexed element cannot be converted to the
+ * return type by an identity or widening conversion
+ * @exception ArrayIndexOutOfBoundsException If the specified {@code index}
+ * argument is negative, or if it is greater than or equal to the
+ * length of the specified array
+ * @see Array#get
+ */
+ public static native int getInt(Object array, int index)
+ throws IllegalArgumentException, ArrayIndexOutOfBoundsException;
+
+ /**
+ * Returns the value of the indexed component in the specified
+ * array object, as a {@code long}.
+ *
+ * @param array the array
+ * @param index the index
+ * @return the value of the indexed component in the specified array
+ * @exception NullPointerException If the specified object is null
+ * @exception IllegalArgumentException If the specified object is not
+ * an array, or if the indexed element cannot be converted to the
+ * return type by an identity or widening conversion
+ * @exception ArrayIndexOutOfBoundsException If the specified {@code index}
+ * argument is negative, or if it is greater than or equal to the
+ * length of the specified array
+ * @see Array#get
+ */
+ public static native long getLong(Object array, int index)
+ throws IllegalArgumentException, ArrayIndexOutOfBoundsException;
+
+ /**
+ * Returns the value of the indexed component in the specified
+ * array object, as a {@code float}.
+ *
+ * @param array the array
+ * @param index the index
+ * @return the value of the indexed component in the specified array
+ * @exception NullPointerException If the specified object is null
+ * @exception IllegalArgumentException If the specified object is not
+ * an array, or if the indexed element cannot be converted to the
+ * return type by an identity or widening conversion
+ * @exception ArrayIndexOutOfBoundsException If the specified {@code index}
+ * argument is negative, or if it is greater than or equal to the
+ * length of the specified array
+ * @see Array#get
+ */
+ public static native float getFloat(Object array, int index)
+ throws IllegalArgumentException, ArrayIndexOutOfBoundsException;
+
+ /**
+ * Returns the value of the indexed component in the specified
+ * array object, as a {@code double}.
+ *
+ * @param array the array
+ * @param index the index
+ * @return the value of the indexed component in the specified array
+ * @exception NullPointerException If the specified object is null
+ * @exception IllegalArgumentException If the specified object is not
+ * an array, or if the indexed element cannot be converted to the
+ * return type by an identity or widening conversion
+ * @exception ArrayIndexOutOfBoundsException If the specified {@code index}
+ * argument is negative, or if it is greater than or equal to the
+ * length of the specified array
+ * @see Array#get
+ */
+ public static native double getDouble(Object array, int index)
+ throws IllegalArgumentException, ArrayIndexOutOfBoundsException;
+
+ /**
+ * Sets the value of the indexed component of the specified array
+ * object to the specified new value. The new value is first
+ * automatically unwrapped if the array has a primitive component
+ * type.
+ * @param array the array
+ * @param index the index into the array
+ * @param value the new value of the indexed component
+ * @exception NullPointerException If the specified object argument
+ * is null
+ * @exception IllegalArgumentException If the specified object argument
+ * is not an array, or if the array component type is primitive and
+ * an unwrapping conversion fails
+ * @exception ArrayIndexOutOfBoundsException If the specified {@code index}
+ * argument is negative, or if it is greater than or equal to
+ * the length of the specified array
+ */
+ public static native void set(Object array, int index, Object value)
+ throws IllegalArgumentException, ArrayIndexOutOfBoundsException;
+
+ /**
+ * Sets the value of the indexed component of the specified array
+ * object to the specified {@code boolean} value.
+ * @param array the array
+ * @param index the index into the array
+ * @param z the new value of the indexed component
+ * @exception NullPointerException If the specified object argument
+ * is null
+ * @exception IllegalArgumentException If the specified object argument
+ * is not an array, or if the specified value cannot be converted
+ * to the underlying array's component type by an identity or a
+ * primitive widening conversion
+ * @exception ArrayIndexOutOfBoundsException If the specified {@code index}
+ * argument is negative, or if it is greater than or equal to
+ * the length of the specified array
+ * @see Array#set
+ */
+ public static native void setBoolean(Object array, int index, boolean z)
+ throws IllegalArgumentException, ArrayIndexOutOfBoundsException;
+
+ /**
+ * Sets the value of the indexed component of the specified array
+ * object to the specified {@code byte} value.
+ * @param array the array
+ * @param index the index into the array
+ * @param b the new value of the indexed component
+ * @exception NullPointerException If the specified object argument
+ * is null
+ * @exception IllegalArgumentException If the specified object argument
+ * is not an array, or if the specified value cannot be converted
+ * to the underlying array's component type by an identity or a
+ * primitive widening conversion
+ * @exception ArrayIndexOutOfBoundsException If the specified {@code index}
+ * argument is negative, or if it is greater than or equal to
+ * the length of the specified array
+ * @see Array#set
+ */
+ public static native void setByte(Object array, int index, byte b)
+ throws IllegalArgumentException, ArrayIndexOutOfBoundsException;
+
+ /**
+ * Sets the value of the indexed component of the specified array
+ * object to the specified {@code char} value.
+ * @param array the array
+ * @param index the index into the array
+ * @param c the new value of the indexed component
+ * @exception NullPointerException If the specified object argument
+ * is null
+ * @exception IllegalArgumentException If the specified object argument
+ * is not an array, or if the specified value cannot be converted
+ * to the underlying array's component type by an identity or a
+ * primitive widening conversion
+ * @exception ArrayIndexOutOfBoundsException If the specified {@code index}
+ * argument is negative, or if it is greater than or equal to
+ * the length of the specified array
+ * @see Array#set
+ */
+ public static native void setChar(Object array, int index, char c)
+ throws IllegalArgumentException, ArrayIndexOutOfBoundsException;
+
+ /**
+ * Sets the value of the indexed component of the specified array
+ * object to the specified {@code short} value.
+ * @param array the array
+ * @param index the index into the array
+ * @param s the new value of the indexed component
+ * @exception NullPointerException If the specified object argument
+ * is null
+ * @exception IllegalArgumentException If the specified object argument
+ * is not an array, or if the specified value cannot be converted
+ * to the underlying array's component type by an identity or a
+ * primitive widening conversion
+ * @exception ArrayIndexOutOfBoundsException If the specified {@code index}
+ * argument is negative, or if it is greater than or equal to
+ * the length of the specified array
+ * @see Array#set
+ */
+ public static native void setShort(Object array, int index, short s)
+ throws IllegalArgumentException, ArrayIndexOutOfBoundsException;
+
+ /**
+ * Sets the value of the indexed component of the specified array
+ * object to the specified {@code int} value.
+ * @param array the array
+ * @param index the index into the array
+ * @param i the new value of the indexed component
+ * @exception NullPointerException If the specified object argument
+ * is null
+ * @exception IllegalArgumentException If the specified object argument
+ * is not an array, or if the specified value cannot be converted
+ * to the underlying array's component type by an identity or a
+ * primitive widening conversion
+ * @exception ArrayIndexOutOfBoundsException If the specified {@code index}
+ * argument is negative, or if it is greater than or equal to
+ * the length of the specified array
+ * @see Array#set
+ */
+ public static native void setInt(Object array, int index, int i)
+ throws IllegalArgumentException, ArrayIndexOutOfBoundsException;
+
+ /**
+ * Sets the value of the indexed component of the specified array
+ * object to the specified {@code long} value.
+ * @param array the array
+ * @param index the index into the array
+ * @param l the new value of the indexed component
+ * @exception NullPointerException If the specified object argument
+ * is null
+ * @exception IllegalArgumentException If the specified object argument
+ * is not an array, or if the specified value cannot be converted
+ * to the underlying array's component type by an identity or a
+ * primitive widening conversion
+ * @exception ArrayIndexOutOfBoundsException If the specified {@code index}
+ * argument is negative, or if it is greater than or equal to
+ * the length of the specified array
+ * @see Array#set
+ */
+ public static native void setLong(Object array, int index, long l)
+ throws IllegalArgumentException, ArrayIndexOutOfBoundsException;
+
+ /**
+ * Sets the value of the indexed component of the specified array
+ * object to the specified {@code float} value.
+ * @param array the array
+ * @param index the index into the array
+ * @param f the new value of the indexed component
+ * @exception NullPointerException If the specified object argument
+ * is null
+ * @exception IllegalArgumentException If the specified object argument
+ * is not an array, or if the specified value cannot be converted
+ * to the underlying array's component type by an identity or a
+ * primitive widening conversion
+ * @exception ArrayIndexOutOfBoundsException If the specified {@code index}
+ * argument is negative, or if it is greater than or equal to
+ * the length of the specified array
+ * @see Array#set
+ */
+ public static native void setFloat(Object array, int index, float f)
+ throws IllegalArgumentException, ArrayIndexOutOfBoundsException;
+
+ /**
+ * Sets the value of the indexed component of the specified array
+ * object to the specified {@code double} value.
+ * @param array the array
+ * @param index the index into the array
+ * @param d the new value of the indexed component
+ * @exception NullPointerException If the specified object argument
+ * is null
+ * @exception IllegalArgumentException If the specified object argument
+ * is not an array, or if the specified value cannot be converted
+ * to the underlying array's component type by an identity or a
+ * primitive widening conversion
+ * @exception ArrayIndexOutOfBoundsException If the specified {@code index}
+ * argument is negative, or if it is greater than or equal to
+ * the length of the specified array
+ * @see Array#set
+ */
+ public static native void setDouble(Object array, int index, double d)
+ throws IllegalArgumentException, ArrayIndexOutOfBoundsException;
+
+ /*
+ * Private
+ */
+
+ private static native Object newArray(Class componentType, int length)
+ throws NegativeArraySizeException;
+
+ private static native Object multiNewArray(Class componentType,
+ int[] dimensions)
+ throws IllegalArgumentException, NegativeArraySizeException;
+
+
+}
Added: trunk/core/src/openjdk/vm/java/lang/reflect/NativeArray.java
===================================================================
--- trunk/core/src/openjdk/vm/java/lang/reflect/NativeArray.java (rev 0)
+++ trunk/core/src/openjdk/vm/java/lang/reflect/NativeArray.java 2008-12-08 14:51:34 UTC (rev 4783)
@@ -0,0 +1,133 @@
+package java.lang.reflect;
+
+/**
+ * @see java.lang.reflect.Array
+ */
+class NativeArray { //TODO OPTIMIZE IT!
+ /**
+ * @see java.lang.reflect.Array#getLength(java.lang.Object)
+ */
+ private static int getLength(Object arg1) {
+ return CPArray.getLength(arg1);
+ }
+ /**
+ * @see java.lang.reflect.Array#get(java.lang.Object, int)
+ */
+ private static Object get(Object arg1, int arg2) {
+ return CPArray.get(arg1, arg2);
+ }
+ /**
+ * @see java.lang.reflect.Array#getBoolean(java.lang.Object, int)
+ */
+ private static boolean getBoolean(Object arg1, int arg2) {
+ return CPArray.getBoolean(arg1, arg2);
+ }
+ /**
+ * @see java.lang.reflect.Array#getByte(java.lang.Object, int)
+ */
+ private static byte getByte(Object arg1, int arg2) {
+ return CPArray.getByte(arg1, arg2);
+ }
+ /**
+ * @see java.lang.reflect.Array#getChar(java.lang.Object, int)
+ */
+ private static char getChar(Object arg1, int arg2) {
+ return CPArray.getChar(arg1, arg2);
+ }
+ /**
+ * @see java.lang.reflect.Array#getShort(java.lang.Object, int)
+ */
+ private static short getShort(Object arg1, int arg2) {
+ return CPArray.getShort(arg1, arg2);
+ }
+ /**
+ * @see java.lang.reflect.Array#getInt(java.lang.Object, int)
+ */
+ private static int getInt(Object arg1, int arg2) {
+ return CPArray.getInt(arg1, arg2);
+ }
+ /**
+ * @see java.lang.reflect.Array#getLong(java.lang.Object, int)
+ */
+ private static long getLong(Object arg1, int arg2) {
+ return CPArray.getLong(arg1, arg2);
+ }
+ /**
+ * @see java.lang.reflect.Array#getFloat(java.lang.Object, int)
+ */
+ private static float getFloat(Object arg1, int arg2) {
+ return CPArray.getFloat(arg1, arg2);
+ }
+ /**
+ * @see java.lang.reflect.Array#getDouble(java.lang.Object, int)
+ */
+ private static double getDouble(Object arg1, int arg2) {
+ return CPArray.getDouble(arg1, arg2);
+ }
+ /**
+ * @see java.lang.reflect.Array#set(java.lang.Object, int, java.lang.Object)
+ */
+ private static void set(Object arg1, int arg2, Object arg3) {
+ CPArray.set(arg1, arg2, arg3);
+ }
+ /**
+ * @see java.lang.reflect.Array#setBoolean(java.lang.Object, int, boolean)
+ */
+ private static void setBoolean(Object arg1, int arg2, boolean arg3) {
+ CPArray.setBoolean(arg1, arg2, arg3);
+ }
+ /**
+ * @see java.lang.reflect.Array#setByte(java.lang.Object, int, byte)
+ */
+ private static void setByte(Object arg1, int arg2, byte arg3) {
+ CPArray.setByte(arg1, arg2, arg3);
+ }
+ /**
+ * @see java.lang.reflect.Array#setChar(java.lang.Object, int, char)
+ */
+ private static void setChar(Object arg1, int arg2, char arg3) {
+ CPArray.setChar(arg1, arg2, arg3);
+ }
+ /**
+ * @see java.lang.reflect.Array#setShort(java.lang.Object, int, short)
+ */
+ private static void setShort(Object arg1, int arg2, short arg3) {
+ CPArray.setShort(arg1, arg2, arg3);
+ }
+ /**
+ * @see java.lang.reflect.Array#setInt(java.lang.Object, int, int)
+ */
+ private static void setInt(Object arg1, int arg2, int arg3) {
+ CPArray.setInt(arg1, arg2, arg3);
+ }
+ /**
+ * @see java.lang.reflect.Array#setLong(java.lang.Object, int, long)
+ */
+ private static void setLong(Object arg1, int arg2, long arg3) {
+ CPArray.setLong(arg1, arg2, arg3);
+ }
+ /**
+ * @see java.lang.reflect.Array#setFloat(java.lang.Object, int, float)
+ */
+ private static void setFloat(Object arg1, int arg2, float arg3) {
+ CPArray.setFloat(arg1, arg2, arg3);
+ }
+ /**
+ * @see java.lang.reflect.Array#setDouble(java.lang.Object, int, double)
+ */
+ private static void setDouble(Object arg1, int arg2, double arg3) {
+ CPArray.setDouble(arg1, arg2, arg3);
+ }
+ /**
+ * @see java.lang.reflect.Array#newArray(java.lang.Class, int)
+ */
+ private static Object newArray(Class arg1, int arg2) {
+ return CPArray.newInstance(arg1, arg2);
+ }
+ /**
+ * @see java.lang.reflect.Array#multiNewArray(java.lang.Class, int[])
+ */
+ private static Object multiNewArray(Class arg1, int[] arg2) {
+ return CPArray.newInstance(arg1, arg2);
+ }
+}
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|