]> source.dussan.org Git - gwtquery.git/commitdiff
Fix client-side code
authorManolo Carrasco <manolo@apache.org>
Wed, 24 Dec 2014 18:05:29 +0000 (19:05 +0100)
committerManolo Carrasco <manolo@apache.org>
Thu, 25 Dec 2014 14:00:26 +0000 (15:00 +0100)
Signed-off-by: Manolo Carrasco <manolo@apache.org>
gwtquery-core/src/main/java/com/google/gwt/query/client/Properties.java
gwtquery-core/src/main/java/com/google/gwt/query/client/builders/JsonBuilderBase.java
gwtquery-core/src/main/java/com/google/gwt/query/rebind/JsonBuilderGenerator.java
gwtquery-core/src/test/java/com/google/gwt/query/client/dbinding/DataBindingTestJre.java
gwtquery-core/src/test/java/com/google/gwt/query/vm/JsonFactoryParseTest.java [deleted file]

index 91259103d8c039b35b4417e13970b450d196240f..0b978548f795272f428b4cb8b5970347ffbe2f4d 100644 (file)
@@ -142,7 +142,7 @@ public class Properties extends JavaScriptObject implements IsProperties {
   public final Object getObject(Object name) {
     return c().get(String.valueOf(name));
   }
-  
+
   public final Properties getProperties(Object name) {
     return getJavaScriptObject(name);
   }
@@ -176,9 +176,9 @@ public class Properties extends JavaScriptObject implements IsProperties {
   /**
    * Adds a new native js function to the properties object.
    * This native function will wrap the passed java Function.
-   * 
+   *
    * Its useful for exporting or importing to javascript.
-   * 
+   *
    */
   public final native <T> void setFunction(T name, Function f) /*-{
     if (!f) return;
@@ -219,12 +219,10 @@ 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));
   }
@@ -236,11 +234,11 @@ public class Properties extends JavaScriptObject implements IsProperties {
   public final String toJson() {
     return toJsonString();
   }
-  
+
   public final String toJsonWithName() {
     return toJsonWithName(getJsonName());
   }
-  
+
   public final String toJsonWithName(String name) {
     return "{\"" + name + "\":{" + toJson() + "}";
   }
index bd5ad66e9a76e7bf7c714e39dada22e624a72f0c..baf4218118ec9d1e3de02d6029c088d0b3416364 100644 (file)
@@ -19,12 +19,18 @@ import com.google.gwt.core.client.JavaScriptObject;
 import com.google.gwt.core.client.JsArray;
 import com.google.gwt.query.client.IsProperties;
 import com.google.gwt.query.client.Properties;
+import com.google.gwt.query.client.js.JsCache;
 import com.google.gwt.query.client.js.JsObjectArray;
 import com.google.gwt.query.client.js.JsUtils;
+import com.google.gwt.user.client.Window;
+
+import java.util.Arrays;
+import java.util.List;
 
 public abstract class JsonBuilderBase<J extends JsonBuilderBase<?>> implements JsonBuilder {
 
   protected Properties p = Properties.create();
+  protected String[] fieldNames = new String[] {};
 
   @SuppressWarnings("unchecked")
   @Override
@@ -37,13 +43,16 @@ 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);
+    List<String> names = Arrays.asList(getFieldNames());
+    for (String jsonName : p.getFieldNames()) {
+      if (!names.contains(jsonName)) {
+        p.<JsCache>cast().delete(jsonName);
+      }
+    }
     return (J)this;
   }
 
@@ -107,7 +116,7 @@ public abstract class JsonBuilderBase<J extends JsonBuilderBase<?>> implements J
   public String toJson() {
     return p.tostring();
   }
-  
+
   public String toJsonWithName() {
     return "{\"" + getJsonName() + "\":" + p.tostring() + "}";
   }
@@ -117,22 +126,22 @@ public abstract class JsonBuilderBase<J extends JsonBuilderBase<?>> implements J
   public Properties getProperties() {
     return p;
   }
-  
+
   @Override
   public String toQueryString() {
     return p.toQueryString();
   }
-  
+
   @SuppressWarnings("unchecked")
   @Override
   public Properties getDataImpl() {
     return p;
   }
-  
+
   public <T> T get(Object key) {
     return p.get(key);
   }
-  
+
   @SuppressWarnings("unchecked")
   public <T extends IsProperties> T set(Object key, Object val) {
     if (val instanceof IsProperties) {
@@ -142,8 +151,12 @@ public abstract class JsonBuilderBase<J extends JsonBuilderBase<?>> implements J
     }
     return (T)this;
   }
-  
+
   public <T extends JsonBuilder> T as(Class<T> clz) {
     return p.as(clz);
   }
+
+  public final String[] getFieldNames() {
+    return fieldNames;
+  }
 }
index c2383c19d0c1f6863af1ea43da50875986e18f3d..197e23b8c691cd3093b764e785c9a38bb04e445a 100644 (file)
@@ -143,7 +143,7 @@ public class JsonBuilderGenerator extends Generator {
     for (Iterator<String> it = attrs.iterator(); it.hasNext();) {
       ret += (ret.isEmpty() ? "" : ",") + "\"" + it.next() + "\"";
     }
-    sw.println("public final String[] getFieldNames() {return new String[]{" + ret + "};}");
+    sw.println("{ fieldNames = new String[]{" + ret + "}; }");
   }
 
   public void generateMethod(SourceWriter sw, JMethod method, String name, TreeLogger logger)
index be0a83f7a6640c4ba92307066e726e7a5e33d159..5aeaacb7285ad2fb3e9e8938cf9dbb70f2d8f87e 100644 (file)
@@ -15,7 +15,6 @@
  */
 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;
@@ -23,8 +22,6 @@ import com.google.gwt.query.client.IsProperties;
 import com.google.gwt.query.client.builders.JsonBuilder;
 import com.google.gwt.query.client.builders.Name;
 
-import java.io.ByteArrayOutputStream;
-import java.io.PrintStream;
 import java.util.Arrays;
 import java.util.Date;
 import java.util.List;
@@ -153,46 +150,41 @@ public class DataBindingTestJre extends GWTTestCase {
 
     assertEquals(1, c.<Number>get("a").intValue());
   }
-  
+
   public interface GUser extends JsonBuilder{
     int getAge();
     void setAge(int age);
 
     String getName();
     void setName(String name);
-    
+
     GUser address(String address);
     String address();
   }
-  
+
   public static final String JSON_USER_EXAMPLE = " { " +
                                                  "   'email': 'foo@bar.com', " +
                                                  "   'age': 27, " +
                                                  "   'name': 'Foo Bar', " +
                                                  "   'address': 'Street Foo N6' " +
                                                  " }";
-      
+
   public void
   test_parse_json() {
     GUser entity = GQ.create(GUser.class);
     entity.parse(JSON_USER_EXAMPLE, true);
-      
+
     assertEquals(27, entity.getAge());
     assertEquals("Foo Bar", entity.getName());
     assertEquals("Street Foo N6", entity.address());
     assertTrue(entity.toJson().contains("email"));
   }
-  
+
   public void
   test_parse_strict_json() {
     GUser entity = GQ.create(GUser.class);
     entity.parse(JSON_USER_EXAMPLE, true);
-    for(String s: entity.getFieldNames()) {
-      System.out.println("Moe: "+s);
-    }
-      
     entity.strip();
-    System.out.println(entity.toJson());
     assertEquals(27, entity.getAge());
     assertEquals("Foo Bar", entity.getName());
     assertEquals("Street Foo N6", entity.address());
diff --git a/gwtquery-core/src/test/java/com/google/gwt/query/vm/JsonFactoryParseTest.java b/gwtquery-core/src/test/java/com/google/gwt/query/vm/JsonFactoryParseTest.java
deleted file mode 100644 (file)
index b4ea332..0000000
+++ /dev/null
@@ -1,52 +0,0 @@
-package com.google.gwt.query.vm;
-
-import com.google.gwt.query.client.GQ;
-import com.google.gwt.query.client.builders.JsonBuilder;
-
-import org.junit.Assert;
-import org.junit.Test;
-
-public class JsonFactoryParseTest {
-
-  public interface GUser extends JsonBuilder{
-    int getAge();
-    void setAge(int age);
-
-    String getName();
-    void setName(String name);
-    
-    GUser address(String address);
-    String address();
-  }
-  
-  public static final String JSON_USER_EXAMPLE = " { " +
-                                                 "   'email': 'foo@bar.com', " +
-                                                 "   'age': 27, " +
-                                                 "   'name': 'Foo Bar', " +
-                                                 "   'address': 'Street Foo N6' " +
-                                                 " }";
-      
-  @Test public void
-  test_parse_json() {
-    GUser entity = GQ.create(GUser.class);
-    entity.parse(JSON_USER_EXAMPLE);
-      
-    Assert.assertEquals(27, entity.getAge());
-    Assert.assertEquals("Foo Bar", entity.getName());
-    Assert.assertEquals("Street Foo N6", entity.address());
-    Assert.assertTrue(entity.toJson().contains("email"));
-  }
-  
-  @Test public void
-  test_parse_strict_json() {
-    GUser entity = GQ.create(GUser.class);
-    entity.parse(JSON_USER_EXAMPLE);
-    entity.strip();
-    System.out.println(entity.toJson());
-    Assert.assertEquals(27, entity.getAge());
-    Assert.assertEquals("Foo Bar", entity.getName());
-    Assert.assertEquals("Street Foo N6", entity.address());
-    Assert.assertFalse(entity.toJson().contains("email"));
-  }
-  
-}