@Retention(CLASS) @Target(TYPE_USE) public @interface Weak
This class provides methods to annotate fields with weak reference. Such fields can be recycled by the RC/GC.
After the fields are recycled, null will be returned when they are read. This allows the fields to be
properly processed by the code logic.
Take entrySet of HashMap as an example. The inner class and outer class objects form a
circular reference. Therefore, you cannot use UnownedOuter to annotate the inner class because
entrySet can be passed as Set to the outside of HashMap. In this case, you can annotate
entrySet as a weak reference. If entrySet is recycled, you can use the entrySet() method
to return a stateless entrySet object.
Please refer the following sample code:
public class HashMap<K,V> extends AbstractMap<K,V>
implements Map<K,V>, Cloneable, Serializable {
...;
@Weak
transient Set<Map.Entry<K,V>> entrySet;
...;
public Set<Map.Entry<K,V>> entrySet() {
Set<Map.Entry<K,V>> es;
return (es = entrySet) == null ? (entrySet = new EntrySet()) : es;
}
}