import com.google.gwt.core.client.JavaScriptObject;
import com.google.gwt.core.client.JsArray;
+import com.google.gwt.query.client.Function;
import com.google.gwt.query.client.Properties;
import com.google.gwt.query.client.js.JsObjectArray;
import com.google.gwt.query.client.js.JsUtils;
protected Properties p = Properties.create();
+ @SuppressWarnings("unchecked")
public J parse(String json) {
return load(JsUtils.parseJSON(json));
}
+ @SuppressWarnings("unchecked")
public J parse(String json, boolean fix) {
return fix ? parse(Properties.wrapPropertiesString(json)) : parse(json);
}
if (prp != null && prp instanceof String) {
return parse((String)prp);
}
+ int i = -1;
p = prp == null ? Properties.create() : (Properties)prp;
- while (JsUtils.isArray(p)) {
+ while (p != null && i != p.hashCode() && JsUtils.isArray(p)) {
+ i = p.hashCode();
p = p.get(0);
}
return (J)this;
return r != null ? r : Properties.create();
}
+ protected Function getFunctionBase(String n) {
+ final Object o = p.get("success");
+ if (o != null) {
+ if (o instanceof Function) {
+ return (Function)o;
+ } else if (o instanceof JavaScriptObject
+ && JsUtils.isFunction((JavaScriptObject)o)) {
+ return new Function() {
+ private native void exec(JavaScriptObject f, Object data) /*-{
+ f(data);
+ }-*/;
+ public void f() {
+ exec((JavaScriptObject)o, getData()[0]);
+ }
+ };
+ }
+ }
+ return null;
+ }
+
public String toString() {
return p.tostring();
}
return hasProperty(o, "currentTarget");
}
+ /**
+ * Check is a javascript object is a function
+ */
+ public static native boolean isFunction(JavaScriptObject o) /*-{
+ return o && typeof o == 'function'
+ }-*/;
+
/**
* Check is a javascript object can be used as an array
*/
public static native boolean isNodeList(JavaScriptObject o) /*-{
var r = Object.prototype.toString.call(o);
return r == '[object HTMLCollection]' || r == '[object NodeList]' ||
- (typeof o == 'object' && o.length && o[0] && o[0].tagName)
+ (typeof o == 'object' && o.length && o[0].tagName)
? true : false;
}-*/;
return Properties.create();
}
}
-
- public static native boolean isDefaultPrevented(JavaScriptObject e)/*-{
- return e.defaultPrevented || e.returnValue === false || e.getPreventDefault && e.getPreventDefault() ? true : false;
- }-*/;
-
}
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.Function;\r
import com.google.gwt.query.client.Properties;\r
import com.google.gwt.query.client.builders.JsonBuilder;\r
import com.google.gwt.query.client.builders.Name;\r
static JClassType stringType;\r
static JClassType jsType;\r
static JClassType listType;\r
+ static JClassType functionType;\r
\r
public String generate(TreeLogger treeLogger,\r
GeneratorContext generatorContext, String requestedClass)\r
stringType = oracle.findType(String.class.getName());\r
jsType = oracle.findType(JavaScriptObject.class.getName());\r
listType = oracle.findType(List.class.getName());\r
+ functionType = oracle.findType(Function.class.getName());\r
\r
String t[] = generateClassName(clazz);\r
\r
\r
public void generateMethod(SourceWriter sw, JMethod method, 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
- : method.getName().replaceFirst("^(get|set)", "");\r
+ : methName.replaceFirst("^(get|set)", "");\r
name = name.substring(0, 1).toLowerCase() + name.substring(1);\r
\r
String retType = method.getReturnType().getParameterizedQualifiedSourceName();\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
+ sw.println("return new Date(Long.parseLong(p.getStr(\"" + 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 getPropertiesBase(\"" + name + "\");");\r
} else if (isTypeAssignableTo(method.getReturnType(), jsType)) {\r
sw.println("return p.getJavaScriptObject(\"" + name + "\");");\r
+ } else if (isTypeAssignableTo(method.getReturnType(), functionType)) {\r
+ sw.println("return getFunctionBase(\"" + name + "\");");\r
} else if (arr != null || list != null) {\r
JType type = arr != null ? arr.getComponentType()\r
: list.getTypeArgs()[0];\r
sw.println("}");\r
ret = "r";\r
} else {\r
- ret = "getArrayBase(\"" + name + "\", new " + t + "[l], " + t+ ".class)";\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
+ \r
} else {\r
- sw.println("return null; // Unsupported return type: " + retType);\r
+ sw.println("System.err.println(\"JsonBuilderGenerator WARN: unknown return type " \r
+ + retType + " " + ifaceName + "." + methName + "()\"); ");\r
+ // We return the object because probably the user knows how to handle it\r
+ sw.println("return p.get(\"" + name + "\");");\r
}\r
sw.outdent();\r
sw.println("}");\r