# HG changeset patch # User Jaroslav Tulach # Date 1358539450 -3600 # Node ID 7e39f344e4a05a56e0831fe402f140dfc64d20a0 # Parent 05a87bc2319222147d20723eb9ccab336fb9a9f5 Getters for basic primitive types diff -r 05a87bc23192 -r 7e39f344e4a0 emul/src/main/java/java/lang/reflect/Array.java --- a/emul/src/main/java/java/lang/reflect/Array.java Fri Jan 18 19:22:49 2013 +0100 +++ b/emul/src/main/java/java/lang/reflect/Array.java Fri Jan 18 21:04:10 2013 +0100 @@ -162,7 +162,7 @@ public static int getLength(Object array) throws IllegalArgumentException { if (!array.getClass().isArray()) { - throw new IllegalArgumentException(); + throw new IllegalArgumentException("Argument is not an array"); } return length(array); } @@ -224,8 +224,14 @@ * length of the specified array * @see Array#get */ - public static native byte getByte(Object array, int index) - throws IllegalArgumentException, ArrayIndexOutOfBoundsException; + public static byte getByte(Object array, int index) + throws IllegalArgumentException, ArrayIndexOutOfBoundsException { + if (array.getClass().getComponentType() != Byte.TYPE) { + throw new IllegalArgumentException(); + } + byte[] arr = (byte[]) array; + return arr[index]; + } /** * Returns the value of the indexed component in the specified @@ -262,8 +268,15 @@ * length of the specified array * @see Array#get */ - public static native short getShort(Object array, int index) - throws IllegalArgumentException, ArrayIndexOutOfBoundsException; + public static short getShort(Object array, int index) + throws IllegalArgumentException, ArrayIndexOutOfBoundsException { + final Class t = array.getClass().getComponentType(); + if (t == Short.TYPE) { + short[] arr = (short[]) array; + return arr[index]; + } + return getByte(array, index); + } /** * Returns the value of the indexed component in the specified @@ -281,8 +294,15 @@ * length of the specified array * @see Array#get */ - public static native int getInt(Object array, int index) - throws IllegalArgumentException, ArrayIndexOutOfBoundsException; + public static int getInt(Object array, int index) + throws IllegalArgumentException, ArrayIndexOutOfBoundsException { + final Class t = array.getClass().getComponentType(); + if (t == Integer.TYPE) { + int[] arr = (int[]) array; + return arr[index]; + } + return getShort(array, index); + } /** * Returns the value of the indexed component in the specified @@ -300,8 +320,15 @@ * length of the specified array * @see Array#get */ - public static native long getLong(Object array, int index) - throws IllegalArgumentException, ArrayIndexOutOfBoundsException; + public static long getLong(Object array, int index) + throws IllegalArgumentException, ArrayIndexOutOfBoundsException { + final Class t = array.getClass().getComponentType(); + if (t == Long.TYPE) { + long[] arr = (long[]) array; + return arr[index]; + } + return getInt(array, index); + } /** * Returns the value of the indexed component in the specified @@ -319,8 +346,15 @@ * length of the specified array * @see Array#get */ - public static native float getFloat(Object array, int index) - throws IllegalArgumentException, ArrayIndexOutOfBoundsException; + public static float getFloat(Object array, int index) + throws IllegalArgumentException, ArrayIndexOutOfBoundsException { + final Class t = array.getClass().getComponentType(); + if (t == Float.TYPE) { + float[] arr = (float[]) array; + return arr[index]; + } + return getLong(array, index); + } /** * Returns the value of the indexed component in the specified @@ -338,8 +372,15 @@ * length of the specified array * @see Array#get */ - public static native double getDouble(Object array, int index) - throws IllegalArgumentException, ArrayIndexOutOfBoundsException; + public static double getDouble(Object array, int index) + throws IllegalArgumentException, ArrayIndexOutOfBoundsException { + final Class t = array.getClass().getComponentType(); + if (t == Double.TYPE) { + double[] arr = (double[]) array; + return arr[index]; + } + return getFloat(array, index); + } /** * Sets the value of the indexed component of the specified array