Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Map w @NonNull Value Type and @Nullable get() method result?

I have a method returning a custom Map implementation, whose entries consist entirely of non-null keys and values, so I would like to add type annotations in order to indicate that clients may iterate over the Map.Entry's without having to check them for null values: Map<@NonNull String,@NonNull String>

The problem is that the Map.get method API specifies that null be returned for any attempt to retrieve a value for a key which isn't present in the Map, and annotating my get method implementation to return a @Nullable String generates a compiler warning that the return type is then incompatible with the @NonNull return specified by the Map.

I understand that a Map.get API created today might perhaps return a java.util.Optional result or throw a NoSuchElementException, but, being beholden to the existing Collections API, is it possible to remain compliant with the get method specification and also specify that my Map contains only @NonNull values?

Much Thanks.

like image 706
Chris Hubick Avatar asked Apr 19 '26 14:04

Chris Hubick


2 Answers

Unfortunately, the Map API is not really compatible with null annotations. Map.get returns the generic type V, even when you define V to be @NonNull, which then violates the API since null must be an allowable return value.

This is a known limitation of null annotations, and will probably only be resolved when nullity profiles for libraries are implemented. Until then, the only workarounds are to check with Map.containsKey before getting a value instead of checking the value afterwards for null, or just avoid using @NonNull on map value types.

like image 184
Sean Van Gorder Avatar answered Apr 22 '26 04:04

Sean Van Gorder


You seem to be referring to Eclipse's implementation of nullity checking. You are right that it does not reason precisely about calls to Map.get.

If more precise reasoning about calls to Map.get is important to you, you might want to consider using a different nullity checker. For example, the Nullness Checker that is built on the Checker Framework handles your case. It treats the return value of Map.get as non-null if the key is in the map and all the elements of the map are non-null.

like image 28
mernst Avatar answered Apr 22 '26 03:04

mernst



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!