use of java.lang.ref.WeakReference in project roboguice by roboguice.
the class OptionalBinderTest method testWeakKeySet_integration.
// Tests for com.google.inject.internal.WeakKeySet not leaking memory.
public void testWeakKeySet_integration() {
Injector parentInjector = Guice.createInjector(new AbstractModule() {
@Override
protected void configure() {
bind(String.class).toInstance("hi");
}
});
WeakKeySetUtils.assertNotBlacklisted(parentInjector, Key.get(Integer.class));
Injector childInjector = parentInjector.createChildInjector(new AbstractModule() {
@Override
protected void configure() {
OptionalBinder.newOptionalBinder(binder(), Integer.class).setDefault().toInstance(4);
}
});
WeakReference<Injector> weakRef = new WeakReference<Injector>(childInjector);
WeakKeySetUtils.assertBlacklisted(parentInjector, Key.get(Integer.class));
// Clear the ref, GC, and ensure that we are no longer blacklisting.
childInjector = null;
WeakKeySetUtils.awaitClear(weakRef);
WeakKeySetUtils.assertNotBlacklisted(parentInjector, Key.get(Integer.class));
}
use of java.lang.ref.WeakReference in project buck by facebook.
the class ResourcesLoader method getActiveResourcesFromResourcesManager.
private static Collection<WeakReference<Resources>> getActiveResourcesFromResourcesManager() {
try {
Class resourcesManagerClass;
try {
resourcesManagerClass = Class.forName("android.app.ResourcesManager");
} catch (ClassNotFoundException e) {
return null;
}
Method getResourcesManager = resourcesManagerClass.getDeclaredMethod("getInstance");
getResourcesManager.setAccessible(true);
Object resourcesManager = getResourcesManager.invoke(null);
try {
return ((ArrayMap<?, WeakReference<Resources>>) Reflect.getField(resourcesManager, resourcesManagerClass, "mActiveResources")).values();
} catch (NoSuchFieldException e) {
return (Collection<WeakReference<Resources>>) Reflect.getField(resourcesManager, resourcesManagerClass, "mResourceReferences");
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}
use of java.lang.ref.WeakReference in project Meizhi by drakeet.
the class SmoothAppBarLayout method removeOnOffsetSyncedListener.
public void removeOnOffsetSyncedListener(OnOffsetSyncedListener listener) {
Iterator i = mOffsetSyncedListeners.iterator();
while (true) {
OnOffsetSyncedListener item;
do {
if (!i.hasNext()) {
return;
}
WeakReference ref = (WeakReference) i.next();
item = (OnOffsetSyncedListener) ref.get();
} while (item != listener && item != null);
i.remove();
}
}
use of java.lang.ref.WeakReference in project Meizhi by drakeet.
the class SmoothAppBarLayout method addOnOffsetSyncedListener.
public void addOnOffsetSyncedListener(OnOffsetSyncedListener listener) {
int i = 0;
for (int z = this.mOffsetSyncedListeners.size(); i < z; ++i) {
WeakReference ref = (WeakReference) this.mOffsetSyncedListeners.get(i);
if (ref != null && ref.get() == listener) {
return;
}
}
this.mOffsetSyncedListeners.add(new WeakReference(listener));
}
use of java.lang.ref.WeakReference in project Meizhi by drakeet.
the class SmoothAppBarLayout method syncOffset.
public void syncOffset() {
int i = 0;
for (int z = mOffsetSyncedListeners.size(); i < z; ++i) {
WeakReference ref = (WeakReference) mOffsetSyncedListeners.get(i);
OnOffsetSyncedListener listener = ref != null ? (OnOffsetSyncedListener) ref.get() : null;
if (listener != null) {
listener.onOffsetSynced(this);
}
}
}
Aggregations