ReferenceQueue rq = new ReferenceQueue();
bool = new Boolean(true);
try {
// Allow the finalizer to run to potentially enqueue
WeakReference wr = new WeakReference(bool, rq);
assertTrue("Initialization failed.", ((Boolean) wr.get())
.booleanValue());
} 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 WeakReference(bool, null);
} catch (NullPointerException e) {
exception = true;
}
assertTrue("Should not throw NullPointerException", !exception);