[Checkers] Nullness checker

Adam Warski adam at warski.org
Mon Mar 23 12:02:51 EDT 2009


Hello,

> AnnotationValueVisitor needs to be declared as:
> interface AnnotationValueVisitor<R extends @Nullable Object, P  
> extends @Nullable Object> { ... }
Ok, thanks :)
Why can't I write @Nullable R, but instead there's R extends @Nullable  
Object? Is it because of current implementation, or some deeper  
requirement?
Also, should things like this go to jdk/nullness/sdk?


>> Doesn't that prohibit type parameters that allow null values at all?
>> After all, @Nullable Object doesn't extend @NonNull Object, it's the
>> other way round. And why do you write this at all, if the default
>> qualifier is @NonNull? Doesn't this apply also to type parameters? I
>> could be of course missing some obvious variance argument on why this
>> is correct ;)
>> (another by the way: Map.get should accept a @Nullable parameter)
>
> True.  There are two things here:
> 1. We decided to insert the @NonNull annotation to explicitly state  
> and emphasize the annotation, despite it being the default.   
> Especially here, in the case of the Collection API, figuring out the  
> parameter bound doesn't seem so obvious.  For the reason mentioned  
> below.
>
> 2. The Collection API offers optional specification that allow the  
> subclasses of the interface to be more strict that the interface  
> itself (e.g. Map.get() and Map.put() optionally may throw  
> NullPointerException if any of the arguments is a null).  It is only  
> some classes that actually accept null (e.g. the most popular ones,  
> like ArrayList, LinkedList, and HashMap), but a lot of other ones  
> don't permit nulls (sorted queues PriorityQueue, Hashtable, most  
> concurrent collections).  We decided to have the interfaces be as  
> strict as possible (i.e. marked with @NonNull) and have the concrete  
> implementations permit Nullable elements (check declaration of  
> ArrayList).

I see - you're right of course. I declared my map as:

Map<? extends /*@Nullable*/ Object, ? extends /*@Nullable*/ Object>  
testMap = new HashMap<Object, Object>();

and it works.

So it seems there's no way for a map to accept @Nullable arguments  
without using the implementation as the type of the object. That's not  
very nice, but the contract for the map says that an NPE can be  
thrown ... ah well, unfortunately we can't fix java.util.Map ;)

Adam



More information about the checkers mailing list