Examples of PhantomReference


Examples of java.lang.ref.PhantomReference

   * @tests java.lang.ref.PhantomReference#get()
   */
  public void test_get() {
    ReferenceQueue rq = new ReferenceQueue();
    bool = new Boolean(false);
    PhantomReference pr = new PhantomReference(bool, rq);
    assertNull("Same object returned.", pr.get());
  }
View Full Code Here

Examples of java.lang.ref.PhantomReference

   */
  public void test_ConstructorLjava_lang_ObjectLjava_lang_ref_ReferenceQueue() {
    ReferenceQueue rq = new ReferenceQueue();
    bool = new Boolean(true);
    try {
      PhantomReference pr = new PhantomReference(bool, rq);
      // Allow the finalizer to run to potentially enqueue
      Thread.sleep(1000);
      assertTrue("Initialization failed.", !pr.isEnqueued());
    } catch (Exception e) {
      fail("Exception during test : " + e.getMessage());
    }
    // need a reference to bool so the jit does not optimize it away
    assertTrue("should always pass", bool.booleanValue());

    boolean exception = false;
    try {
      new PhantomReference(bool, null);
    } catch (NullPointerException e) {
      exception = true;
    }
    assertTrue("Should not throw NullPointerException", !exception);
  }
View Full Code Here

Examples of java.lang.ref.PhantomReference

     * @param obj the object on which to wait
     */
    public WaitUnreachable(Object obj) {
        this.obj = obj;
        queue = new ReferenceQueue();
        ref = new PhantomReference(obj, queue);
    }
View Full Code Here

Examples of java.lang.ref.PhantomReference

      return keyRefEntrySet.size();
  }

  public Iterator iterator() {
      Iterator i = new WVEntryIterator(keyRefEntrySet.iterator());
      iteratorRefs.add(new PhantomReference(i, iteratorRefQueue));
      return i;
  }
View Full Code Here

Examples of java.lang.ref.PhantomReference

        // to the component (by waiting for the component to be garbage
        // collected).
        JComponent component = createTestComponent();
        JComponent overlay = createTestOverlay();
        final ReferenceQueue rq = new ReferenceQueue();
        final PhantomReference componentRef = new PhantomReference(component, rq);

        final JRootPane rootPane = new JRootPane() {
            public boolean isVisible() {
                return true;
            }
            public boolean isShowing() {
                return true;
            }
            protected JLayeredPane createLayeredPane() {
                return new JLayeredPane() {
                    public boolean isVisible() {
                        return true;
                    }
                    public boolean isShowing() {
                        return true;
                    }
                };
            }
        };
       
        final int lpcount = rootPane.getLayeredPane().getComponentCountInLayer(JLayeredPane.PALETTE_LAYER.intValue());
       
        OverlayHelper.attachOverlay(overlay, component, 0, 0, 0);
       
        assertEquals(lpcount, rootPane.getLayeredPane().getComponentCountInLayer(JLayeredPane.PALETTE_LAYER.intValue()));
        rootPane.getContentPane().add(component);
        // updateOverlay is "invokedLater", so wait for it...
        waitUntilEventQueueIsEmpty();
        assertEquals(lpcount + 1, rootPane.getLayeredPane().getComponentCountInLayer(JLayeredPane.PALETTE_LAYER.intValue()));
       
        rootPane.getContentPane().remove(component);
       
        // the remove operation make be invoked later, so wait for it...
        waitUntilEventQueueIsEmpty();
       
        // Clear out our strong references so it gets garbage collected.
        component = null;
        overlay = null;
       
        // Make sure the overlay was removed from the layered pane.
        assertEquals("It appears the overlay was not removed from the layered pane when its component was removed from the content pane", lpcount, rootPane.getLayeredPane().getComponentCountInLayer(JLayeredPane.PALETTE_LAYER.intValue()));
       
        // Make sure no other references are held... wait up to 15 seconds for
        // the object to be garbage collected.
        // Note: we may want to remove this section from the test as it
        // presumes the VM will garbage collect the reference fairly quickly
        // once a System.gc() is invoked.
        // There is no guarantee this will happen, so the assumption may be
        // faulty.  If it ends up being a problem, or starts yielding
        // inconsistent test results, then feel free to yank it.
        PhantomReference pr;
        final long end = System.currentTimeMillis() + 15000;
        do {
            System.gc();
        } while((pr = (PhantomReference)rq.remove(100)) == null && System.currentTimeMillis() < end);
        if(pr != null) {
            pr.clear();
        }
        assertSame("Either something else is still holding a strong reference to the component, or the VM is not garbage collecting it.  See comments in OverlayHelperTests.testRegressionOverlayHelperLeak() for more detail", pr, componentRef);
    }
