From b0f52ef82a0e432eeab694add4924b21d103bdd6 Mon Sep 17 00:00:00 2001 From: Adolfo Panizo Date: Wed, 24 Dec 2014 16:23:39 +0100 Subject: Implementing strip() on isProperties Signed-off-by: Manolo Carrasco --- .../com/google/gwt/query/client/IsProperties.java | 7 +++- .../com/google/gwt/query/client/Properties.java | 6 ++++ .../gwt/query/client/builders/JsonBuilder.java | 6 ---- .../gwt/query/client/builders/JsonBuilderBase.java | 9 ++++++ .../com/google/gwt/query/vm/JsonFactoryJre.java | 37 ++++++++++++++-------- 5 files changed, 44 insertions(+), 21 deletions(-) (limited to 'gwtquery-core/src/main/java') 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; } -- cgit v1.2.3