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 210462

Summary: Inspect & Transform cannot apply "strings using ==" hint in desired way
Product: java Reporter: Jesse Glick <jglick>
Component: HintsAssignee: Svata Dedic <sdedic>
Status: NEW ---    
Severity: normal    
Priority: P3    
Version: 7.2   
Hardware: PC   
OS: Linux   
Issue Type: ENHANCEMENT Exception Reporter:

Description Jesse Glick 2012-03-30 19:36:38 UTC
If (in -source 6 code) you have

private static final String K1 = ...;
private static final String K2 = ...;
...
String k = ...; // known to be @NonNull
if (k == K1) {...}
else if (k == K2) {...}
..

and you want to switch to using equals() without a null check using Inspect & Transform, you cannot: the dialog warns that there are multiple fixes and it will just pick the first one. (Which is particularly stupid in this case because the suggested code compares string _constants_ to null!)

Disabling the option "Check for null using ternary conditional operator" in hint prefs does not help.
Comment 1 Jesse Glick 2012-03-30 19:37:17 UTC
Workaround is to manually accept second hint fix on each affected line.
Comment 2 Svata Dedic 2013-09-05 11:48:16 UTC
Could be improved, but only in limited cases, i.e. one side of the comparison is either a literal, or a final reference variable with an initializer. Definite assingment rules could be added, but I am not sure it cover enough cases to weight out the complexity.

Could also work with @NonNull etc annotations (configurable in some other hint)