Bug 470953 - [null]Wrong NonNull interference for Map
Summary: [null]Wrong NonNull interference for Map
Status: RESOLVED DUPLICATE of bug 331651
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 4.5   Edit
Hardware: PC Windows 7
: P3 normal (vote)
Target Milestone: 4.5 M6   Edit
Assignee: Stephan Herrmann CLA
QA Contact:
URL:
Whiteboard: To be verified for 4.6 M2
Keywords:
Depends on:
Blocks:
 
Reported: 2015-06-25 04:31 EDT by Peter-René Jeschke CLA
Modified: 2015-06-25 05:22 EDT (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Peter-René Jeschke CLA 2015-06-25 04:31:28 EDT
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.
Comment 1 Stephan Herrmann CLA 2015-06-25 05:21:44 EDT
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 ***
Comment 2 Stephan Herrmann CLA 2015-06-25 05:22:47 EDT
(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"