Community
Participate
Working Groups
When declaring a Map with @NonNull-keys, eclipse assumes that map.get(..) will always return a @NonNull-value: public void methodA() { @NonNull Map<@NonNull Object, @NonNull Object> map = new HashMap<>(); @NonNull Object object = map.get(""); } get(..) will return a null-value here because there's no value associated with the key, so the map.get("")-line should show an error.
This works as designed: By declaring 'Map<@NonNull Object,@NonNull Object>' you are instantiating both type parameters with nonnull types. Simply insert "K=@NonNull Object" in the signature of get() and you will see, why the compiler sees a nonnull return. The result in comment 0 is obviously wrong, but this is not an error in JDT's analysis, but caused by the fact that java.util.Map is insufficiently specified. Since Mars you can fill this gap yourself, please see: http://help.eclipse.org/mars/topic/org.eclipse.jdt.doc.user/tasks/task-using_external_null_annotations.htm?cp=1_3_9_2 In fact, method Map.get() is the very example used in the N&N, see https://www.eclipse.org/eclipse/news/4.5/jdt.php#annotate-command Marking this bug as a duplicate of the bug which introduced external annotations. *** This bug has been marked as a duplicate of bug 331651 ***
(In reply to Stephan Herrmann from comment #1) > This works as designed: By declaring 'Map<@NonNull Object,@NonNull Object>' > you are instantiating both type parameters with nonnull types. Simply insert > "K=@NonNull Object" in the signature of get() and you will see, why the > compiler sees a nonnull return. should say: "V=@NonNull Object"