From 44702affcd643c1ddb85c7ac0cce88da75f9a3dc Mon Sep 17 00:00:00 2001 From: Manolo Carrasco Date: Wed, 24 Dec 2014 19:05:29 +0100 Subject: [PATCH] Fix client-side code Signed-off-by: Manolo Carrasco --- .../google/gwt/query/client/Properties.java | 14 +++-- .../client/builders/JsonBuilderBase.java | 33 ++++++++---- .../query/rebind/JsonBuilderGenerator.java | 2 +- .../client/dbinding/DataBindingTestJre.java | 20 +++---- .../gwt/query/vm/JsonFactoryParseTest.java | 52 ------------------- 5 files changed, 36 insertions(+), 85 deletions(-) delete mode 100644 gwtquery-core/src/test/java/com/google/gwt/query/vm/JsonFactoryParseTest.java diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/client/Properties.java b/gwtquery-core/src/main/java/com/google/gwt/query/client/Properties.java index 91259103..0b978548 100644 --- a/gwtquery-core/src/main/java/com/google/gwt/query/client/Properties.java +++ b/gwtquery-core/src/main/java/com/google/gwt/query/client/Properties.java @@ -142,7 +142,7 @@ public class Properties extends JavaScriptObject implements IsProperties { public final Object getObject(Object name) { return c().get(String.valueOf(name)); } - + public final Properties getProperties(Object name) { return getJavaScriptObject(name); } @@ -176,9 +176,9 @@ public class Properties extends JavaScriptObject implements IsProperties { /** * Adds a new native js function to the properties object. * This native function will wrap the passed java Function. - * + * * Its useful for exporting or importing to javascript. - * + * */ public final native void setFunction(T name, Function f) /*-{ if (!f) return; @@ -219,12 +219,10 @@ public class Properties extends JavaScriptObject implements IsProperties { return getDataImpl(); } - @SuppressWarnings("unchecked") - @Override public final J strip() { return getDataImpl(); } - + public final J parse(String json) { return load(JsUtils.parseJSON(json)); } @@ -236,11 +234,11 @@ public class Properties extends JavaScriptObject implements IsProperties { public final String toJson() { return toJsonString(); } - + public final String toJsonWithName() { return toJsonWithName(getJsonName()); } - + public final String toJsonWithName(String name) { return "{\"" + name + "\":{" + toJson() + "}"; } 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 bd5ad66e..baf42181 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 @@ -19,12 +19,18 @@ import com.google.gwt.core.client.JavaScriptObject; import com.google.gwt.core.client.JsArray; import com.google.gwt.query.client.IsProperties; import com.google.gwt.query.client.Properties; +import com.google.gwt.query.client.js.JsCache; import com.google.gwt.query.client.js.JsObjectArray; import com.google.gwt.query.client.js.JsUtils; +import com.google.gwt.user.client.Window; + +import java.util.Arrays; +import java.util.List; public abstract class JsonBuilderBase> implements JsonBuilder { protected Properties p = Properties.create(); + protected String[] fieldNames = new String[] {}; @SuppressWarnings("unchecked") @Override @@ -37,13 +43,16 @@ public abstract class JsonBuilderBase> implements J public J parse(String json, boolean fix) { return fix ? parse(Properties.wrapPropertiesString(json)) : parse(json); } - + @SuppressWarnings("unchecked") @Override public J strip() { - String[] methods = getFieldNames(); //EXCEPTION - String[] jsonMethods = p.getFieldNames(); // OK - System.out.println(methods); + List names = Arrays.asList(getFieldNames()); + for (String jsonName : p.getFieldNames()) { + if (!names.contains(jsonName)) { + p.cast().delete(jsonName); + } + } return (J)this; } @@ -107,7 +116,7 @@ public abstract class JsonBuilderBase> implements J public String toJson() { return p.tostring(); } - + public String toJsonWithName() { return "{\"" + getJsonName() + "\":" + p.tostring() + "}"; } @@ -117,22 +126,22 @@ public abstract class JsonBuilderBase> implements J public Properties getProperties() { return p; } - + @Override public String toQueryString() { return p.toQueryString(); } - + @SuppressWarnings("unchecked") @Override public Properties getDataImpl() { return p; } - + public T get(Object key) { return p.get(key); } - + @SuppressWarnings("unchecked") public T set(Object key, Object val) { if (val instanceof IsProperties) { @@ -142,8 +151,12 @@ public abstract class JsonBuilderBase> implements J } return (T)this; } - + public T as(Class clz) { return p.as(clz); } + + public final String[] getFieldNames() { + return fieldNames; + } } 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 c2383c19..197e23b8 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 @@ -143,7 +143,7 @@ public class JsonBuilderGenerator extends Generator { for (Iterator it = attrs.iterator(); it.hasNext();) { ret += (ret.isEmpty() ? "" : ",") + "\"" + it.next() + "\""; } - sw.println("public final String[] getFieldNames() {return new String[]{" + ret + "};}"); + sw.println("{ fieldNames = new String[]{" + ret + "}; }"); } public void generateMethod(SourceWriter sw, JMethod method, String name, TreeLogger logger) 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 be0a83f7..5aeaacb7 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 @@ -15,7 +15,6 @@ */ package com.google.gwt.query.client.dbinding; -import com.google.gwt.core.shared.GWT; import com.google.gwt.junit.client.GWTTestCase; import com.google.gwt.query.client.Function; import com.google.gwt.query.client.GQ; @@ -23,8 +22,6 @@ import com.google.gwt.query.client.IsProperties; import com.google.gwt.query.client.builders.JsonBuilder; import com.google.gwt.query.client.builders.Name; -import java.io.ByteArrayOutputStream; -import java.io.PrintStream; import java.util.Arrays; import java.util.Date; import java.util.List; @@ -153,46 +150,41 @@ public class DataBindingTestJre extends GWTTestCase { assertEquals(1, c.get("a").intValue()); } - + public interface GUser extends JsonBuilder{ int getAge(); void setAge(int age); String getName(); void setName(String name); - + GUser address(String address); String address(); } - + public static final String JSON_USER_EXAMPLE = " { " + " 'email': 'foo@bar.com', " + " 'age': 27, " + " 'name': 'Foo Bar', " + " 'address': 'Street Foo N6' " + " }"; - + public void test_parse_json() { GUser entity = GQ.create(GUser.class); entity.parse(JSON_USER_EXAMPLE, true); - + assertEquals(27, entity.getAge()); assertEquals("Foo Bar", entity.getName()); assertEquals("Street Foo N6", entity.address()); assertTrue(entity.toJson().contains("email")); } - + public void test_parse_strict_json() { GUser entity = GQ.create(GUser.class); entity.parse(JSON_USER_EXAMPLE, true); - for(String s: entity.getFieldNames()) { - System.out.println("Moe: "+s); - } - entity.strip(); - System.out.println(entity.toJson()); assertEquals(27, entity.getAge()); assertEquals("Foo Bar", entity.getName()); assertEquals("Street Foo N6", entity.address()); diff --git a/gwtquery-core/src/test/java/com/google/gwt/query/vm/JsonFactoryParseTest.java b/gwtquery-core/src/test/java/com/google/gwt/query/vm/JsonFactoryParseTest.java deleted file mode 100644 index b4ea332f..00000000 --- a/gwtquery-core/src/test/java/com/google/gwt/query/vm/JsonFactoryParseTest.java +++ /dev/null @@ -1,52 +0,0 @@ -package com.google.gwt.query.vm; - -import com.google.gwt.query.client.GQ; -import com.google.gwt.query.client.builders.JsonBuilder; - -import org.junit.Assert; -import org.junit.Test; - -public class JsonFactoryParseTest { - - public interface GUser extends JsonBuilder{ - int getAge(); - void setAge(int age); - - String getName(); - void setName(String name); - - GUser address(String address); - String address(); - } - - public static final String JSON_USER_EXAMPLE = " { " + - " 'email': 'foo@bar.com', " + - " 'age': 27, " + - " 'name': 'Foo Bar', " + - " 'address': 'Street Foo N6' " + - " }"; - - @Test public void - test_parse_json() { - GUser entity = GQ.create(GUser.class); - entity.parse(JSON_USER_EXAMPLE); - - Assert.assertEquals(27, entity.getAge()); - Assert.assertEquals("Foo Bar", entity.getName()); - Assert.assertEquals("Street Foo N6", entity.address()); - Assert.assertTrue(entity.toJson().contains("email")); - } - - @Test public void - test_parse_strict_json() { - GUser entity = GQ.create(GUser.class); - entity.parse(JSON_USER_EXAMPLE); - entity.strip(); - System.out.println(entity.toJson()); - Assert.assertEquals(27, entity.getAge()); - Assert.assertEquals("Foo Bar", entity.getName()); - Assert.assertEquals("Street Foo N6", entity.address()); - Assert.assertFalse(entity.toJson().contains("email")); - } - -} -- 2.39.5