| 1 | /* |
| 2 | * $Id: AssertNoReference.java,v 1.1.1.5 2004/05/25 20:23:30 hastings Exp $ |
| 3 | * |
| 4 | * (c) Copyright, Moebius Solutions, Inc., 2004 |
| 5 | * |
| 6 | * All Rights Reserved |
| 7 | * |
| 8 | * This material may be reproduced by or for the U. S. Government |
| 9 | * pursuant to the copyright license under the clause at |
| 10 | * DFARS 252.227-7014 (OCT 2001). |
| 11 | */ |
| 12 | |
| 13 | package com.moesol.ref; |
| 14 | |
| 15 | import java.lang.ref.WeakReference; |
| 16 | |
| 17 | /** |
| 18 | * Allow assertions to be made about strong object references. |
| 19 | * <pre> |
| 20 | * Object test_object = new Object(); |
| 21 | * AssertNoReference ref_check = new AssertNoReference(test_object); |
| 22 | * // use test_object |
| 23 | * test_object = null; |
| 24 | * assert(!ref_check.isReferenced()); |
| 25 | * </pre> |
| 26 | */ |
| 27 | public class AssertNoReference extends WeakReference { |
| 28 | public AssertNoReference(Object referent) { |
| 29 | super(referent); |
| 30 | } |
| 31 | /** |
| 32 | * @return true if there are strong references to |
| 33 | * <code>referent</code>. |
| 34 | */ |
| 35 | public boolean isReferenced() { |
| 36 | // Without this yield the gc would fail to mark some |
| 37 | // references as finalizable. |
| 38 | Thread.yield(); |
| 39 | // Force a garbage collection cycle to be sure that newly |
| 40 | // finalizable references are found. |
| 41 | System.gc(); |
| 42 | return null != get(); |
| 43 | } |
| 44 | } |