Open mernst opened 6 years ago
Can @MustOverride be made conditional, i.e. require an override only if the type parameter is not @Nullable?
null
is often used as a marker for "not yet lazily initialized", which is a pattern that I found particularly useful in ThreadLocal
s.
Background:
I believe that Optional
is not solving any problem that the nullness checker does not solve but in a less clunky fashion (YMMV).
Having to override initialValue
just to return null
for @Nullable T
would add clunkiness.
Hmm... maybe provide a NullableThreadLocal
?
That might be easier to understand for programmers than introducing a conditional @MustOverride. Maybe.
Add a
@MustOverride
method annotation that requires that when a class is subclassed, the method must be overridden. This annotation indicates that the method implementation is broken. It is necessary when the underlying code cannot be fixed, such as when the code is in a library or Java is insufficiently expressive.An example is:
If a client creates a subclass that does not override
initialValue
, thenget
may returnnull
rather than returningT
:Perhaps the method doesn't have to be overridden if the type argument is the top type:
Perhaps
@MustOverride
would take a string argument indicating the reason that the method must be overridden, to help users understand the requirement and when it is acceptable to suppress the warning.The
@MustOverride
annotation will solve one problem. Fully annotating the ThreadLocal class in the JDK requires fixing issue #1330 as well, so that the ThreadLocal constructor can only be invoked with a nullable type:In the end,
ThreadLocal.java
will be annotated as:There is a test case in file
checker/tests/nullness/ThreadLocalTest2.java
.