diff options
Diffstat (limited to 'src/com/vaadin/terminal/gwt/server/JsonCodec.java')
-rw-r--r-- | src/com/vaadin/terminal/gwt/server/JsonCodec.java | 45 |
1 files changed, 34 insertions, 11 deletions
diff --git a/src/com/vaadin/terminal/gwt/server/JsonCodec.java b/src/com/vaadin/terminal/gwt/server/JsonCodec.java index d3a2ef56f8..8199bc6ada 100644 --- a/src/com/vaadin/terminal/gwt/server/JsonCodec.java +++ b/src/com/vaadin/terminal/gwt/server/JsonCodec.java @@ -9,10 +9,12 @@ 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; import java.lang.reflect.Type; +import java.lang.reflect.WildcardType; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; @@ -27,9 +29,9 @@ import java.util.Set; import com.vaadin.external.json.JSONArray; import com.vaadin.external.json.JSONException; import com.vaadin.external.json.JSONObject; -import com.vaadin.terminal.gwt.client.Connector; +import com.vaadin.shared.Connector; +import com.vaadin.shared.communication.UidlValue; import com.vaadin.terminal.gwt.client.communication.JsonEncoder; -import com.vaadin.terminal.gwt.client.communication.UidlValue; import com.vaadin.ui.Component; import com.vaadin.ui.ConnectorTracker; @@ -100,8 +102,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 +142,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 +159,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); @@ -513,6 +523,11 @@ public class JsonCodec implements Serializable { throw new IllegalArgumentException("type must be defined"); } + if (valueType instanceof WildcardType) { + throw new IllegalStateException( + "Can not serialize type with wildcard: " + valueType); + } + if (null == value) { return encodeNull(); } @@ -540,7 +555,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 +666,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)); |