瀏覽代碼

Pass declared types to JsonDecoder and use them (#8677)

tags/7.0.0.alpha3
Leif Åstrand 12 年之前
父節點
當前提交
3702e37192

+ 14
- 5
src/com/vaadin/terminal/gwt/client/ApplicationConnection.java 查看文件

import com.vaadin.terminal.gwt.client.communication.MethodInvocation; import com.vaadin.terminal.gwt.client.communication.MethodInvocation;
import com.vaadin.terminal.gwt.client.communication.RpcManager; import com.vaadin.terminal.gwt.client.communication.RpcManager;
import com.vaadin.terminal.gwt.client.communication.SerializerMap; import com.vaadin.terminal.gwt.client.communication.SerializerMap;
import com.vaadin.terminal.gwt.client.communication.SharedState;
import com.vaadin.terminal.gwt.client.communication.StateChangeEvent; import com.vaadin.terminal.gwt.client.communication.StateChangeEvent;
import com.vaadin.terminal.gwt.client.communication.Type;
import com.vaadin.terminal.gwt.client.communication.UidlValue; import com.vaadin.terminal.gwt.client.communication.UidlValue;
import com.vaadin.terminal.gwt.client.ui.AbstractComponentConnector; import com.vaadin.terminal.gwt.client.ui.AbstractComponentConnector;
import com.vaadin.terminal.gwt.client.ui.VContextMenu; import com.vaadin.terminal.gwt.client.ui.VContextMenu;
JSONArray stateDataAndType = new JSONArray( JSONArray stateDataAndType = new JSONArray(
states.getJavaScriptObject(connectorId)); states.getJavaScriptObject(connectorId));


JsonDecoder.decodeValue(stateDataAndType,
connector.getState(),
SharedState state = connector.getState();
JsonDecoder.decodeValue(new Type(state.getClass()
.getName(), null), stateDataAndType, state,
ApplicationConnection.this); ApplicationConnection.this);


StateChangeEvent event = GWT StateChangeEvent event = GWT
String interfaceName = ((JSONString) rpcCall.get(1)).stringValue(); String interfaceName = ((JSONString) rpcCall.get(1)).stringValue();
String methodName = ((JSONString) rpcCall.get(2)).stringValue(); String methodName = ((JSONString) rpcCall.get(2)).stringValue();
JSONArray parametersJson = (JSONArray) rpcCall.get(3); JSONArray parametersJson = (JSONArray) rpcCall.get(3);

MethodInvocation methodInvocation = new MethodInvocation(connectorId,
interfaceName, methodName);
Type[] parameterTypes = rpcManager.getParameterTypes(methodInvocation);

Object[] parameters = new Object[parametersJson.size()]; Object[] parameters = new Object[parametersJson.size()];
for (int j = 0; j < parametersJson.size(); ++j) { for (int j = 0; j < parametersJson.size(); ++j) {
parameters[j] = JsonDecoder.decodeValue(
parameters[j] = JsonDecoder.decodeValue(parameterTypes[j],
(JSONArray) parametersJson.get(j), null, this); (JSONArray) parametersJson.get(j), null, this);
} }
return new MethodInvocation(connectorId, interfaceName, methodName,
parameters);

methodInvocation.setParameters(parameters);
return methodInvocation;
} }


// Redirect browser, null reloads current page // Redirect browser, null reloads current page

+ 62
- 64
src/com/vaadin/terminal/gwt/client/communication/JsonDecoder.java 查看文件

import com.google.gwt.json.client.JSONString; import com.google.gwt.json.client.JSONString;
import com.google.gwt.json.client.JSONValue; import com.google.gwt.json.client.JSONValue;
import com.vaadin.terminal.gwt.client.ApplicationConnection; import com.vaadin.terminal.gwt.client.ApplicationConnection;
import com.vaadin.terminal.gwt.client.Connector;
import com.vaadin.terminal.gwt.client.ConnectorMap; import com.vaadin.terminal.gwt.client.ConnectorMap;
import com.vaadin.terminal.gwt.client.ServerConnector; import com.vaadin.terminal.gwt.client.ServerConnector;


