private synchronized JsonSplittable makeSplittable(Object object) { if (JSONObject.NULL.equals(object)) { return null; } /* * Maintain a 1:1 mapping between object instances and JsonSplittables. * Doing this with a WeakHashMap doesn't work, since our c.g.g.thirdparty.json * arrays, coming from Android, 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.setWeak(object, JsonSplittable.class.getName(), seen); } else if (object instanceof JSONArray) { seen = new JsonSplittable((JSONArray) object); WeakMapping.setWeak(object, JsonSplittable.class.getName(), seen); } else if (object instanceof String) { seen = new JsonSplittable(object.toString()); } else if (object instanceof Number) { seen = new JsonSplittable(((Number) object).doubleValue()); } else if (object instanceof Boolean) { seen = new JsonSplittable((Boolean) object); } else { throw new RuntimeException("Unhandled type " + object.getClass()); } } return seen; }
public static void setWeak(Object instance, String key, Object value) { set(instance, key, value); }
/** * Constructor that wraps an existing object. The parameters on this method * are reversed to avoid conflicting with the other two-arg constructor for * {@code AutoBean<Splittable>} instances. */ protected AbstractAutoBean(T wrapped, AutoBeanFactory factory) { this.factory = factory; usingSimplePeer = false; data = null; this.wrapped = wrapped; // Used by AutoBeanUtils WeakMapping.setWeak(wrapped, AutoBean.class.getName(), this); }
/** * Return the single AutoBean wrapper that is observing the delegate object or * {@code null} if the parameter is {@code null}or not wrapped by an AutoBean. * * @param delegate a delegate object, or {@code null} * @return the {@link AutoBean} wrapper for the delegate, or {@code null} */ @SuppressWarnings("unchecked") public static <T, U extends T> AutoBean<T> getAutoBean(U delegate) { return delegate == null ? null : (AutoBean<T>) WeakMapping.get(delegate, AutoBean.class .getName()); }
cleanup(); if (instance instanceof String) { throw new IllegalArgumentException("Cannot use Strings with WeakMapping");
private T createShim() { T toReturn = ProxyAutoBean.makeProxy(beanType, new ShimHandler<T>(this, getWrapped())); WeakMapping.setWeak(toReturn, AutoBean.class.getName(), this); return toReturn; } }
/** * Return the single AutoBean wrapper that is observing the delegate object or * {@code null} if the parameter is {@code null}or not wrapped by an AutoBean. * * @param delegate a delegate object, or {@code null} * @return the {@link AutoBean} wrapper for the delegate, or {@code null} */ @SuppressWarnings("unchecked") public static <T, U extends T> AutoBean<T> getAutoBean(U delegate) { return delegate == null ? null : (AutoBean<T>) WeakMapping.get(delegate, AutoBean.class .getName()); }
/** * Returns the Object associated with the given key in the (key, value) * mapping associated with the given Object instance. * * @param instance the source Object. * @param key a String key. * @return an Object associated with that key on the given instance, or null. */ public static Object get(Object instance, String key) { cleanup(); Object ref = new IdentityWeakReference(instance, queue); Map<String, Object> m = map.get(ref); if (m == null) { return null; } Object toReturn = m.get(key); if (toReturn instanceof ManagedWeakReference) { toReturn = ((ManagedWeakReference<?>) toReturn).get(); } return toReturn; }
private synchronized JsonSplittable makeSplittable(Object object) { if (JSONObject.NULL.equals(object)) { return null; } /* * Maintain a 1:1 mapping between object instances and JsonSplittables. * Doing this with a WeakHashMap doesn't work, since our c.g.g.thirdparty.json * arrays, coming from Android, 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.setWeak(object, JsonSplittable.class.getName(), seen); } else if (object instanceof JSONArray) { seen = new JsonSplittable((JSONArray) object); WeakMapping.setWeak(object, JsonSplittable.class.getName(), seen); } else if (object instanceof String) { seen = new JsonSplittable(object.toString()); } else if (object instanceof Number) { seen = new JsonSplittable(((Number) object).doubleValue()); } else if (object instanceof Boolean) { seen = new JsonSplittable((Boolean) object); } else { throw new RuntimeException("Unhandled type " + object.getClass()); } } return seen; }
/** * Constructor that wraps an existing object. The parameters on this method * are reversed to avoid conflicting with the other two-arg constructor for * {@code AutoBean<Splittable>} instances. */ protected AbstractAutoBean(T wrapped, AutoBeanFactory factory) { this.factory = factory; usingSimplePeer = false; data = null; this.wrapped = wrapped; // Used by AutoBeanUtils WeakMapping.setWeak(wrapped, AutoBean.class.getName(), this); }
public T unwrap() { if (usingSimplePeer) { throw new IllegalStateException(); } try { WeakMapping.set(wrapped, AutoBean.class.getName(), null); return wrapped; } finally { wrapped = null; } }
/** * Return the single AutoBean wrapper that is observing the delegate object or * {@code null} if the parameter is {@code null}or not wrapped by an AutoBean. * * @param delegate a delegate object, or {@code null} * @return the {@link AutoBean} wrapper for the delegate, or {@code null} */ @SuppressWarnings("unchecked") public static <T, U extends T> AutoBean<T> getAutoBean(U delegate) { return delegate == null ? null : (AutoBean<T>) WeakMapping.get(delegate, AutoBean.class .getName()); }
private synchronized JsonSplittable makeSplittable(Object object) { if (JSONObject.NULL.equals(object)) { return null; } /* * Maintain a 1:1 mapping between object instances and JsonSplittables. * Doing this with a WeakHashMap doesn't work, since our c.g.g.thirdparty.json * arrays, coming from Android, 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.setWeak(object, JsonSplittable.class.getName(), seen); } else if (object instanceof JSONArray) { seen = new JsonSplittable((JSONArray) object); WeakMapping.setWeak(object, JsonSplittable.class.getName(), seen); } else if (object instanceof String) { seen = new JsonSplittable(object.toString()); } else if (object instanceof Number) { seen = new JsonSplittable(((Number) object).doubleValue()); } else if (object instanceof Boolean) { seen = new JsonSplittable((Boolean) object); } else { throw new RuntimeException("Unhandled type " + object.getClass()); } } return seen; }
/** * Constructor that wraps an existing object. The parameters on this method * are reversed to avoid conflicting with the other two-arg constructor for * {@code AutoBean<Splittable>} instances. */ protected AbstractAutoBean(T wrapped, AutoBeanFactory factory) { this.factory = factory; usingSimplePeer = false; data = null; this.wrapped = wrapped; // Used by AutoBeanUtils WeakMapping.setWeak(wrapped, AutoBean.class.getName(), this); }
public static void setWeak(Object instance, String key, Object value) { set(instance, key, value); }
/** * Return the single AutoBean wrapper that is observing the delegate object or * {@code null} if the parameter is {@code null}or not wrapped by an AutoBean. * * @param delegate a delegate object, or {@code null} * @return the {@link AutoBean} wrapper for the delegate, or {@code null} */ @SuppressWarnings("unchecked") public static <T, U extends T> AutoBean<T> getAutoBean(U delegate) { return delegate == null ? null : (AutoBean<T>) WeakMapping.get(delegate, AutoBean.class .getName()); }
private synchronized JsonSplittable makeSplittable(Object object) { if (JSONObject.NULL.equals(object)) { return null; } /* * Maintain a 1:1 mapping between object instances and JsonSplittables. * Doing this with a WeakHashMap doesn't work, since our c.g.g.thirdparty.json * arrays, coming from Android, 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.setWeak(object, JsonSplittable.class.getName(), seen); } else if (object instanceof JSONArray) { seen = new JsonSplittable((JSONArray) object); WeakMapping.setWeak(object, JsonSplittable.class.getName(), seen); } else if (object instanceof String) { seen = new JsonSplittable(object.toString()); } else if (object instanceof Number) { seen = new JsonSplittable(((Number) object).doubleValue()); } else if (object instanceof Boolean) { seen = new JsonSplittable((Boolean) object); } else { throw new RuntimeException("Unhandled type " + object.getClass()); } } return seen; }
/** * Constructor that wraps an existing object. The parameters on this method * are reversed to avoid conflicting with the other two-arg constructor for * {@code AutoBean<Splittable>} instances. */ protected AbstractAutoBean(T wrapped, AutoBeanFactory factory) { this.factory = factory; usingSimplePeer = false; data = null; this.wrapped = wrapped; // Used by AutoBeanUtils WeakMapping.setWeak(wrapped, AutoBean.class.getName(), this); }
public static void setWeak(Object instance, String key, Object value) { set(instance, key, value); }
private T createShim() { T toReturn = ProxyAutoBean.makeProxy(beanType, new ShimHandler<T>(this, getWrapped())); WeakMapping.setWeak(toReturn, AutoBean.class.getName(), this); return toReturn; } }