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;
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;
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();
*/
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);
- }
- }
- }
+
+ HashSet<String> valid = JsonStripJre.getValidMethodsFrom(type.getMethods());
+ Hashtable<String, Method> 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) {
--- /dev/null
+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<String> getValidMethodsFrom(Method[] methods) {
+ HashSet<String> valid = new HashSet<String>();
+
+ 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<String, Method> getJsonBuildersFrom(Method[] methods) {
+ Hashtable<String, Method> recursiveBuilders = new Hashtable<String, Method>();
+
+ 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;
+ }
+
+}
}
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', "
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());
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());