package com.google.gwt.query.rebind;\r
\r
import java.io.PrintWriter;\r
+import java.util.List;\r
\r
import com.google.gwt.core.client.JavaScriptObject;\r
import com.google.gwt.core.ext.Generator;\r
import com.google.gwt.core.ext.typeinfo.JClassType;\r
import com.google.gwt.core.ext.typeinfo.JMethod;\r
import com.google.gwt.core.ext.typeinfo.JParameter;\r
+import com.google.gwt.core.ext.typeinfo.JParameterizedType;\r
import com.google.gwt.core.ext.typeinfo.JType;\r
import com.google.gwt.core.ext.typeinfo.TypeOracle;\r
import com.google.gwt.query.client.Properties;\r
static JClassType jsonBuilderType;\r
static JClassType stringType;\r
static JClassType jsType;\r
+ static JClassType listType;\r
\r
public String generate(TreeLogger treeLogger,\r
GeneratorContext generatorContext, String requestedClass)\r
jsonBuilderType = oracle.findType(JsonBuilder.class.getName());\r
stringType = oracle.findType(String.class.getName());\r
jsType = oracle.findType(JavaScriptObject.class.getName());\r
+ listType = oracle.findType(List.class.getName());\r
\r
String t[] = generateClassName(clazz);\r
\r
- SourceWriter sw = getSourceWriter(treeLogger, generatorContext, t[0],\r
- t[1], requestedClass);\r
+ SourceWriter sw = getSourceWriter(treeLogger, generatorContext, t[0], t[1],\r
+ requestedClass);\r
if (sw != null) {\r
for (JMethod method : clazz.getMethods()) {\r
generateMethod(sw, method, treeLogger);\r
return (c != null && c.isAssignableTo(o));\r
}\r
\r
+ public static String capitalize(String s) {\r
+ if (s.length() == 0)\r
+ return s;\r
+ return s.substring(0, 1).toUpperCase() + s.substring(1).toLowerCase();\r
+ }\r
+\r
public void generateMethod(SourceWriter sw, JMethod method, TreeLogger logger)\r
throws UnableToCompleteException {\r
Name nameAnnotation = method.getAnnotation(Name.class);\r
String name = nameAnnotation != null ? nameAnnotation.value()\r
- : method.getName().replaceFirst("^(get|set)", "").toLowerCase();\r
+ : method.getName().replaceFirst("^(get|set)", "");\r
+ name = name.substring(0, 1).toLowerCase() + name.substring(1);\r
\r
String retType = method.getReturnType().getParameterizedQualifiedSourceName();\r
-\r
sw.print("public final " + retType + " " + method.getName());\r
JParameter[] params = method.getParameters();\r
if (params.length == 0) {\r
JArrayType arr = method.getReturnType().isArray();\r
+ JParameterizedType list = method.getReturnType().isParameterized();\r
+\r
sw.println("() {");\r
sw.indent();\r
if (retType.matches("(java.lang.Boolean|boolean)")) {\r
sw.println("return p.getBoolean(\"" + name + "\");");\r
+ } else if (retType.matches("java.util.Date")) {\r
+ sw.println("return new Date((long)p.getFloat(\"" + name + "\"));");\r
} else if (method.getReturnType().isPrimitive() != null) {\r
sw.println("return (" + retType + ")p.getFloat(\"" + name + "\");");\r
} else if (isTypeAssignableTo(method.getReturnType(), stringType)) {\r
sw.println("return p.getStr(\"" + name + "\");");\r
} else if (isTypeAssignableTo(method.getReturnType(), jsonBuilderType)) {\r
String q = method.getReturnType().getQualifiedSourceName();\r
- sw.println("return " +\r
- "((" + q + ")GWT.create(" + q + ".class))" +\r
- ".load(p.getJavaScriptObject(\"" + name + "\"));");\r
- } else if (retType.equals(Properties.class.getName())){\r
+ sw.println("return " + "((" + q + ")GWT.create(" + q + ".class))"\r
+ + ".load(p.getJavaScriptObject(\"" + name + "\"));");\r
+ } else if (retType.equals(Properties.class.getName())) {\r
sw.println("return getPropertiesBase(\"" + name + "\");");\r
} else if (isTypeAssignableTo(method.getReturnType(), jsType)) {\r
sw.println("return p.getJavaScriptObject(\"" + name + "\");");\r
- } else if (arr != null) {\r
- String t = arr.getComponentType().getQualifiedSourceName();\r
+ } else if (arr != null || list != null) {\r
+ JType type = arr != null ? arr.getComponentType()\r
+ : list.getTypeArgs()[0];\r
+ boolean buildType = isTypeAssignableTo(type, jsonBuilderType);\r
+ String t = type.getQualifiedSourceName();\r
sw.println("JsArrayMixed a = p.getArray(\"" + name + "\");");\r
sw.println("int l = a == null ? 0 : a.length();");\r
- sw.println("return getArrayBase(\"" + name + "\", new " + t + "[l], "\r
- + t + ".class);");\r
+ String ret;\r
+ if (buildType) {\r
+ sw.println(t + "[] r = new " + t + "[l];");\r
+ sw.println("JsObjectArray<?> a1 = p.getArray(\"" + name\r
+ + "\").cast();");\r
+ sw.println("int l1 = r.length;");\r
+ sw.println("for (int i = 0 ; i < l1 ; i++) {");\r
+ sw.println(" Object w = a1.get(i);");\r
+ sw.println(" " + t + " instance = GWT.create(" + t + ".class);");\r
+ sw.println(" r[i] = instance.load(w);");\r
+ sw.println("}");\r
+ ret = "r";\r
+ } else {\r
+ ret = "getArrayBase(\"" + name + "\", new " + t + "[l], " + t+ ".class)";\r
+ }\r
+ if (arr != null) {\r
+ sw.println("return " + ret + ";");\r
+ } else {\r
+ sw.println("return Arrays.asList(" + ret + ");");\r
+ }\r
} else {\r
sw.println("return null; // Unsupported return type: " + retType);\r
}\r
} else if (params.length == 1) {\r
JType type = params[0].getType();\r
JArrayType arr = type.isArray();\r
+ JParameterizedType list = type.isParameterized();\r
sw.print("(" + type.getParameterizedQualifiedSourceName() + " a)");\r
sw.println("{");\r
sw.indent();\r
- if (arr != null) {\r
- sw.println("setArrayBase(\"" + name + "\", a);");\r
+ if (arr != null || list != null) {\r
+ String a = "a";\r
+ if (list != null) {\r
+ a = "a.toArray(new " + list.getTypeArgs()[0].getQualifiedSourceName()\r
+ + "[0])";\r
+ }\r
+ sw.println("setArrayBase(\"" + name + "\", " + a + ");");\r
+ } else if (type.getParameterizedQualifiedSourceName().matches("java.util.Date")) {\r
+ sw.println("p.set(\"" + name + "\", a.getTime());");\r
} else {\r
sw.println("p.set(\"" + name + "\", a);");\r
}\r
- if (!"void".equals(retType)){\r
- if (isTypeAssignableTo(method.getReturnType(), method.getEnclosingType())) {\r
+ if (!"void".equals(retType)) {\r
+ if (isTypeAssignableTo(method.getReturnType(),\r
+ method.getEnclosingType())) {\r
sw.println("return this;");\r
} else {\r
sw.println("return null;");\r
composerFactory.addImport("com.google.gwt.query.client.*");\r
composerFactory.addImport("com.google.gwt.core.client.*");\r
composerFactory.addImport("com.google.gwt.dom.client.*");\r
+ composerFactory.addImport("java.util.*");\r
+\r
for (String interfaceName : interfaceNames) {\r
composerFactory.addImplementedInterface(interfaceName);\r
}\r
import static com.google.gwt.query.client.GQuery.$;
+import java.util.Arrays;
+import java.util.Date;
+import java.util.List;
+
import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Element;
import com.google.gwt.junit.client.GWTTestCase;
}
}
+ interface Item extends JsonBuilder {
+ Date getDate();
+ void setDate(Date d);
+ }
+
interface JsonExample extends JsonBuilder {
int getA();
JsonExample getB();
JsonExample setT(String[] strings);
JsonExample setZ(Boolean b);
JsonExample setD(long l);
+ List<Item> getItems();
+ void setItems(List<Item> a);
}
public void testJsonBuilder() {
assertEquals("foo", c.getT()[0]);
assertEquals("bar", c.getT()[1]);
assertEquals(1234l, c.getD());
-
+
+ Item i1 = GWT.create(Item.class);
+ Item i2 = GWT.create(Item.class);
+ i1.setDate(new Date(2000));
+ i2.setDate(new Date(3000));
+ Item[] items = new Item[]{i1, i2};
+ c.setItems(Arrays.asList(items));
+ assertEquals(2000l, c.getItems().get(0).getDate().getTime());
+ assertEquals(3000l, c.getItems().get(1).getDate().getTime());
+ String s = "{\"a\":1,\"b\":{\"a\":2,\"b\":{\"a\":3}},\"u\":\"url\",\"d\":1234,\"t\":[\"foo\",\"bar\"],\"z\":false,\"items\":[{\"date\":2000},{\"date\":3000}]}";
+ assertEquals(s, c.toString());
}
interface XmlExample extends XmlBuilder {