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 closure
authorJaroslav Tulach <jaroslav.tulach@apidesign.org>
Wed, 07 May 2014 08:49:54 +0200
branchclosure
changeset 15413471d74a6b99
parent 1540 32531a9626bf
child 1542 12a95b773a39
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
rt/emul/mini/src/main/java/java/lang/reflect/Array.java
rt/emul/mini/src/main/java/java/lang/reflect/Method.java
     1.1 --- a/rt/emul/mini/src/main/java/java/lang/reflect/Array.java	Tue May 06 17:47:45 2014 +0200
     1.2 +++ b/rt/emul/mini/src/main/java/java/lang/reflect/Array.java	Wed May 07 08:49:54 2014 +0200
     1.3 @@ -75,40 +75,10 @@
     1.4          if (length < 0) {
     1.5              throw new NegativeArraySizeException();
     1.6          }
     1.7 -        String sig = findSignature(componentType);
     1.8 +        String sig = Method.findArraySignature(componentType);
     1.9          return newArray(componentType.isPrimitive(), sig, null, length);
    1.10      }
    1.11      
    1.12 -    private static String findSignature(Class<?> type) {
    1.13 -        if (type == Integer.TYPE) {
    1.14 -            return "[I";
    1.15 -        }
    1.16 -        if (type == Long.TYPE) {
    1.17 -            return "[J";
    1.18 -        }
    1.19 -        if (type == Double.TYPE) {
    1.20 -            return "[D";
    1.21 -        }
    1.22 -        if (type == Float.TYPE) {
    1.23 -            return "[F";
    1.24 -        }
    1.25 -        if (type == Byte.TYPE) {
    1.26 -            return "[B";
    1.27 -        }
    1.28 -        if (type == Boolean.TYPE) {
    1.29 -            return "[Z";
    1.30 -        }
    1.31 -        if (type == Short.TYPE) {
    1.32 -            return "[S";
    1.33 -        }
    1.34 -        if (type == Character.TYPE) {
    1.35 -            return "[C";
    1.36 -        }
    1.37 -        if (type.getName().equals("void")) {
    1.38 -            throw new IllegalStateException("Can't create array for " + type);
    1.39 -        }
    1.40 -        return "[L" + type.getName().replace('.', '/') + ";";
    1.41 -    }
    1.42      /**
    1.43       * Creates a new array
    1.44       * with the specified component type and dimensions.
    1.45 @@ -148,7 +118,7 @@
    1.46          for (int i = 1; i < dimensions.length; i++) {
    1.47              sig.append('[');
    1.48          }
    1.49 -        sig.append(findSignature(componentType));
    1.50 +        sig.append(Method.findArraySignature(componentType));
    1.51          return multiNewArray(sig.toString(), dimensions, 0);
    1.52      }
    1.53  
    1.54 @@ -234,7 +204,7 @@
    1.55       */
    1.56      public static byte getByte(Object array, int index)
    1.57      throws IllegalArgumentException, ArrayIndexOutOfBoundsException {
    1.58 -        if (array.getClass().getComponentType() != Byte.TYPE) {
    1.59 +        if (!Method.samePrimitive(array.getClass().getComponentType(), Byte.TYPE)) {
    1.60              throw new IllegalArgumentException();
    1.61          }
    1.62          byte[] arr = (byte[]) array;
    1.63 @@ -279,7 +249,7 @@
    1.64      public static short getShort(Object array, int index)
    1.65      throws IllegalArgumentException, ArrayIndexOutOfBoundsException {
    1.66          final Class<?> t = array.getClass().getComponentType();
    1.67 -        if (t == Short.TYPE) {
    1.68 +        if (Method.samePrimitive(t, Short.TYPE)) {
    1.69              short[] arr = (short[]) array;
    1.70              return arr[index];
    1.71          }
    1.72 @@ -305,7 +275,7 @@
    1.73      public static int getInt(Object array, int index) 
    1.74      throws IllegalArgumentException, ArrayIndexOutOfBoundsException {
    1.75          final Class<?> t = array.getClass().getComponentType();
    1.76 -        if (t == Integer.TYPE) {
    1.77 +        if (Method.samePrimitive(t, Integer.TYPE)) {
    1.78              int[] arr = (int[]) array;
    1.79              return arr[index];
    1.80          }
    1.81 @@ -331,7 +301,7 @@
    1.82      public static long getLong(Object array, int index)
    1.83      throws IllegalArgumentException, ArrayIndexOutOfBoundsException {
    1.84          final Class<?> t = array.getClass().getComponentType();
    1.85 -        if (t == Long.TYPE) {
    1.86 +        if (Method.samePrimitive(t, Long.TYPE)) {
    1.87              long[] arr = (long[]) array;
    1.88              return arr[index];
    1.89          }
    1.90 @@ -357,7 +327,7 @@
    1.91      public static float getFloat(Object array, int index)
    1.92      throws IllegalArgumentException, ArrayIndexOutOfBoundsException {
    1.93          final Class<?> t = array.getClass().getComponentType();
    1.94 -        if (t == Float.TYPE) {
    1.95 +        if (Method.samePrimitive(t, Float.TYPE)) {
    1.96              float[] arr = (float[]) array;
    1.97              return arr[index];
    1.98          }
    1.99 @@ -383,7 +353,7 @@
   1.100      public static double getDouble(Object array, int index)
   1.101      throws IllegalArgumentException, ArrayIndexOutOfBoundsException {
   1.102          final Class<?> t = array.getClass().getComponentType();
   1.103 -        if (t == Double.TYPE) {
   1.104 +        if (Method.samePrimitive(t, Double.TYPE)) {
   1.105              double[] arr = (double[]) array;
   1.106              return arr[index];
   1.107          }
   1.108 @@ -457,7 +427,7 @@
   1.109      public static void setByte(Object array, int index, byte b)
   1.110      throws IllegalArgumentException, ArrayIndexOutOfBoundsException {
   1.111          Class<?> t = array.getClass().getComponentType();
   1.112 -        if (t == Byte.TYPE) {
   1.113 +        if (Method.samePrimitive(t, Byte.TYPE)) {
   1.114              byte[] arr = (byte[]) array;
   1.115              arr[index] = b;
   1.116          } else {
   1.117 @@ -505,7 +475,7 @@
   1.118      public static void setShort(Object array, int index, short s)
   1.119      throws IllegalArgumentException, ArrayIndexOutOfBoundsException {
   1.120          Class<?> t = array.getClass().getComponentType();
   1.121 -        if (t == Short.TYPE) {
   1.122 +        if (Method.samePrimitive(t, Short.TYPE)) {
   1.123              short[] arr = (short[]) array;
   1.124              arr[index] = s;
   1.125          } else {
   1.126 @@ -534,7 +504,7 @@
   1.127      public static void setInt(Object array, int index, int i)
   1.128      throws IllegalArgumentException, ArrayIndexOutOfBoundsException {
   1.129          Class<?> t = array.getClass().getComponentType();
   1.130 -        if (t == Integer.TYPE) {
   1.131 +        if (Method.samePrimitive(t, Integer.TYPE)) {
   1.132              int[] arr = (int[]) array;
   1.133              arr[index] = i;
   1.134          } else {
   1.135 @@ -562,7 +532,7 @@
   1.136      public static void setLong(Object array, int index, long l)
   1.137      throws IllegalArgumentException, ArrayIndexOutOfBoundsException {
   1.138          Class<?> t = array.getClass().getComponentType();
   1.139 -        if (t == Long.TYPE) {
   1.140 +        if (Method.samePrimitive(t, Long.TYPE)) {
   1.141              long[] arr = (long[]) array;
   1.142              arr[index] = l;
   1.143          } else {
   1.144 @@ -590,7 +560,7 @@
   1.145      public static void setFloat(Object array, int index, float f)
   1.146      throws IllegalArgumentException, ArrayIndexOutOfBoundsException {
   1.147          Class<?> t = array.getClass().getComponentType();
   1.148 -        if (t == Float.TYPE) {
   1.149 +        if (Method.samePrimitive(t, Float.TYPE)) {
   1.150              float[] arr = (float[])array;
   1.151              arr[index] = f;
   1.152          } else {
   1.153 @@ -618,7 +588,7 @@
   1.154      public static void setDouble(Object array, int index, double d)
   1.155      throws IllegalArgumentException, ArrayIndexOutOfBoundsException {
   1.156          Class<?> t = array.getClass().getComponentType();
   1.157 -        if (t == Double.TYPE) {
   1.158 +        if (Method.samePrimitive(t, Double.TYPE)) {
   1.159              double[] arr = (double[])array;
   1.160              arr[index] = d;
   1.161          } else {
     2.1 --- a/rt/emul/mini/src/main/java/java/lang/reflect/Method.java	Tue May 06 17:47:45 2014 +0200
     2.2 +++ b/rt/emul/mini/src/main/java/java/lang/reflect/Method.java	Wed May 07 08:49:54 2014 +0200
     2.3 @@ -552,28 +552,28 @@
     2.4      }
     2.5  
     2.6      static Object fromPrimitive(Class<?> type, Object o) {
     2.7 -        if (type == Integer.TYPE) {
     2.8 +        if (samePrimitive(type, Integer.TYPE)) {
     2.9              return fromRaw(Integer.class, "valueOf__Ljava_lang_Integer_2I", o);
    2.10          }
    2.11 -        if (type == Long.TYPE) {
    2.12 +        if (samePrimitive(type, Long.TYPE)) {
    2.13              return fromRaw(Long.class, "valueOf__Ljava_lang_Long_2J", o);
    2.14          }
    2.15 -        if (type == Double.TYPE) {
    2.16 +        if (samePrimitive(type, Double.TYPE)) {
    2.17              return fromRaw(Double.class, "valueOf__Ljava_lang_Double_2D", o);
    2.18          }
    2.19 -        if (type == Float.TYPE) {
    2.20 +        if (samePrimitive(type, Float.TYPE)) {
    2.21              return fromRaw(Float.class, "valueOf__Ljava_lang_Float_2F", o);
    2.22          }
    2.23 -        if (type == Byte.TYPE) {
    2.24 +        if (samePrimitive(type, Byte.TYPE)) {
    2.25              return fromRaw(Byte.class, "valueOf__Ljava_lang_Byte_2B", o);
    2.26          }
    2.27 -        if (type == Boolean.TYPE) {
    2.28 +        if (samePrimitive(type, Boolean.TYPE)) {
    2.29              return fromRaw(Boolean.class, "valueOf__Ljava_lang_Boolean_2Z", o);
    2.30          }
    2.31 -        if (type == Short.TYPE) {
    2.32 +        if (samePrimitive(type, Short.TYPE)) {
    2.33              return fromRaw(Short.class, "valueOf__Ljava_lang_Short_2S", o);
    2.34          }
    2.35 -        if (type == Character.TYPE) {
    2.36 +        if (samePrimitive(type, Character.TYPE)) {
    2.37              return fromRaw(Character.class, "valueOf__Ljava_lang_Character_2C", o);
    2.38          }
    2.39          if (type.getName().equals("void")) {
    2.40 @@ -581,6 +581,46 @@
    2.41          }
    2.42          throw new IllegalStateException("Can't convert " + o);
    2.43      }
    2.44 +    static boolean samePrimitive(Class<?> c1, Class<?> c2) {
    2.45 +        if (c1 == c2) {
    2.46 +            return true;
    2.47 +        }
    2.48 +        if (c1.isPrimitive()) {
    2.49 +            return c1.getName().equals(c2.getName());
    2.50 +        }
    2.51 +        return false;
    2.52 +    }
    2.53 +
    2.54 +    static String findArraySignature(Class<?> type) {
    2.55 +        if (!type.isPrimitive()) {
    2.56 +            return "[L" + type.getName().replace('.', '/') + ";";
    2.57 +        }
    2.58 +        if (samePrimitive(type, Integer.TYPE)) {
    2.59 +            return "[I";
    2.60 +        }
    2.61 +        if (samePrimitive(type, Long.TYPE)) {
    2.62 +            return "[J";
    2.63 +        }
    2.64 +        if (samePrimitive(type, Double.TYPE)) {
    2.65 +            return "[D";
    2.66 +        }
    2.67 +        if (samePrimitive(type, Float.TYPE)) {
    2.68 +            return "[F";
    2.69 +        }
    2.70 +        if (samePrimitive(type, Byte.TYPE)) {
    2.71 +            return "[B";
    2.72 +        }
    2.73 +        if (samePrimitive(type, Boolean.TYPE)) {
    2.74 +            return "[Z";
    2.75 +        }
    2.76 +        if (samePrimitive(type, Short.TYPE)) {
    2.77 +            return "[S";
    2.78 +        }
    2.79 +        if (samePrimitive(type, Character.TYPE)) {
    2.80 +            return "[C";
    2.81 +        }
    2.82 +        throw new IllegalStateException("Can't create array for " + type);
    2.83 +    }
    2.84      
    2.85      @JavaScriptBody(args = { "cls", "m", "o" }, 
    2.86          body = "return cls.cnstr(false)[m](o);"