Search in sources :

Example 31 with WeakReference

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));
}
Also used : Injector(com.google.inject.Injector) WeakReference(java.lang.ref.WeakReference) AbstractModule(com.google.inject.AbstractModule)

Example 32 with WeakReference

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);
    }
}
Also used : WeakReference(java.lang.ref.WeakReference) ArrayMap(android.util.ArrayMap) Method(java.lang.reflect.Method) Resources(android.content.res.Resources) IOException(java.io.IOException)

Example 33 with WeakReference

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();
    }
}
Also used : WeakReference(java.lang.ref.WeakReference) Iterator(java.util.Iterator)

Example 34 with WeakReference

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));
}
Also used : WeakReference(java.lang.ref.WeakReference)

Example 35 with WeakReference

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);
        }
    }
}
Also used : WeakReference(java.lang.ref.WeakReference)

Aggregations

WeakReference (java.lang.ref.WeakReference)291 ArrayList (java.util.ArrayList)32 HashMap (java.util.HashMap)24 Map (java.util.Map)24 Field (java.lang.reflect.Field)19 Activity (android.app.Activity)17 Iterator (java.util.Iterator)17 IOException (java.io.IOException)16 Method (java.lang.reflect.Method)15 File (java.io.File)14 URLClassLoader (java.net.URLClassLoader)14 Test (org.junit.Test)13 Reference (java.lang.ref.Reference)12 ReferenceQueue (java.lang.ref.ReferenceQueue)12 Resources (android.content.res.Resources)10 Handler (android.os.Handler)10 AbstractModule (com.google.inject.AbstractModule)10 Injector (com.google.inject.Injector)10 ArrayMap (android.util.ArrayMap)9 URL (java.net.URL)8