From 1105ed0acb5b3111c6c711fe481372b1f7f3687e Mon Sep 17 00:00:00 2001 From: Manolo Carrasco Date: Thu, 26 Feb 2015 10:04:47 +0100 Subject: Simplify Json generated classes reusing common code --- .../google/gwt/query/client/builders/JsonBuilderBase.java | 14 ++++++++++++++ .../com/google/gwt/query/rebind/JsonBuilderGenerator.java | 15 +++------------ .../gwt/query/client/dbinding/DataBindingTestJre.java | 5 +++++ 3 files changed, 22 insertions(+), 12 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 f0c46931..996f245c 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,8 @@ 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.core.client.JsArrayMixed; +import com.google.gwt.query.client.GQ; import com.google.gwt.query.client.IsProperties; import com.google.gwt.query.client.Properties; import com.google.gwt.query.client.js.JsCache; @@ -107,6 +109,18 @@ public abstract class JsonBuilderBase> implements J return r; } + protected final T[] getIsPropertiesArrayBase(JsArrayMixed js, T[] r, Class clazz) { + JsObjectArray a1 = js.cast(); + for (int i = 0; i < r.length; i++) { + r[i] = getIsPropertiesBase(a1.get(i), clazz); + } + return r; + } + + protected final T getIsPropertiesBase(Object o, Class clazz) { + return GQ.create(clazz).load(o); + } + protected Properties getPropertiesBase(String n) { if (p.getJavaScriptObject(n) == null) { p.set(n, Properties.create()); 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 08e40c24..e30b7588 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 @@ -183,8 +183,7 @@ public class JsonBuilderGenerator extends Generator { sw.println("return p.getStr(\"" + name + "\");"); } else if (isTypeAssignableTo(method.getReturnType(), jsonBuilderType)) { String q = method.getReturnType().getQualifiedSourceName(); - sw.println("return " + "((" + q + ")GWT.create(" + q + ".class))" - + ".load(getPropertiesBase(\"" + name + "\"));"); + sw.println("return " + "getIsPropertiesBase(getPropertiesBase(\"" + name + "\")," + q + ".class);"); } else if (isTypeAssignableTo(method.getReturnType(), settingsType)) { String q = method.getReturnType().getQualifiedSourceName(); sw.println("return " + "((" + q + ")getPropertiesBase(\"" + name + "\"));"); @@ -203,16 +202,7 @@ public class JsonBuilderGenerator extends Generator { sw.println("int l = a == null ? 0 : a.length();"); String ret; if (buildType) { - sw.println(t + "[] r = new " + t + "[l];"); - sw.println("JsObjectArray a1 = p.getArray(\"" + name - + "\").cast();"); - sw.println("int l1 = r.length;"); - sw.println("for (int i = 0 ; i < l1 ; i++) {"); - sw.println(" Object w = a1.get(i);"); - sw.println(" " + t + " instance = GWT.create(" + t + ".class);"); - sw.println(" r[i] = instance.load(w);"); - sw.println("}"); - ret = "r"; + ret = "getIsPropertiesArrayBase(a, new " + t + "[l], " + t + ".class)"; } else { ret = "getArrayBase(\"" + name + "\", new " + t + "[l], " + t + ".class)"; } @@ -326,6 +316,7 @@ public class JsonBuilderGenerator extends Generator { types.add(t); } } + sw.println("GQuery.console.error(\"GQ.create: not registered class :\" + clz);"); sw.println("return null;"); sw.outdent(); sw.println("}"); diff --git a/gwtquery-core/src/test/java/com/google/gwt/query/client/dbinding/DataBindingTestJre.java b/gwtquery-core/src/test/java/com/google/gwt/query/client/dbinding/DataBindingTestJre.java index 50123b80..01b93bfd 100644 --- a/gwtquery-core/src/test/java/com/google/gwt/query/client/dbinding/DataBindingTestJre.java +++ b/gwtquery-core/src/test/java/com/google/gwt/query/client/dbinding/DataBindingTestJre.java @@ -81,6 +81,8 @@ public class DataBindingTestJre extends GWTTestCase { Boolean getZ(); String[] getT(); JsonExample setT(String[] strings); + Item[] getIt(); + JsonExample setIt(Item[] items); JsonExample setZ(Boolean b); JsonExample setD(long l); List getItems(); @@ -139,6 +141,9 @@ public class DataBindingTestJre extends GWTTestCase { Item i2 = GQ.create(Item.class); i2.setDate(new Date(3000)); Item[] items = new Item[]{i1, i2}; + c.setIt(items); + assertEquals(2000l, c.getIt()[0].getDate().getTime()); + assertEquals(3000l, c.getIt()[1].getDate().getTime()); c.setItems(Arrays.asList(items)); assertEquals(2000l, c.getItems().get(0).getDate().getTime()); assertEquals(3000l, c.getItems().get(1).getDate().getTime()); -- cgit v1.2.3