diff options
author | Manolo Carrasco <manolo@apache.org> | 2014-12-25 21:18:10 +0100 |
---|---|---|
committer | Manolo Carrasco <manolo@apache.org> | 2014-12-25 21:35:56 +0100 |
commit | 8b5e7e3ca0f571ca048c04b02e29f342d125a198 (patch) | |
tree | 04d0a2a725243fce03c60fef00936a96ff2cad58 | |
parent | 44702affcd643c1ddb85c7ac0cce88da75f9a3dc (diff) | |
download | gwtquery-8b5e7e3ca0f571ca048c04b02e29f342d125a198.tar.gz gwtquery-8b5e7e3ca0f571ca048c04b02e29f342d125a198.zip |
Make strip recursive in server side.
Signed-off-by: Manolo Carrasco <manolo@apache.org>
3 files changed, 73 insertions, 47 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 baf42181..57f5a909 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 @@ -49,6 +49,8 @@ public abstract class JsonBuilderBase<J extends JsonBuilderBase<?>> implements J public J strip() { List<String> names = Arrays.asList(getFieldNames()); for (String jsonName : p.getFieldNames()) { + // TODO: figure out a way so as we can generate some marks in generated class in + // order to call getters to return JsonBuilder object given an an attribute name if (!names.contains(jsonName)) { p.<JsCache>cast().delete(jsonName); } 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 634a483d..479b09d2 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 @@ -1,13 +1,5 @@ package com.google.gwt.query.vm; -import com.google.gwt.query.client.Function; -import com.google.gwt.query.client.IsProperties; -import com.google.gwt.query.client.Properties; -import com.google.gwt.query.client.builders.JsonBuilder; -import com.google.gwt.query.client.builders.JsonFactory; -import com.google.gwt.query.client.builders.Name; -import com.google.gwt.query.rebind.JsonBuilderGenerator; - import java.lang.reflect.Array; import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; @@ -15,10 +7,19 @@ 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.HashSet; +import java.util.Hashtable; import java.util.List; +import com.google.gwt.query.client.Function; +import com.google.gwt.query.client.IsProperties; +import com.google.gwt.query.client.Properties; +import com.google.gwt.query.client.builders.JsonBuilder; +import com.google.gwt.query.client.builders.JsonFactory; +import com.google.gwt.query.client.builders.Name; +import com.google.gwt.query.rebind.JsonBuilderGenerator; + import elemental.json.Json; import elemental.json.JsonArray; import elemental.json.JsonBoolean; @@ -200,10 +201,8 @@ public class JsonFactoryJre implements JsonFactory { Class<?>[] classes = method.getParameterTypes(); int largs = classes.length; - String regexGetOrSet = "^[gs]et"; - Name name = method.getAnnotation(Name.class); - String attr = name != null ? name.value() : deCapitalize(mname.replaceFirst(regexGetOrSet, "")); + String attr = name != null ? name.value() : methodName2AttrName(mname); if ("getFieldNames".equals(mname)) { return jsonObject.keys(); @@ -222,9 +221,7 @@ public class JsonFactoryJre implements JsonFactory { } jsonObject = Json.parse(json); } else if ("strip".equals(mname)) { - List<String> keys = Arrays.asList(jsonObject.keys()); - Class<?> type = proxy.getClass().getInterfaces()[0]; - strip(keys, type.getMethods()); + stripProxy((JsonBuilder)proxy); } else if (mname.matches("toString")) { return jsonObject.toString(); } else if (mname.matches("toJsonWithName")) { @@ -251,26 +248,40 @@ public class JsonFactoryJre implements JsonFactory { return null; } - private void strip(List<String> keys, Method[] methods) { - for (String key: keys) { - boolean isInType = isInType(key, methods); - if (!isInType) { + /** + * @param proxy + */ + private void stripProxy(JsonBuilder proxy) { + Class<?> type = proxy.getClass().getInterfaces()[0]; + HashSet<String> valid = new HashSet<String>(); + Hashtable<String, Method> getters = new Hashtable<String, Method>(); + for (Method m : type.getMethods()) { + String attr = methodName2AttrName(m.getName()); + valid.add(attr); + Class<?>[] classes = m.getParameterTypes(); + if (classes.length == 0) { + if (IsProperties.class.isAssignableFrom(m.getReturnType())) { + getters.put(attr, m); + } + } + } + for (String key: jsonObject.keys()) { + String name = methodName2AttrName(key); + Method getter = getters.get(name); + if (!valid.contains(name)) { jsonObject.remove(key); + } else if (getter != null) { + try { + ((IsProperties)invoke(proxy, getter, new Object[]{})).strip(); + } catch (Throwable e) { + e.printStackTrace(); + } } } } - 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 methodName2AttrName(String s) { + return deCapitalize(s.replaceFirst("^[gs]et", "")); } private String deCapitalize(String 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 5aeaacb7..3d1273c1 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,6 +15,7 @@ */ 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; @@ -151,33 +152,38 @@ public class DataBindingTestJre extends GWTTestCase { assertEquals(1, c.<Number>get("a").intValue()); } - public interface GUser extends JsonBuilder{ - int getAge(); - void setAge(int age); + public interface GAddress extends JsonBuilder { + String street(); + String city(); + } + public interface GUser extends JsonBuilder { + int getAge(); String getName(); - void setName(String name); - - GUser address(String address); - String address(); + GAddress address(); } - public static final String JSON_USER_EXAMPLE = " { " + - " 'email': 'foo@bar.com', " + - " 'age': 27, " + - " 'name': 'Foo Bar', " + - " 'address': 'Street Foo N6' " + - " }"; + public static final String JSON_USER_EXAMPLE = " { " + + " 'email': 'foo@bar.com', " + + " 'age': 27, " + + " 'name': 'Foo Bar', " + + " 'address': {" + + " 'street': 'Street Foo N6', " + + " 'phone': '670'" + + " }" + + "}"; public void test_parse_json() { GUser entity = GQ.create(GUser.class); entity.parse(JSON_USER_EXAMPLE, true); + assertNotNull(entity.get("email")); assertEquals(27, entity.getAge()); assertEquals("Foo Bar", entity.getName()); - assertEquals("Street Foo N6", entity.address()); - assertTrue(entity.toJson().contains("email")); + assertNotNull(entity.address()); + assertEquals("Street Foo N6", entity.address().street()); + assertNotNull(entity.address().get("phone")); } public void @@ -185,9 +191,16 @@ public class DataBindingTestJre extends GWTTestCase { GUser entity = GQ.create(GUser.class); entity.parse(JSON_USER_EXAMPLE, true); entity.strip(); + + assertNull(entity.get("email")); assertEquals(27, entity.getAge()); assertEquals("Foo Bar", entity.getName()); - assertEquals("Street Foo N6", entity.address()); - assertFalse(entity.toJson().contains("email")); + assertNotNull(entity.address()); + assertEquals("Street Foo N6", entity.address().street()); + + // Recursion not implemented in client side + if (GWT.isScript()) { + assertNull(entity.address().get("phone")); + } } }
\ No newline at end of file |