Search in sources :

Example 21 with Reference

use of java.lang.ref.Reference in project robovm by robovm.

the class ReferenceTest method test_enqueue.

/**
     * java.lang.ref.Reference#enqueue()
     */
public void test_enqueue() {
    ReferenceQueue rq = new ReferenceQueue();
    obj = new Object();
    Reference ref = new SoftReference(obj, rq);
    assertTrue("Enqueue failed.", (!ref.isEnqueued()) && ((ref.enqueue()) && (ref.isEnqueued())));
    assertTrue("Not properly enqueued.", rq.poll().get() == obj);
    // This fails...
    assertTrue("Should remain enqueued.", !ref.isEnqueued());
    assertTrue("Can not enqueue twice.", (!ref.enqueue()) && (rq.poll() == null));
    rq = new ReferenceQueue();
    obj = new Object();
    ref = new WeakReference(obj, rq);
    assertTrue("Enqueue failed2.", (!ref.isEnqueued()) && ((ref.enqueue()) && (ref.isEnqueued())));
    assertTrue("Not properly enqueued2.", rq.poll().get() == obj);
    // This
    assertTrue("Should remain enqueued2.", !ref.isEnqueued());
    // fails.
    assertTrue("Can not enqueue twice2.", (!ref.enqueue()) && (rq.poll() == null));
    ref = new PhantomReference(obj, rq);
    assertTrue("Enqueue failed3.", (!ref.isEnqueued()) && ((ref.enqueue()) && (ref.isEnqueued())));
    assertNull("Not properly enqueued3.", rq.poll().get());
    // This
    assertTrue("Should remain enqueued3.", !ref.isEnqueued());
    // fails.
    assertTrue("Can not enqueue twice3.", (!ref.enqueue()) && (rq.poll() == null));
}
Also used : ReferenceQueue(java.lang.ref.ReferenceQueue) SoftReference(java.lang.ref.SoftReference) PhantomReference(java.lang.ref.PhantomReference) Reference(java.lang.ref.Reference) SoftReference(java.lang.ref.SoftReference) WeakReference(java.lang.ref.WeakReference) WeakReference(java.lang.ref.WeakReference) PhantomReference(java.lang.ref.PhantomReference)

Example 22 with Reference

use of java.lang.ref.Reference in project robovm by robovm.

the class ReferenceTest method test_subclass.

/**
     * Makes sure that overridden versions of clear() and enqueue()
     * get called, and that clear/enqueue/finalize happen in the
     * right order for WeakReferences.
     *
     * java.lang.ref.Reference#clear()
     * java.lang.ref.Reference#enqueue()
     * java.lang.Object#finalize()
     */
public void test_subclass() {
    error = null;
    testObjectFinalized = false;
    twr = null;
    class TestObject {

        public TestWeakReference testWeakReference = null;

        public void setTestWeakReference(TestWeakReference twr) {
            testWeakReference = twr;
        }

        protected void finalize() {
            testObjectFinalized = true;
        }
    }
    final ReferenceQueue rq = new ReferenceQueue();
    class TestThread extends Thread {

        public void run() {
            // Create the object in a separate thread to ensure it will be
            // gc'ed
            TestObject testObj = new TestObject();
            twr = new TestWeakReference(testObj, rq);
            testObj.setTestWeakReference(twr);
            testObj = null;
        }
    }
    Reference ref;
    try {
        Thread t = new TestThread();
        t.start();
        t.join();
        FinalizationTester.induceFinalization();
        // Give up after five seconds.
        ref = rq.remove(5000L);
        assertNotNull("Object not garbage collected.", ref);
        assertTrue("Unexpected reference.", ref == twr);
        assertNull("Object could not be reclaimed.", twr.get());
        //assertTrue("Overridden clear() should have been called.",
        //       twr.clearSeen);
        //assertTrue("Overridden enqueue() should have been called.",
        //        twr.enqueueSeen);
        assertTrue("finalize() should have been called.", testObjectFinalized);
    } catch (InterruptedException e) {
        fail("InterruptedException : " + e.getMessage());
    }
}
Also used : ReferenceQueue(java.lang.ref.ReferenceQueue) PhantomReference(java.lang.ref.PhantomReference) Reference(java.lang.ref.Reference) SoftReference(java.lang.ref.SoftReference) WeakReference(java.lang.ref.WeakReference)

Example 23 with Reference

use of java.lang.ref.Reference in project morphia by mongodb.

the class ReferenceMap method purge.

/**
     * Purges stale mappings from this map.<P>
     * <p/>
     * Ordinarily, stale mappings are only removed during a write operation; typically a write operation will occur often enough that
     * you'll
     * never need to manually invoke this method.<P>
     * <p/>
     * Note that this method is not synchronized!  Special care must be taken if, for instance, you want stale mappings to be removed on a
     * periodic basis by some background thread.
     */
private void purge() {
    Reference ref = queue.poll();
    while (ref != null) {
        purge(ref);
        ref = queue.poll();
    }
}
Also used : Reference(java.lang.ref.Reference) SoftReference(java.lang.ref.SoftReference) WeakReference(java.lang.ref.WeakReference)

Example 24 with Reference

use of java.lang.ref.Reference in project spring-loaded by spring-projects.

the class JVMPlugin method clearThreadGroupContext.

