use of java.util.WeakHashMap in project mockito by mockito.
the class TypeCachingMockBytecodeGeneratorTest method validate_simple_code_idea_where_weakhashmap_with_classloader_as_key_get_GCed_when_no_more_references.
@Test
public void validate_simple_code_idea_where_weakhashmap_with_classloader_as_key_get_GCed_when_no_more_references() throws Exception {
// given
WeakHashMap<ClassLoader, Object> cache = new WeakHashMap<ClassLoader, Object>();
ClassLoader short_lived_classloader = inMemoryClassLoader().withClassDefinition("foo.Bar", makeMarkerInterface("foo.Bar")).build();
cache.put(short_lived_classloader, new HoldingAReference(new WeakReference<Class<?>>(short_lived_classloader.loadClass("foo.Bar"))));
assertThat(cache).hasSize(1);
// when
short_lived_classloader = is_no_more_referenced();
System.gc();
ensure_gc_happened();
// then
assertThat(cache).isEmpty();
}
use of java.util.WeakHashMap in project gerrit by GerritCodeReview.
the class DynamicOptions method getMergedClassLoader.
protected ClassLoader getMergedClassLoader(ClassLoader beanCl, ClassLoader dynamicBeanCl) {
Map<ClassLoader, WeakReference<ClassLoader>> mergedClByCl = mergedClByCls.get(beanCl);
if (mergedClByCl == null) {
mergedClByCl = Collections.synchronizedMap(new WeakHashMap<>());
mergedClByCls.put(beanCl, mergedClByCl);
}
WeakReference<ClassLoader> mergedClRef = mergedClByCl.get(dynamicBeanCl);
ClassLoader mergedCl = null;
if (mergedClRef != null) {
mergedCl = mergedClRef.get();
}
if (mergedCl == null) {
mergedCl = new DelegatingClassLoader(beanCl, dynamicBeanCl);
mergedClByCl.put(dynamicBeanCl, new WeakReference<>(mergedCl));
}
return mergedCl;
}
use of java.util.WeakHashMap in project guava by google.
the class GcFinalizationTest method testAwaitDone_FinalizationPredicate.
public void testAwaitDone_FinalizationPredicate() {
final WeakHashMap<Object, Object> map = new WeakHashMap<>();
map.put(new Object(), Boolean.TRUE);
GcFinalization.awaitDone(new FinalizationPredicate() {
@Override
public boolean isDone() {
return map.isEmpty();
}
});
assertTrue(map.isEmpty());
}
use of java.util.WeakHashMap in project j2objc by google.
the class AbstractMapTest method test_values.
/**
* java.util.AbstractMap#values()
*/
public void test_values() {
AbstractMap map1 = new HashMap(0);
assertSame("HashMap(0)", map1.values(), map1.values());
AbstractMap map2 = new HashMap(10);
assertSame("HashMap(10)", map2.values(), map2.values());
Map map3 = Collections.EMPTY_MAP;
assertSame("EMPTY_MAP", map3.values(), map3.values());
AbstractMap map4 = new IdentityHashMap(1);
assertSame("IdentityHashMap", map4.values(), map4.values());
AbstractMap map5 = new LinkedHashMap(122);
assertSame("IdentityHashMap", map5.values(), map5.values());
AbstractMap map6 = new TreeMap();
assertSame("TreeMap", map6.values(), map6.values());
AbstractMap map7 = new WeakHashMap();
assertSame("WeakHashMap", map7.values(), map7.values());
}
use of java.util.WeakHashMap in project j2objc by google.
the class AbstractMapTest method test_removeLjava_lang_Object.
/**
* java.util.AbstractMap#remove(java.lang.Object)
*/
public void test_removeLjava_lang_Object() {
Object key = new Object();
Object value = new Object();
AbstractMap map1 = new HashMap(0);
map1.put("key", value);
assertSame("HashMap(0)", map1.remove("key"), value);
AbstractMap map4 = new IdentityHashMap(1);
map4.put(key, value);
assertSame("IdentityHashMap", map4.remove(key), value);
AbstractMap map5 = new LinkedHashMap(122);
map5.put(key, value);
assertSame("LinkedHashMap", map5.remove(key), value);
AbstractMap map6 = new TreeMap(new Comparator() {
// Bogus comparator
public int compare(Object object1, Object object2) {
return 0;
}
});
map6.put(key, value);
assertSame("TreeMap", map6.remove(key), value);
AbstractMap map7 = new WeakHashMap();
map7.put(key, value);
assertSame("WeakHashMap", map7.remove(key), value);
AbstractMap aSpecialMap = new MyMap();
aSpecialMap.put(specialKey, specialValue);
Object valueOut = aSpecialMap.remove(specialKey);
assertSame("MyMap", valueOut, specialValue);
}
Aggregations