Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: REM cost reform

...

Overriding the equals() method without overriding the hashCode() method can lead to unexpected results.

Rule

Severity

Likelihood

Detectable

RepairableRemediation Cost

Priority

Level

MET09-J

Low

Unlikely

Yes

NoHigh

P1P2

L3

Automated Detection

Automated detection of classes that override only one of equals() and hashcode() is straightforward. Sound static determination that the implementations of equals() and hashcode() are mutually consistent is not feasible in the general case, although heuristic techniques may be useful.

CODSTAOIMImplemented
ToolVersionCheckerDescription
CodeSonar
Include Page
CodeSonar_V
CodeSonar_V
FB

JAVA.

CORRECTNESS.HE_SIGNATURE_DECLARES_HASHING_OF_UNHASHABLE_CLASS
FB.CORRECTNESS.HE_USE_OF_UNHASHABLE_CLASS

IDEF.EQUALSNOHC
JAVA.IDEF.HCNOEQUALS

Defines equals but not hashCode
Defines hashCode but not equals

Klocwork

Include Page
Klocwork_V
Klocwork_V

EHC.EQ
EHC.HASH
Signature declares use of unhashable class in hashed construct
Use of class without a hashCode() method in a hashed data structure

Parasoft Jtest
Include Page
Parasoft_V
Parasoft_V
CERT.MET09.OVERRIDEOverride 'Object.hashCode()' when you override 'Object.equals()' and vice versa
PVS-Studio

Include Page
PVS-Studio_V
PVS-Studio_V

V6049
SonarQube
Include Page
SonarQube_V
SonarQube_V

S1206

"equals(Object obj)" and "hashCode()" should be overridden in pairs

Related Guidelines

MITRE CWE

CWE-581, Object Model Violation: Just One of equals and hashcode Defined

...