diff options
author | Manolo Carrasco <manolo@apache.org> | 2011-12-23 08:02:27 +0000 |
---|---|---|
committer | Manolo Carrasco <manolo@apache.org> | 2011-12-23 08:02:27 +0000 |
commit | 20701fa27ebf71ff3cdfca10433e2a98a1e2576b (patch) | |
tree | 3495c6441ceafc7c956d9e19fd3ea31656c8d0ea /gwtquery-core | |
parent | c29dd50d90c9006c9b87333e495ad07685fff56e (diff) | |
download | gwtquery-20701fa27ebf71ff3cdfca10433e2a98a1e2576b.tar.gz gwtquery-20701fa27ebf71ff3cdfca10433e2a98a1e2576b.zip |
Support for Functions in Json Generators, related with issue_116. Do not return null for unknown types but warn the user
Diffstat (limited to 'gwtquery-core')
3 files changed, 49 insertions, 11 deletions
diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/client/builders/JsonBuilderBase.java b/gwtquery-core/src/main/java/com/google/gwt/query/client/builders/JsonBuilderBase.java index e8f23d4b..a209a5a3 100644 --- a/gwtquery-core/src/main/java/com/google/gwt/query/client/builders/JsonBuilderBase.java +++ b/gwtquery-core/src/main/java/com/google/gwt/query/client/builders/JsonBuilderBase.java @@ -17,6 +17,7 @@ package com.google.gwt.query.client.builders; 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; @@ -25,10 +26,12 @@ public abstract class JsonBuilderBase<J extends JsonBuilderBase<?>> implements J 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); } @@ -39,8 +42,10 @@ public abstract class JsonBuilderBase<J extends JsonBuilderBase<?>> implements J 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; @@ -83,6 +88,26 @@ public abstract class JsonBuilderBase<J extends JsonBuilderBase<?>> implements J 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(); } diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/client/js/JsUtils.java b/gwtquery-core/src/main/java/com/google/gwt/query/client/js/JsUtils.java index 5dac2578..1992edad 100644 --- a/gwtquery-core/src/main/java/com/google/gwt/query/client/js/JsUtils.java +++ b/gwtquery-core/src/main/java/com/google/gwt/query/client/js/JsUtils.java @@ -244,6 +244,13 @@ public class JsUtils { } /** + * 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 isArray(JavaScriptObject o) /*-{ @@ -257,7 +264,7 @@ public class JsUtils { 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; }-*/; @@ -314,9 +321,4 @@ public class JsUtils { return Properties.create(); } } - - public static native boolean isDefaultPrevented(JavaScriptObject e)/*-{ - return e.defaultPrevented || e.returnValue === false || e.getPreventDefault && e.getPreventDefault() ? true : false; - }-*/; - } diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/rebind/JsonBuilderGenerator.java b/gwtquery-core/src/main/java/com/google/gwt/query/rebind/JsonBuilderGenerator.java index 25a6899c..3fcb79bf 100644 --- a/gwtquery-core/src/main/java/com/google/gwt/query/rebind/JsonBuilderGenerator.java +++ b/gwtquery-core/src/main/java/com/google/gwt/query/rebind/JsonBuilderGenerator.java @@ -30,6 +30,7 @@ import com.google.gwt.core.ext.typeinfo.JParameter; import com.google.gwt.core.ext.typeinfo.JParameterizedType;
import com.google.gwt.core.ext.typeinfo.JType;
import com.google.gwt.core.ext.typeinfo.TypeOracle;
+import com.google.gwt.query.client.Function;
import com.google.gwt.query.client.Properties;
import com.google.gwt.query.client.builders.JsonBuilder;
import com.google.gwt.query.client.builders.Name;
@@ -44,6 +45,7 @@ public class JsonBuilderGenerator extends Generator { static JClassType stringType;
static JClassType jsType;
static JClassType listType;
+ static JClassType functionType;
public String generate(TreeLogger treeLogger,
GeneratorContext generatorContext, String requestedClass)
@@ -54,6 +56,7 @@ public class JsonBuilderGenerator extends Generator { stringType = oracle.findType(String.class.getName());
jsType = oracle.findType(JavaScriptObject.class.getName());
listType = oracle.findType(List.class.getName());
+ functionType = oracle.findType(Function.class.getName());
String t[] = generateClassName(clazz);
@@ -90,9 +93,11 @@ public class JsonBuilderGenerator extends Generator { public void generateMethod(SourceWriter sw, JMethod method, TreeLogger logger)
throws UnableToCompleteException {
+ String ifaceName = method.getEnclosingType().getQualifiedSourceName();
+ String methName = method.getName();
Name nameAnnotation = method.getAnnotation(Name.class);
String name = nameAnnotation != null ? nameAnnotation.value()
- : method.getName().replaceFirst("^(get|set)", "");
+ : methName.replaceFirst("^(get|set)", "");
name = name.substring(0, 1).toLowerCase() + name.substring(1);
String retType = method.getReturnType().getParameterizedQualifiedSourceName();
@@ -107,7 +112,7 @@ public class JsonBuilderGenerator extends Generator { if (retType.matches("(java.lang.Boolean|boolean)")) {
sw.println("return p.getBoolean(\"" + name + "\");");
} else if (retType.matches("java.util.Date")) {
- sw.println("return new Date((long)p.getFloat(\"" + name + "\"));");
+ sw.println("return new Date(Long.parseLong(p.getStr(\"" + name + "\")));");
} else if (method.getReturnType().isPrimitive() != null) {
sw.println("return (" + retType + ")p.getFloat(\"" + name + "\");");
} else if (isTypeAssignableTo(method.getReturnType(), stringType)) {
@@ -120,6 +125,8 @@ public class JsonBuilderGenerator extends Generator { sw.println("return getPropertiesBase(\"" + name + "\");");
} else if (isTypeAssignableTo(method.getReturnType(), jsType)) {
sw.println("return p.getJavaScriptObject(\"" + name + "\");");
+ } else if (isTypeAssignableTo(method.getReturnType(), functionType)) {
+ sw.println("return getFunctionBase(\"" + name + "\");");
} else if (arr != null || list != null) {
JType type = arr != null ? arr.getComponentType()
: list.getTypeArgs()[0];
@@ -140,15 +147,19 @@ public class JsonBuilderGenerator extends Generator { sw.println("}");
ret = "r";
} else {
- ret = "getArrayBase(\"" + name + "\", new " + t + "[l], " + t+ ".class)";
+ ret = "getArrayBase(\"" + name + "\", new " + t + "[l], " + t + ".class)";
}
if (arr != null) {
sw.println("return " + ret + ";");
} else {
sw.println("return Arrays.asList(" + ret + ");");
}
+
} else {
- sw.println("return null; // Unsupported return type: " + retType);
+ sw.println("System.err.println(\"JsonBuilderGenerator WARN: unknown return type "
+ + retType + " " + ifaceName + "." + methName + "()\"); ");
+ // We return the object because probably the user knows how to handle it
+ sw.println("return p.get(\"" + name + "\");");
}
sw.outdent();
sw.println("}");
|