From b0f52ef82a0e432eeab694add4924b21d103bdd6 Mon Sep 17 00:00:00 2001 From: Adolfo Panizo Date: Wed, 24 Dec 2014 16:23:39 +0100 Subject: [PATCH] Implementing strip() on isProperties Signed-off-by: Manolo Carrasco --- .../google/gwt/query/client/IsProperties.java | 7 ++- .../google/gwt/query/client/Properties.java | 6 +++ .../query/client/builders/JsonBuilder.java | 6 --- .../client/builders/JsonBuilderBase.java | 9 ++++ .../google/gwt/query/vm/JsonFactoryJre.java | 37 +++++++++------ .../client/dbinding/DataBindingTestJre.java | 46 +++++++++++++++++++ .../gwt/query/vm/JsonFactoryParseTest.java | 16 +++++-- 7 files changed, 101 insertions(+), 26 deletions(-) diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/client/IsProperties.java b/gwtquery-core/src/main/java/com/google/gwt/query/client/IsProperties.java index bcd79cdb..3b88edf5 100644 --- a/gwtquery-core/src/main/java/com/google/gwt/query/client/IsProperties.java +++ b/gwtquery-core/src/main/java/com/google/gwt/query/client/IsProperties.java @@ -31,6 +31,12 @@ public interface IsProperties { * parses a json string and loads the resulting properties object. */ T parse(String json); + + /** + * Removes the extra JSON and leaves only the setters/getters described + * in the JsonBuilder interface. + */ + T strip(); /** * Returns the underlying object, normally a Properties jso in client @@ -77,7 +83,6 @@ public interface IsProperties { */ String getJsonName(); - /** * converts a JsonBuilder instance into another JsonBuilder type but * preserving the underlying data object. 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 db35f5d6..91259103 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 @@ -219,6 +219,12 @@ 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)); } diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/client/builders/JsonBuilder.java b/gwtquery-core/src/main/java/com/google/gwt/query/client/builders/JsonBuilder.java index 9dd3df78..b5f30451 100644 --- a/gwtquery-core/src/main/java/com/google/gwt/query/client/builders/JsonBuilder.java +++ b/gwtquery-core/src/main/java/com/google/gwt/query/client/builders/JsonBuilder.java @@ -30,12 +30,6 @@ public interface JsonBuilder extends IsProperties { */ J parse(String json, boolean fix); - /** - * Parses a json string and loads the resulting properties object. - * It will parse only the json's properties which are in clz. - */ - J parseStrict(String json, Class clz); - /** * Returns the wrapped object, normally a Properties jso in client * but can be used to return the underlying Json implementation in JVM 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 c09836a5..bd5ad66e 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 @@ -37,6 +37,15 @@ 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); + return (J)this; + } @SuppressWarnings("unchecked") @Override diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/vm/JsonFactoryJre.java b/gwtquery-core/src/main/java/com/google/gwt/query/vm/JsonFactoryJre.java index dfdb4cf2..634a483d 100644 --- a/gwtquery-core/src/main/java/com/google/gwt/query/vm/JsonFactoryJre.java +++ b/gwtquery-core/src/main/java/com/google/gwt/query/vm/JsonFactoryJre.java @@ -15,6 +15,7 @@ import java.lang.reflect.ParameterizedType; import java.lang.reflect.Proxy; import java.lang.reflect.Type; import java.util.ArrayList; +import java.util.Arrays; import java.util.Date; import java.util.List; @@ -220,11 +221,10 @@ public class JsonFactoryJre implements JsonFactory { json = Properties.wrapPropertiesString(json); } jsonObject = Json.parse(json); - } else if (largs > 1 && ("parseStrict".equals(mname))) { - Class clz = (Class) args[1]; - Class proxie = (Class) Proxy.getProxyClass(clz.getClassLoader(), new Class[] {clz}); - JsonObject jsonArg = Json.parse((String)args[0]); - parseStrict(proxie, jsonArg, regexGetOrSet); + } else if ("strip".equals(mname)) { + List keys = Arrays.asList(jsonObject.keys()); + Class type = proxy.getClass().getInterfaces()[0]; + strip(keys, type.getMethods()); } else if (mname.matches("toString")) { return jsonObject.toString(); } else if (mname.matches("toJsonWithName")) { @@ -251,19 +251,28 @@ public class JsonFactoryJre implements JsonFactory { return null; } - public void parseStrict(Class proxie, JsonObject jsonArg, - String regexGetOrSet) { - for(Method m: proxie.getMethods()) { - if (!m.getName().matches("^set.+")) { - continue; + private void strip(List keys, Method[] methods) { + for (String key: keys) { + boolean isInType = isInType(key, methods); + if (!isInType) { + jsonObject.remove(key); } - String attrJs = deCapitalize(m.getName().replaceFirst(regexGetOrSet, "")); - Object value = jsonArg.get(attrJs); - setValue(null, jsonObject, attrJs, value); + } + } + + private boolean isInType(String key, Method[] methods) { + if (methods == null || methods.length == 0) { + return false; } + for(Method m : methods) { + if (m.getName().toLowerCase().contains(key)) { + return true; + } + } + return false; } - + private String deCapitalize(String s) { return s != null && s.length() > 0 ? s.substring(0, 1).toLowerCase() + s.substring(1) : s; } 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 7158fe8d..be0a83f7 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 @@ -29,6 +29,7 @@ import java.util.Arrays; import java.util.Date; import java.util.List; + /** * Tests for Deferred which can run either in JVM and GWT */ @@ -152,4 +153,49 @@ 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()); + assertFalse(entity.toJson().contains("email")); + } } \ No newline at end of file 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 index 48272a61..b4ea332f 100644 --- 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 @@ -14,32 +14,38 @@ public class JsonFactoryParseTest { 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' " + + " 'name': 'Foo Bar', " + + " 'address': 'Street Foo N6' " + " }"; @Test public void test_parse_json() { GUser entity = GQ.create(GUser.class); entity.parse(JSON_USER_EXAMPLE); - System.out.println(entity.toJson()); - + 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.parseStrict(JSON_USER_EXAMPLE, 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