Search in sources :

Example 46 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)

Example 47 with Reference

use of java.lang.ref.Reference in project ACS by ACS-Community.

the class GarbageCollectionTestHelper method waitForGC.

public boolean waitForGC() {
    waitingThread = Thread.currentThread();
    Reference retRef = null;
    System.gc();
    while (true) {
        try {
            //@
            System.err.println("refQ:waiting to remove()");
            retRef = refQ.remove();
            //@
            System.err.println("refQ:removed()");
            break;
        } catch (InterruptedException ex) {
            if (cancelFlag) {
                break;
            }
            // ignore and continue
            continue;
        }
    }
    waitingThread = null;
    return (retRef != null);
}
Also used : PhantomReference(java.lang.ref.PhantomReference) Reference(java.lang.ref.Reference)

Example 48 with Reference

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

the class ReferenceQueueTest method test_removeJ.

/**
     * java.lang.ref.ReferenceQueue#remove(long)
     */
public void test_removeJ() {
    try {
        assertNull("Queue should be empty. (poll)", rq.poll());
        assertNull("Queue should be empty. (remove(1))", rq.remove((long) 1));
        Thread ct = new Thread(new ChildThread());
        ct.start();
        Reference ret = rq.remove(0L);
        assertNotNull("Delayed remove failed.", ret);
    } catch (InterruptedException e) {
        fail("InterruptedExeException during test : " + e.getMessage());
    } catch (Exception e) {
        fail("Exception during test : " + e.getMessage());
    }
    Object obj = new Object();
    WeakReference wr = new WeakReference(obj, rq);
    Boolean b = new Boolean(true);
    SoftReference sr = new SoftReference(b, rq);
    String str = "Test";
    PhantomReference pr = new PhantomReference(str, rq);
    pr.enqueue();
    wr.enqueue();
    sr.enqueue();
    try {
        Reference result = rq.remove(1L);
        assertTrue((Boolean) result.get());
        result = rq.remove(1L);
        assertEquals(obj, result.get());
        result = rq.remove(1L);
        assertNull(result.get());
    } catch (IllegalArgumentException e1) {
        fail("IllegalArgumentException was thrown.");
    } catch (InterruptedException e1) {
        fail("InterruptedException was thrown.");
    }
    rq = new ReferenceQueue();
    isThrown = false;
    assertNull(rq.poll());
    class RemoveThread extends Thread {

        public void run() {
            try {
                rq.remove(1000L);
            } catch (InterruptedException ie) {
                isThrown = true;
            }
        }
    }
    RemoveThread rt = new RemoveThread();
    rt.start();
    try {
        Thread.sleep(10);
    } catch (InterruptedException ie) {
    }
    rt.interrupt();
    try {
        Thread.sleep(10);
    } catch (InterruptedException ie) {
    }
    assertTrue(isThrown);
    assertNull(rq.poll());
    try {
        rq.remove(-1);
        fail("IllegalArgumentException expected.");
    } catch (IllegalArgumentException iae) {
    //expected
    } catch (InterruptedException e) {
        fail("Unexpected InterruptedException.");
    }
}
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) SoftReference(java.lang.ref.SoftReference) WeakReference(java.lang.ref.WeakReference) PhantomReference(java.lang.ref.PhantomReference)

Example 49 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 50 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)

Aggregations

Reference (java.lang.ref.Reference)74 WeakReference (java.lang.ref.WeakReference)34 SoftReference (java.lang.ref.SoftReference)33 ReferenceQueue (java.lang.ref.ReferenceQueue)13 Field (java.lang.reflect.Field)13 Map (java.util.Map)13 HashMap (java.util.HashMap)11 PhantomReference (java.lang.ref.PhantomReference)10 ArrayList (java.util.ArrayList)7 List (java.util.List)5 Tracing (brave.Tracing)3 Platform (brave.internal.Platform)3 TraceContext (brave.propagation.TraceContext)3 BufferedImage (java.awt.image.BufferedImage)3 VolatileImage (java.awt.image.VolatileImage)3 IOException (java.io.IOException)3 IllegalClassFormatException (java.lang.instrument.IllegalClassFormatException)3 ConcurrentModificationException (java.util.ConcurrentModificationException)3 Iterator (java.util.Iterator)3 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)3