Search in sources :

Example 41 with SoftReference

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

the class SmartPsiElementPointersTest method testSmartPointersSurvivePsiFileUnload.

public void testSmartPointersSurvivePsiFileUnload() throws IOException {
    final VirtualFile vfile = createChildData(myRoot, "X.txt");
    String xxx = "xxx";
    String text = xxx + " " + xxx + " " + xxx;
    setFileText(vfile, text);
    PsiFile psiFile = PsiManager.getInstance(getProject()).findFile(vfile);
    assertTrue(String.valueOf(psiFile), psiFile instanceof PsiPlainTextFile);
    SmartPointerManagerImpl manager = getPointerManager();
    TextRange range1 = TextRange.from(text.indexOf(xxx), xxx.length());
    SmartPsiFileRange pointer1 = manager.createSmartPsiFileRangePointer(psiFile, range1);
    TextRange range2 = TextRange.from(text.lastIndexOf(xxx), xxx.length());
    SmartPsiFileRange pointer2 = manager.createSmartPsiFileRangePointer(psiFile, range2);
    assertNotNull(FileDocumentManager.getInstance().getCachedDocument(vfile));
    SoftReference<PsiFile> ref = new SoftReference<>(psiFile);
    psiFile = null;
    while (ref.get() != null) {
        PlatformTestUtil.tryGcSoftlyReachableObjects();
    }
    assertNull(FileDocumentManager.getInstance().getCachedDocument(vfile));
    assertEquals(pointer1.getRange(), range1);
    WriteCommandAction.runWriteCommandAction(getProject(), () -> insertString(FileDocumentManager.getInstance().getDocument(vfile), 0, " "));
    assertEquals(range1.shiftRight(1), pointer1.getRange());
    assertEquals(range2.shiftRight(1), pointer2.getRange());
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) SoftReference(java.lang.ref.SoftReference) TextRange(com.intellij.openapi.util.TextRange)

Example 42 with SoftReference

use of java.lang.ref.SoftReference in project wildfly by wildfly.

the class WorkCacheManager method lookupDone.

/**
     * Lookup an analysis in the fully done map.
     */
private ContainerAnalysis lookupDone(Class cls) {
    SoftReference ref = (SoftReference) workDone.get(cls);
    if (ref == null)
        return null;
    ContainerAnalysis ret = (ContainerAnalysis) ref.get();
    if (ret == null)
        // clear map entry if soft ref. was cleared.
        workDone.remove(cls);
    return ret;
}
Also used : SoftReference(java.lang.ref.SoftReference)

Example 43 with SoftReference

use of java.lang.ref.SoftReference in project joda-time by JodaOrg.

the class ZoneInfoProvider method getZone.

//-----------------------------------------------------------------------
/**
     * If an error is thrown while loading zone data, the exception is logged
     * to system error and null is returned for this and all future requests.
     * 
     * @param id  the id to load
     * @return the loaded zone
     */
public DateTimeZone getZone(String id) {
    if (id == null) {
        return null;
    }
    Object obj = iZoneInfoMap.get(id);
    if (obj == null) {
        return null;
    }
    if (obj instanceof SoftReference<?>) {
        @SuppressWarnings("unchecked") SoftReference<DateTimeZone> ref = (SoftReference<DateTimeZone>) obj;
        DateTimeZone tz = ref.get();
        if (tz != null) {
            return tz;
        }
        // Reference cleared; load data again.
        return loadZoneData(id);
    } else if (id.equals(obj)) {
        // Load zone data for the first time.
        return loadZoneData(id);
    }
    // If this point is reached, mapping must link to another.
    return getZone((String) obj);
}
Also used : SoftReference(java.lang.ref.SoftReference) DateTimeZone(org.joda.time.DateTimeZone)

Example 44 with SoftReference

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

the class ReferenceQueueTest method test_remove.

/**
     * java.lang.ref.ReferenceQueue#remove()
     */
public void test_remove() {
    // store in a static so it won't be gc'ed because the jit
    // optimized it out
    b = new Boolean(true);
    SoftReference sr = new SoftReference(b, rq);
    sr.enqueue();
    try {
        assertTrue("Remove failed.", ((Boolean) rq.remove().get()).booleanValue());
    } catch (Exception e) {
        fail("Exception during the test : " + e.getMessage());
    }
    assertNull(rq.poll());
    sr.enqueue();
    class RemoveThread extends Thread {

        public void run() {
            try {
                rq.remove();
            } catch (InterruptedException ie) {
                isThrown = true;
            }
        }
    }
    RemoveThread rt = new RemoveThread();
    rt.start();
    try {
        Thread.sleep(100);
    } catch (InterruptedException ie) {
    }
    rt.interrupt();
    try {
        Thread.sleep(100);
    } catch (InterruptedException ie) {
    }
    assertTrue(isThrown);
    assertNull(rq.poll());
}
Also used : SoftReference(java.lang.ref.SoftReference)

Example 45 with SoftReference

use of java.lang.ref.SoftReference 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)

Aggregations

SoftReference (java.lang.ref.SoftReference)56 ReferenceQueue (java.lang.ref.ReferenceQueue)7 Reference (java.lang.ref.Reference)6 WeakReference (java.lang.ref.WeakReference)6 PhantomReference (java.lang.ref.PhantomReference)5 Random (java.util.Random)5 IOException (java.io.IOException)4 Bitmap (android.graphics.Bitmap)3 Context (android.content.Context)2 BitmapFactory (android.graphics.BitmapFactory)2 Handler (android.os.Handler)2 Message (android.os.Message)2 MetaClass (groovy.lang.MetaClass)2 Shape (java.awt.Shape)2 File (java.io.File)2 InputStream (java.io.InputStream)2 UndeclaredThrowableException (java.lang.reflect.UndeclaredThrowableException)2 PrivilegedAction (java.security.PrivilegedAction)2 PrivilegedActionException (java.security.PrivilegedActionException)2 SecureClassLoader (java.security.SecureClassLoader)2