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

Summary: Unsafe usageof Optional<T> has no warnings
Product: java Reporter: skiwi
Component: HintsAssignee: Svata Dedic <sdedic>
Status: NEW ---    
Severity: normal CC: markiewb
Priority: P2    
Version: 8.0   
Hardware: PC   
OS: Windows 8 x64   
Issue Type: ENHANCEMENT Exception Reporter:

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.