001 /* java.lang.ref.Reference 002 Copyright (C) 1999, 2002, 2003, 2006 Free Software Foundation, Inc. 003 004 This file is part of GNU Classpath. 005 006 GNU Classpath is free software; you can redistribute it and/or modify 007 it under the terms of the GNU General Public License as published by 008 the Free Software Foundation; either version 2, or (at your option) 009 any later version. 010 011 GNU Classpath is distributed in the hope that it will be useful, but 012 WITHOUT ANY WARRANTY; without even the implied warranty of 013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 014 General Public License for more details. 015 016 You should have received a copy of the GNU General Public License 017 along with GNU Classpath; see the file COPYING. If not, write to the 018 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 019 02110-1301 USA. 020 021 Linking this library statically or dynamically with other modules is 022 making a combined work based on this library. Thus, the terms and 023 conditions of the GNU General Public License cover the whole 024 combination. 025 026 As a special exception, the copyright holders of this library give you 027 permission to link this library with independent modules to produce an 028 executable, regardless of the license terms of these independent 029 modules, and to copy and distribute the resulting executable under 030 terms of your choice, provided that you also meet, for each linked 031 independent module, the terms and conditions of the license of that 032 module. An independent module is a module which is not derived from 033 or based on this library. If you modify this library, you may extend 034 this exception to your version of the library, but you are not 035 obligated to do so. If you do not wish to do so, delete this 036 exception statement from your version. */ 037 038 039 package java.lang.ref; 040 041 /** 042 * This is the base class of all references. A reference allows 043 * refering to an object without preventing the garbage collector to 044 * collect it. The only way to get the referred object is via the 045 * <code>get()</code>-method. This method will return 046 * <code>null</code> if the object was collected. <br> 047 * 048 * A reference may be registered with a queue. When a referred 049 * element gets collected the reference will be put on the queue, so 050 * that you will be notified. <br> 051 * 052 * There are currently three types of references: soft reference, 053 * weak reference and phantom reference. <br> 054 * 055 * Soft references will be cleared if the garbage collector is told 056 * to free some memory and there are no unreferenced or weakly referenced 057 * objects. It is useful for caches. <br> 058 * 059 * Weak references will be cleared as soon as the garbage collector 060 * determines that the refered object is only weakly reachable. They 061 * are useful as keys in hashtables (see <code>WeakHashtable</code>) as 062 * you get notified when nobody has the key anymore. 063 * 064 * Phantom references don't prevent finalization. If an object is only 065 * phantom reachable, it will be finalized, and the reference will be 066 * enqueued, but not cleared. Since you mustn't access an finalized 067 * object, the <code>get</code> method of a phantom reference will never 068 * work. It is useful to keep track, when an object is finalized. 069 * 070 * @author Jochen Hoenicke 071 * @see java.util.WeakHashtable 072 */ 073 public abstract class Reference<T> 074 { 075 /** 076 * The underlying object. This field is handled in a special way by 077 * the garbage collector. 078 * GCJ LOCAL: 079 * This is a RawData because it must be disguised from the GC. 080 * END GCJ LOCAL 081 */ 082 gnu.gcj.RawData referent; 083 084 /** 085 * This is like REFERENT but is not scanned by the GC. We keep a 086 * copy around so that we can clean up our internal data structure 087 * even after clear() is called. 088 * GCJ LOCAL: 089 * This field doesn't exist in Classpath. 090 * END GCJ LOCAL 091 */ 092 gnu.gcj.RawData copy; 093 094 /** 095 * Set to true if {@link #clear()} is called. 096 * GCJ LOCAL: 097 * This field doesn't exist in Classpath. It is used internally in 098 * natReference.cc, which enqueues the reference unless it is true 099 * (has been cleared). 100 * END GCJ LOCAL 101 */ 102 boolean cleared = false; 103 104 /** 105 * The queue this reference is registered on. This is null, if this 106 * wasn't registered to any queue or reference was already enqueued. 107 */ 108 ReferenceQueue<? super T> queue; 109 110 /** 111 * Link to the next entry on the queue. If this is null, this 112 * reference is not enqueued. Otherwise it points to the next 113 * reference. The last reference on a queue will point to itself 114 * (not to null, that value is used to mark a not enqueued 115 * reference). 116 */ 117 Reference nextOnQueue; 118 119 /** 120 * This lock should be taken by the garbage collector, before 121 * determining reachability. It will prevent the get()-method to 122 * return the reference so that reachability doesn't change. 123 */ 124 static Object lock = new Object(); 125 126 /** 127 * Creates a new reference that is not registered to any queue. 128 * Since it is package private, it is not possible to overload this 129 * class in a different package. 130 * @param referent the object we refer to. 131 */ 132 Reference(T ref) 133 { 134 create (ref); 135 } 136 137 /** 138 * Creates a reference that is registered to a queue. Since this is 139 * package private, it is not possible to overload this class in a 140 * different package. 141 * @param referent the object we refer to. 142 * @param q the reference queue to register on. 143 * @exception NullPointerException if q is null. 144 */ 145 Reference(T ref, ReferenceQueue<? super T> q) 146 { 147 if (q == null) 148 throw new NullPointerException(); 149 queue = q; 150 create (ref); 151 } 152 153 /** 154 * Notifies the VM that a new Reference has been created. 155 */ 156 private native void create (T o); 157 158 /** 159 * Returns the object, this reference refers to. 160 * @return the object, this reference refers to, or null if the 161 * reference was cleared. 162 */ 163 public native T get(); 164 165 /** 166 * Clears the reference, so that it doesn't refer to its object 167 * anymore. For soft and weak references this is called by the 168 * garbage collector. For phantom references you should call 169 * this when enqueuing the reference. 170 */ 171 public void clear() 172 { 173 // Must synchronize so changes are visible in finalizer thread. 174 synchronized (lock) 175 { 176 referent = null; 177 cleared = true; 178 } 179 } 180 181 /** 182 * Tells if the object is enqueued on a reference queue. 183 * @return true if it is enqueued, false otherwise. 184 */ 185 public boolean isEnqueued() 186 { 187 return nextOnQueue != null; 188 } 189 190 /** 191 * Enqueue an object on a reference queue. This is normally executed 192 * by the garbage collector. 193 */ 194 public boolean enqueue() 195 { 196 if (queue != null && nextOnQueue == null) 197 { 198 queue.enqueue(this); 199 queue = null; 200 return true; 201 } 202 return false; 203 } 204 }