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 158042 - "Comparing Strings using == or !=" hint should offer a Fix
Summary: "Comparing Strings using == or !=" hint should offer a Fix
Status: RESOLVED FIXED
Alias: None
Product: java
Classification: Unclassified
Component: Hints (show other bugs)
Version: 6.x
Hardware: All All
: P3 blocker (vote)
Assignee: Max Sauer
URL:
Keywords:
Depends on:
Blocks: 178441
  Show dependency tree
 
Reported: 2009-02-05 21:08 UTC by malfunction84
Modified: 2009-12-10 12:03 UTC (History)
0 users

See Also:
Issue Type: ENHANCEMENT
Exception Reporter:


Attachments
Proposed patch (18.54 KB, patch)
2009-05-24 05:17 UTC, malfunction84
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description malfunction84 2009-02-05 21:08:26 UTC
It would be nice if the hint which warns of comparing Strings using == or != provided a fix which changed the expression
to use the equals() method instead.

Original expression:
  (str1 == str2)
Fix:
  (str1.equals(str2))

A truly equivalent fix would actually ensure that the new expression always evaluates to the same value as the original
(and avoids the potential NPE).  Such a Fix could incorporate a ternary null check.  For those who dislike ternary
operators, a third Fix could be offered.

Original expression:
  (str1 == str2)
Fix 1 (no null check):
  (str1.equals(str2))
Fix 2 (ternary null check):
  ((str1 == null) ? (str2 == null) : str1.equals(str2))
Fix 3 (non-ternary null check):
  (((str1 == null) && (str2 == null)) || ((str1 != null) && str1.equals(str2)))

Similarly, != would be handled thusly:

Original expression:
  (str1 != str2)
Fix 1 (no null check):
  (!str1.equals(str2))
Fix 2 (ternary null check):
  ((str1 == null) ? (str2 != null) : !str1.equals(str2))
Fix 3 (non-ternary null check):
  (((str1 == null) && (str2 != null)) || ((str1 != null) && !str1.equals(str2)))
Comment 1 malfunction84 2009-02-05 21:10:26 UTC
I've already started on a patch.  Am I allowed to assign this to myself?
Comment 2 Max Sauer 2009-02-06 08:18:47 UTC
Yes, please go ahead. Please introduce some tests and options for this functionality. Thanks.
Comment 3 malfunction84 2009-05-24 05:17:55 UTC
Created attachment 82701 [details]
Proposed patch
Comment 4 malfunction84 2009-05-24 05:22:06 UTC
I've implemented the fix and introduced an option to control whether the fix uses a ternary condition for the null check
(default is false).

I don't know how to set up unit tests for it, though.
Comment 5 Max Sauer 2009-05-25 14:32:27 UTC
I've integrated the fix, made some minor adjustments, changed the default to ternary (to be aligned with 'generate equals()') and added some tests -- you 
can have a look in the changeset mentioned below in case you're interested how to set them up. Marking as FIXED. Thank you very much for your 
contribution.
---
http://hg.netbeans.org/jet-main/rev/12e027c37200
Comment 6 malfunction84 2009-05-25 18:11:39 UTC
Thanks!
Comment 7 Quality Engineering 2009-05-26 07:28:48 UTC
Integrated into 'main-golden', will be available in build *200905260201* on http://bits.netbeans.org/dev/nightly/ (upload may still be in progress)
Changeset: http://hg.netbeans.org/main-golden/rev/12e027c37200
User: Max Sauer <msauer@netbeans.org>
Log: #158042: "Comparing Strings using == or !=" hint should offer a Fix