Revision: 10252
Author:   [email protected]
Date:     Wed Jun  1 09:55:17 2011
Log: Use identity semantics when canonicalizing JsonSplittable instances. This is necessary to support Android, where the org.json arrays appear to have value-based equality.
http://code.google.com/p/google-web-toolkit/issues/detail?id=6390
Patch by: bobv
Review by: rjrjr

Review at http://gwt-code-reviews.appspot.com/1451805

http://code.google.com/p/google-web-toolkit/source/detail?r=10252

Modified:
 /trunk/user/src/com/google/web/bindery/autobean/vm/impl/JsonSplittable.java

=======================================
--- /trunk/user/src/com/google/web/bindery/autobean/vm/impl/JsonSplittable.java Mon Apr 18 02:42:06 2011 +++ /trunk/user/src/com/google/web/bindery/autobean/vm/impl/JsonSplittable.java Wed Jun 1 09:55:17 2011
@@ -15,6 +15,7 @@
  */
 package com.google.web.bindery.autobean.vm.impl;

+import com.google.gwt.core.client.impl.WeakMapping;
 import com.google.web.bindery.autobean.shared.Splittable;
 import com.google.web.bindery.autobean.shared.impl.HasSplittable;
 import com.google.web.bindery.autobean.shared.impl.StringQuoter;
@@ -23,28 +24,18 @@
 import org.json.JSONException;
 import org.json.JSONObject;

-import java.lang.ref.Reference;
-import java.lang.ref.WeakReference;
 import java.util.Arrays;
 import java.util.Collections;
 import java.util.HashMap;
 import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
-import java.util.WeakHashMap;

 /**
  * Uses the org.json packages to slice and dice request payloads.
  */
 public class JsonSplittable implements Splittable, HasSplittable {

-  /**
- * Ensures that the same JsonSplittable will be returned for a given backing
-   * JSONObject.
-   */
-  private static final Map<Object, Reference<JsonSplittable>> canonical =
-      new WeakHashMap<Object, Reference<JsonSplittable>>();
-
   public static JsonSplittable create() {
     return new JsonSplittable(new JSONObject());
   }
@@ -306,13 +297,19 @@
     if (JSONObject.NULL.equals(object)) {
       return null;
     }
-    Reference<JsonSplittable> ref = canonical.get(object);
-    JsonSplittable seen = ref == null ? null : ref.get();
+    /*
+     * Maintain a 1:1 mapping between object instances and JsonSplittables.
+ * Doing this with a WeakHashMap doesn't work on Android, since its org.json
+     * arrays appear to have value-based equality.
+     */
+ JsonSplittable seen = (JsonSplittable) WeakMapping.get(object, JsonSplittable.class.getName());
     if (seen == null) {
       if (object instanceof JSONObject) {
         seen = new JsonSplittable((JSONObject) object);
+        WeakMapping.set(object, JsonSplittable.class.getName(), seen);
       } else if (object instanceof JSONArray) {
         seen = new JsonSplittable((JSONArray) object);
+        WeakMapping.set(object, JsonSplittable.class.getName(), seen);
       } else if (object instanceof String) {
         seen = new JsonSplittable(object.toString());
       } else if (object instanceof Number) {
@@ -322,7 +319,6 @@
       } else {
         throw new RuntimeException("Unhandled type " + object.getClass());
       }
-      canonical.put(object, new WeakReference<JsonSplittable>(seen));
     }
     return seen;
   }

--
http://groups.google.com/group/Google-Web-Toolkit-Contributors

Reply via email to