private boolean clearThreadGroupContext(Class<?> clazz) {
    boolean beanInfoCacheCleared = false;
    try {
        if (threadGroupContextClass == null) {
            threadGroupContextClass = Class.forName("java.beans.ThreadGroupContext", true, Introspector.class.getClassLoader());
        }
        if (threadGroupContextClass != null) {
            if (threadGroupContext_contextsField == null) {
                threadGroupContext_contextsField = threadGroupContextClass.getDeclaredField("contexts");
                threadGroupContext_removeBeanInfoMethod = threadGroupContextClass.getDeclaredMethod("removeBeanInfo", Class.class);
            }
            if (threadGroupContext_contextsField != null) {
                threadGroupContext_contextsField.setAccessible(true);
                Object threadGroupContext_contextsField_value = threadGroupContext_contextsField.get(null);
                if (threadGroupContext_contextsField_value == null) {
                    beanInfoCacheCleared = true;
                } else {
                    if (threadGroupContext_contextsField_value instanceof Map) {
                        // Indicates Java 7 up to rev21
                        Map<?, ?> m = (Map<?, ?>) threadGroupContext_contextsField_value;
                        Collection<?> threadGroupContexts = m.values();
                        for (Object o : threadGroupContexts) {
                            threadGroupContext_removeBeanInfoMethod.setAccessible(true);
                            threadGroupContext_removeBeanInfoMethod.invoke(o, clazz);
                        }
                        beanInfoCacheCleared = true;
                    } else {
                        // At update Java7u21 it changes
                        Class weakIdentityMapClazz = threadGroupContext_contextsField.getType();
                        Field tableField = weakIdentityMapClazz.getDeclaredField("table");
                        tableField.setAccessible(true);
                        Reference<?>[] refs = (Reference[]) tableField.get(threadGroupContext_contextsField_value);
                        Field valueField = null;
                        if (refs != null) {
                            for (int i = 0; i < refs.length; i++) {
                                Reference<?> r = refs[i];
                                Object o = (r == null ? null : r.get());
                                if (o != null) {
                                    if (valueField == null) {
                                        valueField = r.getClass().getDeclaredField("value");
                                    }
                                    valueField.setAccessible(true);
                                    Object threadGroupContext = valueField.get(r);
                                    threadGroupContext_removeBeanInfoMethod.setAccessible(true);
                                    threadGroupContext_removeBeanInfoMethod.invoke(threadGroupContext, clazz);
                                }
                            }
                        }
                        beanInfoCacheCleared = true;
                    }
                }
            }
        }
    } catch (Throwable t) {
        System.err.println("Unexpected problem clearing ThreadGroupContext beaninfo: ");
        t.printStackTrace();
    }
    return beanInfoCacheCleared;
}
Also used : Field(java.lang.reflect.Field) Reference(java.lang.ref.Reference) Map(java.util.Map) ConcurrentMap(java.util.concurrent.ConcurrentMap)

Example 25 with Reference

use of java.lang.ref.Reference in project intellij-community by JetBrains.

the class AbstractClassGenerator method create.

protected Object create(Object key) {
    try {
        Class gen = null;
        synchronized (source) {
            ClassLoader loader = getClassLoader();
            Map cache2 = null;
            cache2 = (Map) source.cache.get(loader);
            if (cache2 == null) {
                cache2 = new HashMap();
                cache2.put(NAME_KEY, new HashSet());
                source.cache.put(loader, cache2);
            } else if (useCache) {
                Reference ref = (Reference) cache2.get(key);
                gen = (Class) SoftReference.dereference(ref);
            }
            if (gen == null) {
                Object save = CURRENT.get();
                CURRENT.set(this);
                try {
                    this.key = key;
                    if (attemptLoad) {
                        try {
                            gen = loader.loadClass(getClassName());
                        } catch (ClassNotFoundException e) {
                        // ignore
                        }
                    }
                    if (gen == null) {
                        byte[] b = strategy.generate(this);
                        String className = ClassNameReader.getClassName(new $ClassReader(b));
                        getClassNameCache(loader).add(className);
                        gen = ReflectUtils.defineClass(className, b, loader);
                    }
                    if (useCache) {
                        cache2.put(key, new WeakReference(gen));
                    }
                    return firstInstance(gen);
                } finally {
                    CURRENT.set(save);
                }
            }
        }
        return firstInstance(gen);
    } catch (RuntimeException e) {
        throw e;
    } catch (Error e) {
        throw e;
    } catch (Exception e) {
        throw new CodeGenerationException(e);
    }
}
Also used : Reference(java.lang.ref.Reference) SoftReference(com.intellij.reference.SoftReference) WeakReference(java.lang.ref.WeakReference) net.sf.cglib.asm.$ClassReader(net.sf.cglib.asm.$ClassReader) WeakReference(java.lang.ref.WeakReference)

Aggregations

Reference (java.lang.ref.Reference)31 WeakReference (java.lang.ref.WeakReference)17 PhantomReference (java.lang.ref.PhantomReference)10 ReferenceQueue (java.lang.ref.ReferenceQueue)10 SoftReference (java.lang.ref.SoftReference)10 Field (java.lang.reflect.Field)5 Map (java.util.Map)5 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 ConcurrentMap (java.util.concurrent.ConcurrentMap)3 IOException (java.io.IOException)2 AccessControlContext (java.security.AccessControlContext)2 HashSet (java.util.HashSet)2 Iterator (java.util.Iterator)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 AtomicReference (java.util.concurrent.atomic.AtomicReference)2 Supplier (com.google.common.base.Supplier)1 SoftReference (com.intellij.reference.SoftReference)1 SideEffect (dalvik.annotation.SideEffect)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1