package com.google.gwt.query.rebind;\r
\r
import java.io.PrintWriter;\r
+import java.util.Collection;\r
+import java.util.HashSet;\r
+import java.util.Iterator;\r
import java.util.List;\r
+import java.util.Set;\r
\r
import com.google.gwt.core.client.JavaScriptObject;\r
import com.google.gwt.core.ext.Generator;\r
/**\r
*/\r
public class JsonBuilderGenerator extends Generator {\r
- TypeOracle oracle;\r
+ static JClassType enumType;\r
+ static JClassType functionType;\r
static JClassType jsonBuilderType;\r
- static JClassType stringType;\r
static JClassType jsType;\r
static JClassType listType;\r
- static JClassType functionType;\r
- static JClassType enumType;\r
+ static JClassType stringType;\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 static String classNameToJsonName(String name) {\r
+ return deCapitalize(name.replaceAll("^.*[\\.\\$_]", ""));\r
+ }\r
+\r
+ public static String deCapitalize(String s) {\r
+ return s == null || s.isEmpty() ? s : \r
+ (s.substring(0, 1).toLowerCase() + (s.length() > 1 ? s.substring(1) : ""));\r
+ }\r
+ \r
+ TypeOracle oracle;\r
\r
public String generate(TreeLogger treeLogger,\r
GeneratorContext generatorContext, String requestedClass)\r
SourceWriter sw = getSourceWriter(treeLogger, generatorContext, t[0], t[1],\r
requestedClass);\r
if (sw != null) {\r
+ Set<String> attrs = new HashSet<String>();\r
for (JMethod method : clazz.getMethods()) {\r
- generateMethod(sw, method, treeLogger);\r
+ String methName = method.getName();\r
+ Name nameAnnotation = method.getAnnotation(Name.class);\r
+ String name = nameAnnotation != null \r
+ ? nameAnnotation.value()\r
+ : methName.replaceFirst("^(get|set)", "");\r
+ name = name.substring(0, 1).toLowerCase() + name.substring(1);\r
+ attrs.add(name);\r
+ generateMethod(sw, method, name, treeLogger);\r
}\r
+ generateFieldNamesMethod(sw, attrs, treeLogger);\r
+ generateToJsonMethod(sw, t[3], treeLogger);\r
sw.commit(treeLogger);\r
}\r
return t[2];\r
}\r
\r
public String[] generateClassName(JType t) {\r
- String[] ret = new String[3];\r
+ String[] ret = new String[4];\r
JClassType c = t.isClassOrInterface();\r
ret[0] = c.getPackage().getName();\r
ret[1] = c.getName().replace('.', '_') + "_JsonBuilder";\r
ret[2] = ret[0] + "." + ret[1];\r
+ ret[3] = classNameToJsonName(c.getName());\r
return ret;\r
}\r
-\r
- public boolean isTypeAssignableTo(JType t, JClassType o) {\r
- JClassType c = t.isClassOrInterface();\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
+ public void generateFieldNamesMethod(SourceWriter sw, Collection<String> attrs, TreeLogger logger) {\r
+ String ret = "";\r
+ for (Iterator<String> it = attrs.iterator(); it.hasNext();) {\r
+ ret += (ret.isEmpty() ? "" : ",") + "\"" + it.next() + "\"";\r
+ }\r
+ sw.println("public final String[] getFieldNames() {return new String[]{" + ret + "};}");\r
}\r
-\r
- public void generateMethod(SourceWriter sw, JMethod method, TreeLogger logger)\r
+ \r
+ public void generateMethod(SourceWriter sw, JMethod method, String name, TreeLogger logger)\r
throws UnableToCompleteException {\r
String ifaceName = method.getEnclosingType().getQualifiedSourceName();\r
- String methName = method.getName();\r
- Name nameAnnotation = method.getAnnotation(Name.class);\r
- String name = nameAnnotation != null ? nameAnnotation.value()\r
- : methName.replaceFirst("^(get|set)", "");\r
- name = name.substring(0, 1).toLowerCase() + name.substring(1);\r
+\r
\r
String retType = method.getReturnType().getParameterizedQualifiedSourceName();\r
sw.print("public final " + retType + " " + method.getName());\r
sw.println("return "+method.getReturnType().getQualifiedSourceName()+".valueOf(p.getStr(\"" + name + "\"));");\r
}else {\r
sw.println("System.err.println(\"JsonBuilderGenerator WARN: unknown return type " \r
- + retType + " " + ifaceName + "." + methName + "()\"); ");\r
+ + retType + " " + ifaceName + "." + name + "()\"); ");\r
// We return the object because probably the user knows how to handle it\r
sw.println("return p.get(\"" + name + "\");");\r
}\r
sw.println("}");\r
}\r
}\r
+ \r
+ public void generateToJsonMethod(SourceWriter sw, String name, TreeLogger logger) {\r
+ sw.println("public final String getJsonName() {return \"" + name + "\";}");\r
+ sw.println("public final String toJson() {return \"{\\\"\" + getJsonName() + \"\\\":\" + toString() + \"}\";}");\r
+ }\r
\r
protected SourceWriter getSourceWriter(TreeLogger logger,\r
GeneratorContext context, String packageName, String className,\r
}\r
return composerFactory.createSourceWriter(context, printWriter);\r
}\r
+\r
+ public boolean isTypeAssignableTo(JType t, JClassType o) {\r
+ JClassType c = t.isClassOrInterface();\r
+ return (c != null && c.isAssignableTo(o));\r
+ }\r
}\r