This is really just a Java question, but it’s got me stumped, and internet searches are finding the same terms from other languages, so haven’t been helpful. Please let me know if there’s a better, more generic place to ask.
I’m working on a binding. The code style guidelines say all classes need to be defined with @NonNullByDefault
. I’m porting some old code which didn’t do this, and adding it adds a bunch of warnings. Most of them are pretty manageable, though, and I’m able to work through them.
One of the ones that’s left me stumped is that when the code uses a HashMap, all the methods on the HashMap also get defined as @NonNull
, such as HashMap’s get()
method.
This is wrong.
Get() is explicitly documented to return null when an item isn’t found, and all the code that exists in what I’m porting that carefully checks the return of get() for null is flagged as dead code, because get() can’t return null.
Since HashMap is defined in java.util, I can’t easily mark it up with @Nullable.
How do I prevent this warning correctly? I’d rather not just disable it for the whole method, as it’s valuable, but I haven’t figured out a way to annotate a specific spot to say, “No, really, this is (or is not) nullable right here, trust me, I checked.”
Any guidance? Pointers to docs I missed?
Do I need to implement a HashMap that’s marked Nullable somehow? Can I just override “get” to be nullable?