From cbb6310058903ac5e994494ccee78363e0682505 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Leif=20=C3=85strand?= Date: Tue, 3 Jul 2012 10:30:43 +0300 Subject: [PATCH] Support array as a generic type parameter (#8861) --- .../vaadin/terminal/gwt/server/JsonCodec.java | 35 ++++++++++++++----- .../tests/serialization/SerializerTest.java | 19 +++++----- 2 files changed, 36 insertions(+), 18 deletions(-) diff --git a/src/com/vaadin/terminal/gwt/server/JsonCodec.java b/src/com/vaadin/terminal/gwt/server/JsonCodec.java index d3a2ef56f8..8091a1d205 100644 --- a/src/com/vaadin/terminal/gwt/server/JsonCodec.java +++ b/src/com/vaadin/terminal/gwt/server/JsonCodec.java @@ -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)); diff --git a/tests/testbench/com/vaadin/tests/serialization/SerializerTest.java b/tests/testbench/com/vaadin/tests/serialization/SerializerTest.java index d0b672fbf9..881a3c3be1 100644 --- a/tests/testbench/com/vaadin/tests/serialization/SerializerTest.java +++ b/tests/testbench/com/vaadin/tests/serialization/SerializerTest.java @@ -63,13 +63,12 @@ public class SerializerTest extends AbstractTestRoot { rpc.sendList(Arrays.asList(5, 8, -234), Arrays. 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 primitiveArrayList, List objectArrayList, List 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[] objectListArray, -- 2.39.5