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 252456

Summary: Unable to infer method return type from ternary operator
Product: java Reporter: cezariusz <cezariusz>
Component: CompilerAssignee: Dusan Balek <dbalek>
Status: RESOLVED FIXED    
Severity: normal CC: cezariusz
Priority: P3    
Version: 8.1   
Hardware: PC   
OS: Windows 7   
Issue Type: DEFECT Exception Reporter:
Attachments: Invalid hint
Sample project
Userdir and cache

Description cezariusz 2015-05-18 11:47:47 UTC
Product Version: NetBeans IDE Dev (Build 201505160001)
Java: 1.8.0_45; Java HotSpot(TM) 64-Bit Server VM 25.45-b02
Runtime: Java(TM) SE Runtime Environment 1.8.0_45-b14
System: Windows 7 version 6.1 running on amd64; UTF-8; pl_PL (nb)

Hint to change method type wrongly suggests "void" type, when a ternary operator is used for return.

Testcase:
    public void getValue(boolean which) {
        return which ? "abc" : "def";
    }

Call hint on the line with return, and you will get the hint "Change method return type to void" instead of "Change method return type to String".
Comment 1 Svata Dedic 2015-05-22 12:15:39 UTC
Already works in dev version
Comment 2 cezariusz 2015-05-22 12:58:59 UTC
Product Version: NetBeans IDE Dev (Build 201505220001)
Java: 1.8.0_45; Java HotSpot(TM) 64-Bit Server VM 25.45-b02
Runtime: Java(TM) SE Runtime Environment 1.8.0_45-b14
System: Windows 7 version 6.1 running on amd64; UTF-8; pl_PL (nb)

No it doesn't.
Comment 3 cezariusz 2015-05-22 13:00:44 UTC
Created attachment 153818 [details]
Invalid hint
Comment 4 Svata Dedic 2015-05-22 14:16:48 UTC
Well, I could attach the same screenshot where the hint says "Change ... to String". Built from fresh sources 10 minutes ago:

Product Version: NetBeans IDE Dev (Build 20150522-2bbdcb3f4f50)
Java: 1.8.0_20; Java HotSpot(TM) 64-Bit Server VM 25.20-b23
Runtime: Java(TM) SE Runtime Environment 1.8.0_20-b26
System: Linux version 3.16.0-25-generic running on amd64; UTF-8; pl_PL (nb)

(I doubt that java micro-version would change anything - NB uses its own bundled javac impl for analysis).

Anyway - could you please isolate the issue into a separate project, zip it and attach here ? Also please pack your $nbuser/config directory so I can recreate similar environment for testing. Thanks.
Comment 5 cezariusz 2015-05-22 14:32:44 UTC
Created attachment 153820 [details]
Sample project
Comment 6 cezariusz 2015-05-22 14:38:36 UTC
Created attachment 153821 [details]
Userdir and cache

Reproduced on fresh userdir and cache.
Comment 7 Svata Dedic 2015-08-07 18:57:07 UTC
Reproduced. Reassigning to Compiler for evaluation: Please use the sample project, run on JDK8 or configure JDK8 for the project. At all times, the TypeMirror which javac returns for the conditional expression is erroneous.

When the source level is set to JDK8, javac API Trees.getOriginalType() returns `void' for the erroneous type. If the source level is JDK7, the getOriginalType() reports j.l.String

Please check whether this difference is intentional / desirable. Suggestions how to compensate in java hints module are welcome. Thanks.
Comment 9 Quality Engineering 2015-08-11 01:14:51 UTC
Integrated into 'main-silver', will be available in build *201508110002* on http://bits.netbeans.org/dev/nightly/ (upload may still be in progress)

Changeset: http://hg.netbeans.org/main-silver/rev/47043236be58
User: Dusan Balek <dbalek@netbeans.org>
Log: Issues #252456 and #253922 fixed.