There are a few valid use cases where `new WeakReference<>(null)` makes some sense. So, we shouldn't forbid nulls.
`new WeakReference<>(null, queue)` makes less sense but I bet someone is using this too for some reason. My initial message was not about changing the behavior but only about changing the JavaDoc. I just want to add a clear specification of what happens to references with null referents. 2018-07-11 6:26 GMT+07:00 mandy chung <[email protected]>: > > > On 7/10/18 4:04 PM, Éamonn McManus wrote: > >> Do you know any other example? >>> >> >> Here's an example from the JDK itself: >> http://hg.openjdk.java.net/jdk/jdk11/file/2a89e80301b1/src/ >> java.desktop/share/classes/sun/font/Type1Font.java#l101 >> > > Thanks Eamonn. > > I also found a few other similar usages in JDK to initialize > with an "empty" WeakReference field that may be updated with > a weak ref with a referent at runtime so that the code can > always assume this field is non-null. > > Due to the current usage and compatibility risk, not worth > to enforce non-null referent. > > Mandy > > > On Tue, 10 Jul 2018 at 10:11, mandy chung <[email protected]> wrote: >> >>> >>> >>> >>> On 7/9/18 11:59 PM, Peter Levart wrote: >>> >>> >>>> There are situations where being able to create a XxxReference without a >>>> referent is useful. For example, imagine a doubly-linked list of >>>> WeakReference subclasses - a special instance without a referent can be >>>> used as the head of such list that never goes away and simplifies >>>> insertion/deletion logic, etc... >>>> >>>> JDK's java.lang.ref.Cleaner API uses such scheme in its implementation >>>> (see jdk.internal.ref.CleanerImpl.[Phantom|Weak|Soft]CleanableRef >>>> no-arg >>>> constructors). >>>> >>> >>> Thanks for pointing this out. It's used as a convenience and it's >>> not hard to handle insertion/deletion logic though. Another >>> alternative may be to create an object as the strongly reachable >>> referent. I wonder if any existing code depends on null referent >>> like the cleaner case and the compatibility risk. Do you know >>> any other example? >>> >>> Mandy >>> >>
