This Bugzilla instance is a read-only archive of historic NetBeans bug reports. To report a bug in NetBeans please follow the project's instructions for reporting issues.

Bug 241828 - Unsafe usageof Optional<T> has no warnings
Summary: Unsafe usageof Optional<T> has no warnings
Status: NEW
Alias: None
Product: java
Classification: Unclassified
Component: Hints (show other bugs)
Version: 8.0
Hardware: PC Windows 8 x64
: P2 normal (vote)
Assignee: Svata Dedic
Depends on:
Reported: 2014-02-13 14:58 UTC by skiwi
Modified: 2015-10-02 13:00 UTC (History)
1 user (show)

See Also:
Exception Reporter:


Note You need to log in before you can comment on or make changes to this bug.
Description skiwi 2014-02-13 14:58:06 UTC
In Java 8: There is the class Optional<T> that stores a variable that may or may not be present.

Assume you have: Optional<String> optional = someFunctionCall();

You do not know the content of it.
Currently you can call optional.get() without problems, which may result in a NoSuchElementException.

I suggest a check similar to null variable values, as the following: optional.isPresent() MUST be tested before you can call optional.get() to get rid of the warning.

There is an edge case where someone may know that the Optional<T> has a value, by calling for example: Optional<String> optional = Optional.of("abc")
However you could argue that in this case you should not be using an Optional at all, AND the warning can still be turned off per project.
Comment 1 Svata Dedic 2015-10-02 13:00:58 UTC
Wow. This is a thing similar to possible NPE and data flow analysis, yet different.  I need to think this through.