* reference to the current ApplicationConnection * reference to the current ApplicationConnection
* @return decoded value (does not contain JSON types) * @return decoded value (does not contain JSON types)
*/ */
public static Object decodeValue(JSONArray jsonArray, Object target,
ApplicationConnection connection) {
String type = ((JSONString) jsonArray.get(0)).stringValue();
public static Object decodeValue(Type type, JSONArray jsonArray,
Object target, ApplicationConnection connection) {
return decodeValue(type, jsonArray.get(1), target, connection); return decodeValue(type, jsonArray.get(1), target, connection);
} }


private static Object decodeValue(String variableType, JSONValue value,
private static Object decodeValue(Type type, JSONValue jsonValue,
Object target, ApplicationConnection connection) { Object target, ApplicationConnection connection) {
Object val = null;
// TODO type checks etc.
if (JsonEncoder.VTYPE_NULL.equals(variableType)) {
val = null;
} else if (JsonEncoder.VTYPE_ARRAY.equals(variableType)) {
val = decodeArray((JSONArray) value, connection);
} else if (JsonEncoder.VTYPE_MAP.equals(variableType)) {
val = decodeMap((JSONObject) value, connection);
} else if (JsonEncoder.VTYPE_LIST.equals(variableType)) {
val = decodeList((JSONArray) value, connection);
} else if (JsonEncoder.VTYPE_SET.equals(variableType)) {
val = decodeSet((JSONArray) value, connection);
} else if (JsonEncoder.VTYPE_STRINGARRAY.equals(variableType)) {
val = decodeStringArray((JSONArray) value);
} else if (JsonEncoder.VTYPE_STRING.equals(variableType)) {
val = ((JSONString) value).stringValue();
} else if (JsonEncoder.VTYPE_INTEGER.equals(variableType)) {
// TODO handle properly
val = Integer.valueOf(String.valueOf(value));
} else if (JsonEncoder.VTYPE_LONG.equals(variableType)) {

// Null is null, regardless of type
if (jsonValue.isNull() != null) {
return null;
}

String baseTypeName = type.getBaseTypeName();
if (baseTypeName.endsWith("[]")) {
return decodeArray(type, (JSONArray) jsonValue, connection);
} else if (Map.class.getName().equals(baseTypeName)
|| HashMap.class.getName().equals(baseTypeName)) {
return decodeMap(type, (JSONObject) jsonValue, connection);
} else if (List.class.getName().equals(baseTypeName)
|| ArrayList.class.getName().equals(baseTypeName)) {
return decodeList(type, (JSONArray) jsonValue, connection);
} else if (Set.class.getName().equals(baseTypeName)) {
return decodeSet(type, (JSONArray) jsonValue, connection);
} else if (String.class.getName().equals(baseTypeName)) {
return ((JSONString) jsonValue).stringValue();
} else if (Integer.class.getName().equals(baseTypeName)) {
return Integer.valueOf(String.valueOf(jsonValue));
} else if (Long.class.getName().equals(baseTypeName)) {
// TODO handle properly // TODO handle properly
val = Long.valueOf(String.valueOf(value));
} else if (JsonEncoder.VTYPE_FLOAT.equals(variableType)) {
return Long.valueOf(String.valueOf(jsonValue));
} else if (Float.class.getName().equals(baseTypeName)) {
// TODO handle properly // TODO handle properly
val = Float.valueOf(String.valueOf(value));
} else if (JsonEncoder.VTYPE_DOUBLE.equals(variableType)) {
return Float.valueOf(String.valueOf(jsonValue));
} else if (Double.class.getName().equals(baseTypeName)) {
// TODO handle properly // TODO handle properly
val = Double.valueOf(String.valueOf(value));
} else if (JsonEncoder.VTYPE_BOOLEAN.equals(variableType)) {
return Double.valueOf(String.valueOf(jsonValue));
} else if (Boolean.class.getName().equals(baseTypeName)) {
// TODO handle properly // TODO handle properly
val = Boolean.valueOf(String.valueOf(value));
} else if (JsonEncoder.VTYPE_CONNECTOR.equals(variableType)) {
val = ConnectorMap.get(connection).getConnector(
((JSONString) value).stringValue());
return Boolean.valueOf(String.valueOf(jsonValue));
} else if (Connector.class.getName().equals(baseTypeName)) {
return ConnectorMap.get(connection).getConnector(
((JSONString) jsonValue).stringValue());
} else { } else {
return decodeObject(new Type(variableType, null), value, target,
connection);
return decodeObject(type, jsonValue, target, connection);
} }

return val;
} }