View Full Code Here

Examples of java.lang.ref.PhantomReference

    }
   
    public void testEntriesAreRemovedIfKeyIsGarbageCollected() throws InterruptedException {
        String key = new String("key");
        ReferenceQueue refQueue = new ReferenceQueue();
        Reference ref = new PhantomReference(key, refQueue);
       
        Map map = new WeakCache();
        map.put(key, "value");
        key = null;

        int i = 0;
        while (refQueue.poll() == null) {
            ref.get(); // always null
            assertTrue("Key still alive even after "+i+" forced garbage collections", i++ < 5);
            Thread.sleep(10);
            System.gc();
        }
        assertEquals(0, map.size());
View Full Code Here

Examples of java.lang.ref.PhantomReference

    }
   
    public void testSelfReferencingEntriesAreRemovedIfKeyIsGarbageCollected() throws InterruptedException {
        String key = new String("key");
        ReferenceQueue refQueue = new ReferenceQueue();
        Reference ref = new PhantomReference(key, refQueue);
       
        Map map = new WeakCache();
        map.put(key, Collections.singleton(key));
        key = null;

        int i = 0;
        while (refQueue.poll() == null) {
            ref.get(); // always null
            assertTrue("Key still alive even after "+i+" forced garbage collections", i++ < 5);
            Thread.sleep(10);
            System.gc();
        }
        assertEquals(0, map.size());
View Full Code Here

Examples of java.lang.ref.PhantomReference

    }
   
    public void testEntriesAreRemovedIfValueIsGarbageCollected() throws InterruptedException {
        String value = new String("value");
        ReferenceQueue refQueue = new ReferenceQueue();
        Reference ref = new PhantomReference(value, refQueue);
       
        Map map = new WeakCache();
        map.put("key", value);
        value = null;

        int i = 0;
        while (refQueue.poll() == null) {
            ref.get(); // always null
            assertTrue("Value still alive even after "+i+" forced garbage collections", i++ < 5);
            Thread.sleep(10);
            System.gc();
        }
        assertEquals(0, map.size());
View Full Code Here

Examples of java.lang.ref.PhantomReference

   
    public void testSelfReferencingEntriesAreRemovedIfValueIsGarbageCollected() throws InterruptedException {
        String key = new String("key");
        Set value = Collections.singleton(key);
        ReferenceQueue refQueue = new ReferenceQueue();
        Reference ref = new PhantomReference(value, refQueue);
       
        Map map = new WeakCache();
        map.put(key, value);
        value = null;

        int i = 0;
        while (refQueue.poll() == null) {
            ref.get(); // always null
            assertTrue("Value still alive even after "+i+" forced garbage collections", i++ < 5);
            Thread.sleep(10);
            System.gc();
        }
        assertEquals(0, map.size());
View Full Code Here

Examples of java.lang.ref.PhantomReference

        ClassLoader classLoader = new URLClassLoader(new URL[]{proxyToys.toURI().toURL()}, getClass().getClassLoader());
        Class simpleReferenceType = classLoader.loadClass("com.thoughtworks.proxy.kit.SimpleReference");
        Field instance = simpleReferenceType.getDeclaredField("instance");
       
        ReferenceQueue refQueue = new ReferenceQueue();
        Reference ref = new PhantomReference(instance, refQueue);
       
        Map map = new WeakCache();
        map.put(simpleReferenceType, instance);
        simpleReferenceType = null;
        instance = null;

        int i = 0;
        while (refQueue.poll() == null) {
            ref.get(); // always null
            //assertTrue("Value still alive even after "+i+" forced garbage collections", i++ < 5);
            if (i++ >= 10) {
                // actually never reached - unfortunately
                break;
            }
View Full Code Here
TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.