From c6fd41796742df282f523bfddedba31a69883f44 Mon Sep 17 00:00:00 2001 From: Adolfo Panizo Date: Fri, 26 Dec 2014 14:26:07 +0100 Subject: Fix for @Name in strip() implementation on the VM (added some refactors) --- .../com/google/gwt/query/client/Properties.java | 5 +- .../com/google/gwt/query/vm/JsonFactoryJre.java | 62 ++++++++++------------ .../google/gwt/query/vm/strip/JsonStripJre.java | 61 +++++++++++++++++++++ .../google/gwt/query/vm/util/MethodNameParser.java | 12 +++++ .../query/client/dbinding/DataBindingTestJre.java | 6 +++ 5 files changed, 109 insertions(+), 37 deletions(-) create mode 100644 gwtquery-core/src/main/java/com/google/gwt/query/vm/strip/JsonStripJre.java create mode 100644 gwtquery-core/src/main/java/com/google/gwt/query/vm/util/MethodNameParser.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 0b978548..2c2ba54c 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 @@ -218,9 +218,10 @@ public class Properties extends JavaScriptObject implements IsProperties { } return getDataImpl(); } - + + @SuppressWarnings("unchecked") public final J strip() { - return getDataImpl(); + return (J)this; } public final J parse(String json) { 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 479b09d2..799cf256 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,5 +1,15 @@ 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 com.google.gwt.query.vm.strip.JsonStripJre; +import com.google.gwt.query.vm.util.MethodNameParser; + import java.lang.reflect.Array; import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; @@ -12,14 +22,6 @@ 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; @@ -202,7 +204,7 @@ public class JsonFactoryJre implements JsonFactory { int largs = classes.length; Name name = method.getAnnotation(Name.class); - String attr = name != null ? name.value() : methodName2AttrName(mname); + String attr = name != null ? name.value() : MethodNameParser.methodName2AttrName(mname); if ("getFieldNames".equals(mname)) { return jsonObject.keys(); @@ -253,39 +255,29 @@ public class JsonFactoryJre implements JsonFactory { */ private void stripProxy(JsonBuilder proxy) { Class type = proxy.getClass().getInterfaces()[0]; - HashSet valid = new HashSet(); - Hashtable getters = new Hashtable(); - 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); - } - } - } + + HashSet valid = JsonStripJre.getValidMethodsFrom(type.getMethods()); + Hashtable recursiveBuilders = JsonStripJre.getJsonBuildersFrom(type.getMethods()); + for (String key: jsonObject.keys()) { - String name = methodName2AttrName(key); - Method getter = getters.get(name); + String name = MethodNameParser.methodName2AttrName(key); if (!valid.contains(name)) { jsonObject.remove(key); - } else if (getter != null) { - try { - ((IsProperties)invoke(proxy, getter, new Object[]{})).strip(); - } catch (Throwable e) { - e.printStackTrace(); - } + continue; + } + Method recursiveBuilder = recursiveBuilders.get(name); + if (recursiveBuilder != null) { + callRecursiveStrip(proxy,recursiveBuilder); } } } - private String methodName2AttrName(String s) { - return deCapitalize(s.replaceFirst("^[gs]et", "")); - } - - private String deCapitalize(String s) { - return s != null && s.length() > 0 ? s.substring(0, 1).toLowerCase() + s.substring(1) : s; + private void callRecursiveStrip(JsonBuilder proxy, Method recursiveBuilder) { + try { + ((IsProperties)invoke(proxy, recursiveBuilder, new Object[]{})).strip(); + } catch (Throwable e) { + e.printStackTrace(); + } } private String getDataBindingClassName(Class type) { diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/vm/strip/JsonStripJre.java b/gwtquery-core/src/main/java/com/google/gwt/query/vm/strip/JsonStripJre.java new file mode 100644 index 00000000..19e961af --- /dev/null +++ b/gwtquery-core/src/main/java/com/google/gwt/query/vm/strip/JsonStripJre.java @@ -0,0 +1,61 @@ +package com.google.gwt.query.vm.strip; + +import com.google.gwt.query.client.IsProperties; +import com.google.gwt.query.client.builders.Name; +import com.google.gwt.query.vm.util.MethodNameParser; + +import java.lang.reflect.Method; +import java.util.HashSet; +import java.util.Hashtable; + +public class JsonStripJre { + + public static HashSet getValidMethodsFrom(Method[] methods) { + HashSet valid = new HashSet(); + + if (!isValidArray(methods)) { + return valid; + } + + for (Method m : methods) { + String attr = MethodNameParser.methodName2AttrName(m.getName()); + Name annotation = m.getAnnotation(Name.class); + + if (annotation != null) { + attr = annotation.value(); + } + valid.add(attr); + } + return valid; + } + + + + public static Hashtable getJsonBuildersFrom(Method[] methods) { + Hashtable recursiveBuilders = new Hashtable(); + + if (!isValidArray(methods)) { + return recursiveBuilders; + } + + for (Method m : methods) { + if(isRecursiveJsonBuilder(m)) { + String attr = MethodNameParser.methodName2AttrName(m.getName()); + recursiveBuilders.put(attr, m); + } + } + + return recursiveBuilders; + } + + private static boolean isRecursiveJsonBuilder(Method m) { + Class[] classes = m.getParameterTypes(); + return classes.length == 0 && IsProperties.class.isAssignableFrom(m.getReturnType()); + } + + + private static boolean isValidArray(Object[] array) { + return array != null && array.length > 0; + } + +} diff --git a/gwtquery-core/src/main/java/com/google/gwt/query/vm/util/MethodNameParser.java b/gwtquery-core/src/main/java/com/google/gwt/query/vm/util/MethodNameParser.java new file mode 100644 index 00000000..99e9ff93 --- /dev/null +++ b/gwtquery-core/src/main/java/com/google/gwt/query/vm/util/MethodNameParser.java @@ -0,0 +1,12 @@ +package com.google.gwt.query.vm.util; + +public class MethodNameParser { + + public static String methodName2AttrName(String s) { + return deCapitalize(s.replaceFirst("^[gs]et", "")); + } + + private static 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 3d1273c1..5ea76a18 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 @@ -158,12 +158,16 @@ public class DataBindingTestJre extends GWTTestCase { } public interface GUser extends JsonBuilder { + @Name("_id") + String getId(); + int getAge(); String getName(); GAddress address(); } public static final String JSON_USER_EXAMPLE = " { " + + " '_id': 'aaabbbccc', " + " 'email': 'foo@bar.com', " + " 'age': 27, " + " 'name': 'Foo Bar', " @@ -179,6 +183,7 @@ public class DataBindingTestJre extends GWTTestCase { entity.parse(JSON_USER_EXAMPLE, true); assertNotNull(entity.get("email")); + assertEquals("aaabbbccc", entity.getId()); assertEquals(27, entity.getAge()); assertEquals("Foo Bar", entity.getName()); assertNotNull(entity.address()); @@ -192,6 +197,7 @@ public class DataBindingTestJre extends GWTTestCase { entity.parse(JSON_USER_EXAMPLE, true); entity.strip(); + assertEquals("aaabbbccc", entity.getId()); assertNull(entity.get("email")); assertEquals(27, entity.getAge()); assertEquals("Foo Bar", entity.getName()); -- cgit v1.2.3