private static Object decodeObject(Type type, JSONValue encodedValue,
private static Object decodeObject(Type type, JSONValue jsonValue,
Object target, ApplicationConnection connection) { Object target, ApplicationConnection connection) {
// object, class name as type
JSONSerializer<Object> serializer = connection.getSerializerMap() JSONSerializer<Object> serializer = connection.getSerializerMap()
.getSerializer(type.getBaseTypeName()); .getSerializer(type.getBaseTypeName());
// TODO handle case with no serializer found // TODO handle case with no serializer found
// Currently getSerializer throws exception if not found


if (target != null && serializer instanceof DiffJSONSerializer<?>) { if (target != null && serializer instanceof DiffJSONSerializer<?>) {
DiffJSONSerializer<Object> diffSerializer = (DiffJSONSerializer<Object>) serializer; DiffJSONSerializer<Object> diffSerializer = (DiffJSONSerializer<Object>) serializer;
diffSerializer.update(target, type, encodedValue, connection);
diffSerializer.update(target, type, jsonValue, connection);
return target; return target;
} else { } else {
Object object = serializer.deserialize(type, encodedValue,
connection);
Object object = serializer.deserialize(type, jsonValue, connection);
return object; return object;
} }
} }


private static Map<Object, Object> decodeMap(JSONObject jsonMap,
private static Map<Object, Object> decodeMap(Type type, JSONObject jsonMap,
ApplicationConnection connection) { ApplicationConnection connection) {
HashMap<Object, Object> map = new HashMap<Object, Object>(); HashMap<Object, Object> map = new HashMap<Object, Object>();
Iterator<String> it = jsonMap.keySet().iterator(); Iterator<String> it = jsonMap.keySet().iterator();
String key = it.next(); String key = it.next();
JSONArray encodedKey = (JSONArray) JSONParser.parseStrict(key); JSONArray encodedKey = (JSONArray) JSONParser.parseStrict(key);
JSONArray encodedValue = (JSONArray) jsonMap.get(key); JSONArray encodedValue = (JSONArray) jsonMap.get(key);
Object decodedKey = decodeValue(encodedKey, null, connection);
Object decodedValue = decodeValue(encodedValue, null, connection);
Object decodedKey = decodeValue(type.getParameterTypes()[0],
encodedKey, null, connection);
Object decodedValue = decodeValue(type.getParameterTypes()[1],
encodedValue, null, connection);
map.put(decodedKey, decodedValue); map.put(decodedKey, decodedValue);
} }
return map; return map;
} }


private static String[] decodeStringArray(JSONArray jsonArray) {
int size = jsonArray.size();
List<String> tokens = new ArrayList<String>(size);
for (int i = 0; i < size; ++i) {
tokens.add(String.valueOf(jsonArray.get(i)));
}
return tokens.toArray(new String[tokens.size()]);
}

private static Object[] decodeArray(JSONArray jsonArray,
private static Object[] decodeArray(Type type, JSONArray jsonArray,
ApplicationConnection connection) { ApplicationConnection connection) {
List<Object> list = decodeList(jsonArray, connection);
String arrayTypeName = type.getBaseTypeName();
String chldTypeName = arrayTypeName.substring(0,
arrayTypeName.length() - 2);
List<Object> list = decodeList(new Type(chldTypeName, null), jsonArray,
connection);
return list.toArray(new Object[list.size()]); return list.toArray(new Object[list.size()]);
} }


private static List<Object> decodeList(JSONArray jsonArray,
private static List<Object> decodeList(Type type, JSONArray jsonArray,
ApplicationConnection connection) { ApplicationConnection connection) {
List<Object> tokens = new ArrayList<Object>(); List<Object> tokens = new ArrayList<Object>();
decodeIntoCollection(jsonArray, connection, tokens);
decodeIntoCollection(type.getParameterTypes()[0], jsonArray,
connection, tokens);
return tokens; return tokens;
} }


