Bug 413311 - Some classes missing hashCode()
Summary: Some classes missing hashCode()
Status: NEW
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 4.3   Edit
Hardware: All All
: P3 enhancement (vote)
Target Milestone: ---   Edit
Assignee: JDT-Core-Inbox CLA
QA Contact:
URL:
Whiteboard:
Keywords: performance
Depends on:
Blocks:
 
Reported: 2013-07-19 08:54 EDT by Mickael Istria CLA
Modified: 2018-03-05 05:17 EST (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Mickael Istria CLA 2013-07-19 08:54:27 EDT
Using this reports for CheckStyle in Sonar https://dev.eclipse.org/sonar/drilldown/violations/org.eclipse.platform:platform-aggregator?&rule=checkstyle%3Acom.puppycrawl.tools.checkstyle.checks.coding.EqualsHashCodeCheck&rule_sev=CRITICAL&severity=CRITICAL , we can see that some classes are missing a hashCode() method matching the behaviour of the equals() one.

Depending on the usage of the class (inside JDT code, or even outside of JDT for classes that are exported), it can cause some performance issues.

Indentified classes with this bad small
* org.eclipse.jdt.apt.core.internal.type.PrimitiveTypeImpl
* org.eclipse.jdt.core.tests.builder.Problem
* org.eclipse.jdt.internal.core.builder.ClasspathMultiDirectory
* org.eclipse.jdt.internal.compiler.apt.util.ArchiveFileObject
* org.eclipse.jdt.internal.compiler.tool.ArchiveFileObject
Comment 1 Mickael Istria CLA 2013-07-19 09:52:45 EDT
Suggested fix: https://git.eclipse.org/r/14700
Comment 2 Mickael Istria CLA 2013-08-28 12:08:21 EDT
As this issue can cause bad performance, and a patch is provided, I think this can be targeted for next milestone.
Comment 3 Stephan Herrmann CLA 2013-08-28 18:43:28 EDT
Not every class that has an equals method is also stored in a hashed collection.

So I just made a test using ClasspathMultiDirectory as my example:
I added a stupid hashCode() method and ran all relevant tests (RunModelTests - 7255 tests) in the debugger with a breakpoint in this hashCode() method: result: 0 hits.

Also, none of the listed classes are intended for use outside JDT.

Do you have a candidate where a hashCode() method actually makes a difference?
Comment 4 Mickael Istria CLA 2013-08-29 03:32:29 EDT
This is just a generic Java rule that is IMO beyond and more important than JDT best practices. Maybe it's not used directly in JDT, and shouldn't be used out of JDT, but the fact is that there might be a current or future use-case for putting this into a HashMap or a HashSet, and in such case, not having a hashCode() is a bug.
Comment 5 Mickael Istria CLA 2018-03-05 05:17:23 EST
I've abandoned my patch which was partly covered by fixed of some other bugs surfacing later (such as Bug 521438).
But I think this is still all valid. It's a very bad practice to not implement hashCode when classes define an equals, and by ignoring this rule, JDT leaves a door open to performance problems later or in consumer code.