]> source.dussan.org Git - vaadin-framework.git/commitdiff
Support array as a generic type parameter (#8861)
authorLeif Åstrand <leif@vaadin.com>
Tue, 3 Jul 2012 07:30:43 +0000 (10:30 +0300)
committerLeif Åstrand <leif@vaadin.com>
Tue, 3 Jul 2012 07:30:43 +0000 (10:30 +0300)
src/com/vaadin/terminal/gwt/server/JsonCodec.java
tests/testbench/com/vaadin/tests/serialization/SerializerTest.java

index d3a2ef56f888cd681444e5a0bbac4431ceae8562..8091a1d205e8e55a81c23337b9d3d872f9eee0da 100644 (file)
@@ -9,6 +9,7 @@ import java.beans.Introspector;
 import java.beans.PropertyDescriptor;
 import java.io.Serializable;
 import java.lang.reflect.Array;
+import java.lang.reflect.GenericArrayType;
 import java.lang.reflect.InvocationTargetException;
 import java.lang.reflect.Method;
 import java.lang.reflect.ParameterizedType;
@@ -100,8 +101,10 @@ public class JsonCodec implements Serializable {
     private static Class<?> getClassForType(Type type) {
         if (type instanceof ParameterizedType) {
             return (Class<?>) (((ParameterizedType) type).getRawType());
-        } else {
+        } else if (type instanceof Class<?>) {
             return (Class<?>) type;
+        } else {
+            return null;
         }
     }
 
@@ -138,7 +141,13 @@ public class JsonCodec implements Serializable {
                 && ((Class<?>) targetType).isArray()) {
             // Legacy Object[] and String[] handled elsewhere, this takes care
             // of generic arrays
-            return decodeArray((Class<?>) targetType, (JSONArray) value,
+            Class<?> componentType = ((Class<?>) targetType).getComponentType();
+            return decodeArray(componentType, (JSONArray) value,
+                    connectorTracker);
+        } else if (targetType instanceof GenericArrayType) {
+            Type componentType = ((GenericArrayType) targetType)
+                    .getGenericComponentType();
+            return decodeArray(componentType, (JSONArray) value,
                     connectorTracker);
         } else if (targetType == JSONObject.class
                 || targetType == JSONArray.class) {
@@ -149,10 +158,10 @@ public class JsonCodec implements Serializable {
         }
     }
 
-    private static Object decodeArray(Class<?> targetType, JSONArray value,
+    private static Object decodeArray(Type componentType, JSONArray value,
             ConnectorTracker connectorTracker) throws JSONException {
-        Class<?> componentType = targetType.getComponentType();
-        Object array = Array.newInstance(componentType, value.length());
+        Class<?> componentClass = getClassForType(componentType);
+        Object array = Array.newInstance(componentClass, value.length());
         for (int i = 0; i < value.length(); i++) {
             Object decodedValue = decodeInternalOrCustomType(componentType,
                     value.get(i), connectorTracker);
@@ -540,7 +549,15 @@ public class JsonCodec implements Serializable {
             return jsonArray;
         } else if (valueType instanceof Class<?>
                 && ((Class<?>) valueType).isArray()) {
-            JSONArray jsonArray = encodeArrayContents(value, connectorTracker);
+            JSONArray jsonArray = encodeArrayContents(
+                    ((Class<?>) valueType).getComponentType(), value,
+                    connectorTracker);
+            return jsonArray;
+        } else if (valueType instanceof GenericArrayType) {
+            Type componentType = ((GenericArrayType) valueType)
+                    .getGenericComponentType();
+            JSONArray jsonArray = encodeArrayContents(componentType, value,
+                    connectorTracker);
             return jsonArray;
         } else if (value instanceof Map) {
             Object jsonMap = encodeMap(valueType, (Map<?, ?>) value,
@@ -643,10 +660,10 @@ public class JsonCodec implements Serializable {
         return e.name();
     }
 
-    private static JSONArray encodeArrayContents(Object array,
-            ConnectorTracker connectorTracker) throws JSONException {
+    private static JSONArray encodeArrayContents(Type componentType,
+            Object array, ConnectorTracker connectorTracker)
+            throws JSONException {
         JSONArray jsonArray = new JSONArray();
-        Class<?> componentType = array.getClass().getComponentType();
         for (int i = 0; i < Array.getLength(array); i++) {
             jsonArray.put(encode(Array.get(array, i), null, componentType,
                     connectorTracker));
index d0b672fbf9d4b6ddf7557fc0b3fce1a978fb7e00..881a3c3be19aee35ebcb044b831da769cda532b5 100644 (file)
@@ -63,13 +63,12 @@ public class SerializerTest extends AbstractTestRoot {
         rpc.sendList(Arrays.asList(5, 8, -234), Arrays.<Connector> asList(this,
                 testExtension), Arrays.asList(new SimpleTestBean(234),
                 new SimpleTestBean(-568)));
-        // Disabled because of #8861
-        // rpc.sendArrayList(
-        // Arrays.asList(new int[] { 1, 2 }, new int[] { 3, 4 }),
-        // Arrays.asList(new Integer[] { 5, 6 }, new Integer[] { 7, 8 }),
-        // Collections
-        // .singletonList(new SimpleTestBean[] { new SimpleTestBean(
-        // 7) }));
+        rpc.sendArrayList(
+                Arrays.asList(new int[] { 1, 2 }, new int[] { 3, 4 }),
+                Arrays.asList(new Integer[] { 5, 6 }, new Integer[] { 7, 8 }),
+                Collections
+                        .singletonList(new SimpleTestBean[] { new SimpleTestBean(
+                                7) }));
         // Disabled because of #8861
         // rpc.sendListArray(
         // new List[] { Arrays.asList(1, 2), Arrays.asList(3, 4) },
@@ -199,8 +198,10 @@ public class SerializerTest extends AbstractTestRoot {
             public void sendArrayList(List<int[]> primitiveArrayList,
                     List<Integer[]> objectArrayList,
                     List<SimpleTestBean[]> beanArrayList) {
-                log.log("sendArrayList: " + primitiveArrayList + ", "
-                        + objectArrayList + ", " + beanArrayList);
+                log.log("sendArrayList: "
+                        + Arrays.deepToString(primitiveArrayList.toArray())
+                        + ", " + Arrays.deepToString(objectArrayList.toArray())
+                        + ", " + Arrays.deepToString(beanArrayList.toArray()));
             }
 
             public void sendListArray(List<Integer>[] objectListArray,