Bug 481919 - [compiler][null] @NonNullByDefault(TYPE_ARGUMENT) leads to @NonNull for Map.get(someValue)
Summary: [compiler][null] @NonNullByDefault(TYPE_ARGUMENT) leads to @NonNull for Map.g...
Status: CLOSED DUPLICATE of bug 331651
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 4.5.1   Edit
Hardware: All All
: P3 normal (vote)
Target Milestone: 4.5 M6   Edit
Assignee: Stephan Herrmann CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2015-11-11 05:55 EST by Marcel Bruch CLA
Modified: 2015-11-11 14:03 EST (History)
0 users

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Marcel Bruch CLA 2015-11-11 05:55:34 EST
It seems too eager when @NonNullByDefault(TYPE_ARGUMENT) leads to the assumption that   map.get(someValue) will always return a non-null value.

My code:

private Map<String, BundleFrequency> bundles = Maps.newTreeMap();


@Nullable BundleFrequency frequency = bundles.get(key(bundle)); // this can be null (frequency == null) { // here the compiler complains frequency cannot be null
 frequency = new BundleFrequency(bundle.getName(), bundle.getVersion(), 0);
 bundles.put(key(bundle), frequency);
}

The same will likely happen for list.get(index) and other methods.
Comment 1 Stephan Herrmann CLA 2015-11-11 14:03:54 EST
Have a look at http://www.eclipse.org/eclipse/news/4.5/jdt.php#annotate-command

Your's was the exact example I used to motivate and explain external annotations.

Map.get() returns T and if T="@NonNull Something" than Map.get() declares to return "@NonNull Somthing". That's how generics work. That declaration is "wrong".

Then only way to fix is to fix the signature of Map.get(). You can do that since Mars.

*** This bug has been marked as a duplicate of bug 331651 ***