# HG changeset patch # User Jaroslav Tulach # Date 1399445394 -7200 # Node ID 3471d74a6b9963a4325185c451703628936538f7 # Parent 32531a9626bf46d5a1b9d9c58b6554d1c2d2d87f Methods in Array are shared between different VMs, but other types like Integer, or Float, aren't. Need to be careful when using equality then diff -r 32531a9626bf -r 3471d74a6b99 rt/emul/mini/src/main/java/java/lang/reflect/Array.java --- a/rt/emul/mini/src/main/java/java/lang/reflect/Array.java Tue May 06 17:47:45 2014 +0200 +++ b/rt/emul/mini/src/main/java/java/lang/reflect/Array.java Wed May 07 08:49:54 2014 +0200 @@ -75,40 +75,10 @@ if (length < 0) { throw new NegativeArraySizeException(); } - String sig = findSignature(componentType); + String sig = Method.findArraySignature(componentType); return newArray(componentType.isPrimitive(), sig, null, length); } - private static String findSignature(Class type) { - if (type == Integer.TYPE) { - return "[I"; - } - if (type == Long.TYPE) { - return "[J"; - } - if (type == Double.TYPE) { - return "[D"; - } - if (type == Float.TYPE) { - return "[F"; - } - if (type == Byte.TYPE) { - return "[B"; - } - if (type == Boolean.TYPE) { - return "[Z"; - } - if (type == Short.TYPE) { - return "[S"; - } - if (type == Character.TYPE) { - return "[C"; - } - if (type.getName().equals("void")) { - throw new IllegalStateException("Can't create array for " + type); - } - return "[L" + type.getName().replace('.', '/') + ";"; - } /** * Creates a new array * with the specified component type and dimensions. @@ -148,7 +118,7 @@ for (int i = 1; i < dimensions.length; i++) { sig.append('['); } - sig.append(findSignature(componentType)); + sig.append(Method.findArraySignature(componentType)); return multiNewArray(sig.toString(), dimensions, 0); } @@ -234,7 +204,7 @@ */ public static byte getByte(Object array, int index) throws IllegalArgumentException, ArrayIndexOutOfBoundsException { - if (array.getClass().getComponentType() != Byte.TYPE) { + if (!Method.samePrimitive(array.getClass().getComponentType(), Byte.TYPE)) { throw new IllegalArgumentException(); } byte[] arr = (byte[]) array; @@ -279,7 +249,7 @@ public static short getShort(Object array, int index) throws IllegalArgumentException, ArrayIndexOutOfBoundsException { final Class t = array.getClass().getComponentType(); - if (t == Short.TYPE) { + if (Method.samePrimitive(t, Short.TYPE)) { short[] arr = (short[]) array; return arr[index]; } @@ -305,7 +275,7 @@ public static int getInt(Object array, int index) throws IllegalArgumentException, ArrayIndexOutOfBoundsException { final Class t = array.getClass().getComponentType(); - if (t == Integer.TYPE) { + if (Method.samePrimitive(t, Integer.TYPE)) { int[] arr = (int[]) array; return arr[index]; } @@ -331,7 +301,7 @@ public static long getLong(Object array, int index) throws IllegalArgumentException, ArrayIndexOutOfBoundsException { final Class t = array.getClass().getComponentType(); - if (t == Long.TYPE) { + if (Method.samePrimitive(t, Long.TYPE)) { long[] arr = (long[]) array; return arr[index]; } @@ -357,7 +327,7 @@ public static float getFloat(Object array, int index) throws IllegalArgumentException, ArrayIndexOutOfBoundsException { final Class t = array.getClass().getComponentType(); - if (t == Float.TYPE) { + if (Method.samePrimitive(t, Float.TYPE)) { float[] arr = (float[]) array; return arr[index]; } @@ -383,7 +353,7 @@ public static double getDouble(Object array, int index) throws IllegalArgumentException, ArrayIndexOutOfBoundsException { final Class t = array.getClass().getComponentType(); - if (t == Double.TYPE) { + if (Method.samePrimitive(t, Double.TYPE)) { double[] arr = (double[]) array; return arr[index]; } @@ -457,7 +427,7 @@ public static void setByte(Object array, int index, byte b) throws IllegalArgumentException, ArrayIndexOutOfBoundsException { Class t = array.getClass().getComponentType(); - if (t == Byte.TYPE) { + if (Method.samePrimitive(t, Byte.TYPE)) { byte[] arr = (byte[]) array; arr[index] = b; } else { @@ -505,7 +475,7 @@ public static void setShort(Object array, int index, short s) throws IllegalArgumentException, ArrayIndexOutOfBoundsException { Class t = array.getClass().getComponentType(); - if (t == Short.TYPE) { + if (Method.samePrimitive(t, Short.TYPE)) { short[] arr = (short[]) array; arr[index] = s; } else { @@ -534,7 +504,7 @@ public static void setInt(Object array, int index, int i) throws IllegalArgumentException, ArrayIndexOutOfBoundsException { Class t = array.getClass().getComponentType(); - if (t == Integer.TYPE) { + if (Method.samePrimitive(t, Integer.TYPE)) { int[] arr = (int[]) array; arr[index] = i; } else { @@ -562,7 +532,7 @@ public static void setLong(Object array, int index, long l) throws IllegalArgumentException, ArrayIndexOutOfBoundsException { Class t = array.getClass().getComponentType(); - if (t == Long.TYPE) { + if (Method.samePrimitive(t, Long.TYPE)) { long[] arr = (long[]) array; arr[index] = l; } else { @@ -590,7 +560,7 @@ public static void setFloat(Object array, int index, float f) throws IllegalArgumentException, ArrayIndexOutOfBoundsException { Class t = array.getClass().getComponentType(); - if (t == Float.TYPE) { + if (Method.samePrimitive(t, Float.TYPE)) { float[] arr = (float[])array; arr[index] = f; } else { @@ -618,7 +588,7 @@ public static void setDouble(Object array, int index, double d) throws IllegalArgumentException, ArrayIndexOutOfBoundsException { Class t = array.getClass().getComponentType(); - if (t == Double.TYPE) { + if (Method.samePrimitive(t, Double.TYPE)) { double[] arr = (double[])array; arr[index] = d; } else { diff -r 32531a9626bf -r 3471d74a6b99 rt/emul/mini/src/main/java/java/lang/reflect/Method.java --- a/rt/emul/mini/src/main/java/java/lang/reflect/Method.java Tue May 06 17:47:45 2014 +0200 +++ b/rt/emul/mini/src/main/java/java/lang/reflect/Method.java Wed May 07 08:49:54 2014 +0200 @@ -552,28 +552,28 @@ } static Object fromPrimitive(Class type, Object o) { - if (type == Integer.TYPE) { + if (samePrimitive(type, Integer.TYPE)) { return fromRaw(Integer.class, "valueOf__Ljava_lang_Integer_2I", o); } - if (type == Long.TYPE) { + if (samePrimitive(type, Long.TYPE)) { return fromRaw(Long.class, "valueOf__Ljava_lang_Long_2J", o); } - if (type == Double.TYPE) { + if (samePrimitive(type, Double.TYPE)) { return fromRaw(Double.class, "valueOf__Ljava_lang_Double_2D", o); } - if (type == Float.TYPE) { + if (samePrimitive(type, Float.TYPE)) { return fromRaw(Float.class, "valueOf__Ljava_lang_Float_2F", o); } - if (type == Byte.TYPE) { + if (samePrimitive(type, Byte.TYPE)) { return fromRaw(Byte.class, "valueOf__Ljava_lang_Byte_2B", o); } - if (type == Boolean.TYPE) { + if (samePrimitive(type, Boolean.TYPE)) { return fromRaw(Boolean.class, "valueOf__Ljava_lang_Boolean_2Z", o); } - if (type == Short.TYPE) { + if (samePrimitive(type, Short.TYPE)) { return fromRaw(Short.class, "valueOf__Ljava_lang_Short_2S", o); } - if (type == Character.TYPE) { + if (samePrimitive(type, Character.TYPE)) { return fromRaw(Character.class, "valueOf__Ljava_lang_Character_2C", o); } if (type.getName().equals("void")) { @@ -581,6 +581,46 @@ } throw new IllegalStateException("Can't convert " + o); } + static boolean samePrimitive(Class c1, Class c2) { + if (c1 == c2) { + return true; + } + if (c1.isPrimitive()) { + return c1.getName().equals(c2.getName()); + } + return false; + } + + static String findArraySignature(Class type) { + if (!type.isPrimitive()) { + return "[L" + type.getName().replace('.', '/') + ";"; + } + if (samePrimitive(type, Integer.TYPE)) { + return "[I"; + } + if (samePrimitive(type, Long.TYPE)) { + return "[J"; + } + if (samePrimitive(type, Double.TYPE)) { + return "[D"; + } + if (samePrimitive(type, Float.TYPE)) { + return "[F"; + } + if (samePrimitive(type, Byte.TYPE)) { + return "[B"; + } + if (samePrimitive(type, Boolean.TYPE)) { + return "[Z"; + } + if (samePrimitive(type, Short.TYPE)) { + return "[S"; + } + if (samePrimitive(type, Character.TYPE)) { + return "[C"; + } + throw new IllegalStateException("Can't create array for " + type); + } @JavaScriptBody(args = { "cls", "m", "o" }, body = "return cls.cnstr(false)[m](o);"