private static Set<Object> decodeSet(JSONArray jsonArray,
private static Set<Object> decodeSet(Type type, JSONArray jsonArray,
ApplicationConnection connection) { ApplicationConnection connection) {
Set<Object> tokens = new HashSet<Object>(); Set<Object> tokens = new HashSet<Object>();
decodeIntoCollection(jsonArray, connection, tokens);
decodeIntoCollection(type.getParameterTypes()[0], jsonArray,
connection, tokens);
return tokens; return tokens;
} }


private static void decodeIntoCollection(JSONArray jsonArray,
ApplicationConnection connection, Collection<Object> tokens) {
private static void decodeIntoCollection(Type childType,
JSONArray jsonArray, ApplicationConnection connection,
Collection<Object> tokens) {
for (int i = 0; i < jsonArray.size(); ++i) { for (int i = 0; i < jsonArray.size(); ++i) {
// each entry always has two elements: type and value // each entry always has two elements: type and value
JSONArray entryArray = (JSONArray) jsonArray.get(i); JSONArray entryArray = (JSONArray) jsonArray.get(i);
tokens.add(decodeValue(entryArray, null, connection));
tokens.add(decodeValue(childType, entryArray, null, connection));
} }
} }
} }

+ 3
- 2
src/com/vaadin/terminal/gwt/client/communication/URLReference_Serializer.java 查看文件

JSONObject json = (JSONObject) jsonValue; JSONObject json = (JSONObject) jsonValue;
if (json.containsKey("URL")) { if (json.containsKey("URL")) {
JSONArray jsonURL = (JSONArray) json.get("URL"); JSONArray jsonURL = (JSONArray) json.get("URL");
String URL = (String) JsonDecoder.decodeValue(jsonURL, null,
connection);
String URL = (String) JsonDecoder.decodeValue(
new Type(String.class.getCanonicalName(), null), jsonURL,
null, connection);
reference.setURL(connection.translateVaadinUri(URL)); reference.setURL(connection.translateVaadinUri(URL));
} }
return reference; return reference;

+ 9
- 3
src/com/vaadin/terminal/gwt/widgetsetutils/GeneratedRpcMethodProviderGenerator.java 查看文件



} }


private void writeTypeCreator(SourceWriter sourceWriter, JType type) {
sourceWriter.print("new Type(\""
+ type.getErasedType().getQualifiedSourceName() + "\", ");
public static void writeTypeCreator(SourceWriter sourceWriter, JType type) {
String typeName;
if (type.isPrimitive() != null) {
// Used boxed types for primitives
typeName = type.isPrimitive().getQualifiedBoxedSourceName();
} else {
typeName = type.getErasedType().getQualifiedBinaryName();
}
sourceWriter.print("new Type(\"" + typeName + "\", ");
JParameterizedType parameterized = type.isParameterized(); JParameterizedType parameterized = type.isParameterized();
if (parameterized != null) { if (parameterized != null) {
sourceWriter.print("new Type[] {"); sourceWriter.print("new Type[] {");

+ 7
- 4
src/com/vaadin/terminal/gwt/widgetsetutils/SerializerGenerator.java 查看文件

// target.setHeight((String) // target.setHeight((String)
// JsonDecoder.decodeValue(jsonFieldValue,referenceValue, idMapper, // JsonDecoder.decodeValue(jsonFieldValue,referenceValue, idMapper,
// connection)); // connection));
sourceWriter.println("target." + setterName + "((" + fieldType
+ ") " + JsonDecoder.class.getName() + ".decodeValue("
+ jsonFieldName + ", referenceValue, connection));");
sourceWriter.print("target." + setterName + "((" + fieldType + ") "
+ JsonDecoder.class.getName() + ".decodeValue(");
GeneratedRpcMethodProviderGenerator.writeTypeCreator(sourceWriter,
setterParameterType);
sourceWriter.println(", " + jsonFieldName
+ ", referenceValue, connection));");


// } ... end of if contains // } ... end of if contains
sourceWriter.println("}");
sourceWriter.outdent(); sourceWriter.outdent();
sourceWriter.println("}");
} }


if (!update) { if (!update) {

Loading…
取消
儲存