cleanup(); if (instance instanceof String) { throw new IllegalArgumentException("Cannot use Strings with WeakMapping");
/** * 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; }