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 255763 - Menu item 'Create equals() and hashCode()' methods doesn't work correctly, produces a useless hashCode()
Summary: Menu item 'Create equals() and hashCode()' methods doesn't work correctly, pr...
Status: RESOLVED WORKSFORME
Alias: None
Product: java
Classification: Unclassified
Component: Hints (show other bugs)
Version: 8.0.2
Hardware: Macintosh Mac OS X
: P3 normal (vote)
Assignee: Svata Dedic
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2015-10-07 09:52 UTC by achirizzi
Modified: 2016-05-26 08:37 UTC (History)
0 users

See Also:
Issue Type: DEFECT
Exception Reporter:


Attachments
IDE log (176.65 KB, text/plain)
2015-10-07 09:52 UTC, achirizzi
Details
Java file to show the hashCode method creation bug (11.43 KB, application/octet-stream)
2015-10-07 09:54 UTC, achirizzi
Details
Screenshot of the hashcode+equals tool (226.12 KB, image/jpeg)
2016-01-29 11:17 UTC, Svata Dedic
Details

Note You need to log in before you can comment on or make changes to this bug.
Description achirizzi 2015-10-07 09:52:26 UTC
Product Version = NetBeans IDE 8.0.2 (Build 201411181905)
Operating System = Mac OS X version 10.10.5 running on x86_64
Java; VM; Vendor = 1.8.0_45
Runtime = Java HotSpot(TM) 64-Bit Server VM 25.45-b02

I will attach a class example which reproduces the problem. Take the attached file, go at the bottom, where the IndexToReplicate class is, press CTRL-I when in the compareTo() method and choose the "equals() and hashCode()..." methods.
Check the four class members and create the new methods.

It will produce this hashCode method:

    @Override
    public int hashCode() {
      int hash = 7;
      return hash;
    }

which is useless.

Now delete that method and repeat the creation, press CTRL-I and choose "hashCode()..."

It will now produce a better method:

    @Override
    public int hashCode() {
      int hash = 7;
      hash = 17 * hash + this.indexNumber;
      hash = 17 * hash + Objects.hashCode(this.projectName);
      hash = 17 * hash + Objects.hashCode(this.lastUpdatedOn);
      hash = 17 * hash + (int) (this.currentTime ^ (this.currentTime >>> 32));
      return hash;
    }
Comment 1 achirizzi 2015-10-07 09:52:29 UTC
Created attachment 156580 [details]
IDE log
Comment 2 achirizzi 2015-10-07 09:54:38 UTC
Created attachment 156581 [details]
Java file to show the hashCode method creation bug

Use this file to reproduce the bug
Comment 3 Jiri Kovalsky 2015-10-08 13:16:46 UTC
Reassigned for further evaluation.
Comment 4 Svata Dedic 2016-01-29 07:18:26 UTC
Did you check the members in BOTH equals() and hashCode() choosers during the initial equals + hashCode chooser ?

The chooser for hashCode() only generator will respect choices made for existing equals() - the same when generating equals() when hashCode already exists.
Comment 5 achirizzi 2016-01-29 10:07:02 UTC
Sorry I don't get what you mean.
If you use the file I attached, and go to the bottom of that file, you should be able to reproduce the problem.

Search for the following comment:

// GO HERE AND PRESS CTRL-I to produce equals() and hashCode(). Check all the 4 members.
Comment 6 Svata Dedic 2016-01-29 11:16:46 UTC
OK, some misunderstandings may be there. 

1/ I admit I didn't use CTRL-I: that shortcut stands for either Copy or QuickSearch in the standard keymap, which seems rather unrelated to this topic). But used Alt-Ins to bring up "Generate" popup menu - also reachable from Source / Insert code. Check if the CTRL-I keystroke does the same in your setup. If not, please go to Tools - Options - Keymaps, search for CTRL-I keystroke and note the attached action name in the bug report. Though unlikely, there may be different hashCode() generators. 

2/ In absence of both equals and hashCode(), the generator offers to create both equals and hashCode. See the attached screenshot -- if I check items in BOTH columns, I'll get a properly generated hashcode:

        @Override
        public int hashCode() {
            int hash = 5;
            hash = 53 * hash + this.indexNumber;
            hash = 53 * hash + Objects.hashCode(this.projectName);
            hash = 53 * hash + Objects.hashCode(this.lastUpdatedOn);
            hash = 53 * hash + (int) (this.currentTime ^ (this.currentTime >>> 32));
            return hash;
        }

When the right column is left at its default (none checked), the generated hashCode looks as in your report.
Comment 7 Svata Dedic 2016-01-29 11:17:17 UTC
Created attachment 158284 [details]
Screenshot of the hashcode+equals tool
Comment 8 achirizzi 2016-01-29 11:35:47 UTC
I have redone the steps I had described a couple of months ago, and now it doesn't show any problems while creating the hashCode method, even using CTRL-I.
Of course I checked all or some of the members but I am sure I was not getting the right methods 2 months ago.

Now it works well, with my example. I am still on NetBeans IDE 8.0.2 (Build 201411181905), some updates were installed, but I have not taken note of what I have updated so far. Or it can depend on some other reasons. If I hit that again I will produce a more thorough documentation.

Thanks,

-Antonio
Comment 9 Svata Dedic 2016-01-29 11:39:29 UTC
OK, is the dialog on the screenshot the same as in your IDE (to be sure I am looking at the same feature) ? I used also 8.0.2 release (NetBeans IDE 8.0.2 (Build 201411181905)) for testing - the same result.
Comment 10 achirizzi 2016-01-29 11:44:41 UTC
Yes, it's the same dialog.
Comment 11 Svata Dedic 2016-05-26 08:37:49 UTC
OK, closing as works for me then.