diff options
author | Adolfo Panizo <adolfo.panizo@gmail.com> | 2014-12-24 16:23:39 +0100 |
---|---|---|
committer | Manolo Carrasco <manolo@apache.org> | 2014-12-25 15:00:11 +0100 |
commit | b0f52ef82a0e432eeab694add4924b21d103bdd6 (patch) | |
tree | 17fea64ca26d3cef9175a6bc766d9826102f609a /gwtquery-core/src/main/java | |
parent | ea1d26c2ffc9d82a515b41636abd8b2da3af61b1 (diff) | |
download | gwtquery-b0f52ef82a0e432eeab694add4924b21d103bdd6.tar.gz gwtquery-b0f52ef82a0e432eeab694add4924b21d103bdd6.zip |
Implementing strip() on isProperties
Signed-off-by: Manolo Carrasco <manolo@apache.org>
Diffstat (limited to 'gwtquery-core/src/main/java')
5 files changed, 44 insertions, 21 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 extends IsProperties> T parse(String json); + + /** + * Removes the extra JSON and leaves only the setters/getters described + * in the JsonBuilder interface. + */ + <T extends IsProperties> 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 extends IsProperties> J strip() { + return getDataImpl(); + } + public final <J extends IsProperties> 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 @@ -31,12 +31,6 @@ public interface JsonBuilder extends IsProperties { <J> 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> J parseStrict(String json, Class<J> clz); - - /** * Returns the wrapped object, normally a Properties jso in client * but can be used to return the underlying Json implementation in JVM * @deprecated use asObject() instead 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<J extends 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<? extends JsonBuilder> clz = (Class<? extends JsonBuilder>) args[1]; - Class<? extends JsonBuilder> proxie = (Class<? extends JsonBuilder>) Proxy.getProxyClass(clz.getClassLoader(), new Class[] {clz}); - JsonObject jsonArg = Json.parse((String)args[0]); - parseStrict(proxie, jsonArg, regexGetOrSet); + } else if ("strip".equals(mname)) { + List<String> 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<? extends JsonBuilder> proxie, JsonObject jsonArg, - String regexGetOrSet) { - for(Method m: proxie.getMethods()) { - if (!m.getName().matches("^set.+")) { - continue; + private void strip(List<String> 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; } |