Let's say, I implement a "Poller<E>" class, delegating the "poll()" method to a Queue<E>.
The Javadoc of "poll()" says: return "null", if Collection is empty. Otherwise return the element of e.g. Type E.
Now the Poller does something, if the element wasn't "null" and otherwise it does something. So the Result of "delegate.poll()" is stored in a local variable which for this reason can be "null", and hence would need the annotation "@Nullable".
I get "yellow" Code (i highlighted it in the snippets below in
RED) either on the "delegate.poll()" or on the "@Nullable" added to the local variable:
Here is the example code causing unexpected problems, due to the use of "@NullMarked":
import org.jspecify.annotations.NullMarked;
import org.jspecify.annotations.Nullable;
import java.util.concurrent.ConcurrentLinkedQueue;
@NullMarked
public class Test<E> {
private final ConcurrentLinkedQueue<E> delegate = new ConcurrentLinkedQueue<>();
public @Nullable E poll1() {
final @Nullable E poll = delegate.poll();
if (poll != null) {
// do something, since an element was removed
}
return poll;
}
public @Nullable E poll2() {
final E poll = delegate.poll();
if (poll != null) {
// do something, since an element was removed
}
return poll;
}
}
Seems like the "var" case somehow like mentioned in:
Issue 301I hope it will be considered to allow the "@Nullable" annotation on local variables for this case.