...
Do not violate any of these five conditions while overriding the equals
method.
...
Noncompliant Code Example
This non-compliant noncompliant code example violates the second condition in the contract (symmetry). This requirement means that if one object is equal to another then the other must also be equal to this one. Consider a CaseInsensitiveString
class that defines a String
and overrides the equals
method. The CaseInsensitiveString
knows about ordinary strings but the String
class has no idea about case-insensitive strings. ThusAs a result, s.equalsIgnoreCase(((CaseInsensitiveString)o).s)
returns true but s.equalsIgnoreCase((String)o)
always returns false.
Code Block | ||
---|---|---|
| ||
public final class CaseInsensitiveString { private String s; public CaseInsensitiveString(String s) { if (s == null) throw new NullPointerException(); this.s = s; } //This method violates asymmetry public boolean equals(Object o) { if (o instanceof CaseInsensitiveString) return s.equalsIgnoreCase(((CaseInsensitiveString)o).s); if (o instanceof String) return s.equalsIgnoreCase((String)o); return false; } public static void main(String[] args) { CaseInsensitiveString cis = new CaseInsensitiveString("Java"); String s = "java"; System.out.println(cis.equals(s)); //returns true System.out.println(s.equals(cis)); //returns false } } |
Compliant Solution
Do not try to inter-operate with String
from the equals
method. The new equals
method is highlighted in this compliant solution.
Code Block | ||
---|---|---|
| ||
public final class CaseInsensitiveString { private String s; public CaseInsensitiveString(String s) { if (s == null) throw new NullPointerException(); this.s = s; } public boolean equals(Object o) { return o instanceof CaseInsensitiveString && ((CaseInsensitiveString)o).s.equalsIgnoreCase(s); } public static void main(String[] args) { CaseInsensitiveString cis = new CaseInsensitiveString("Java"); String s = "java"; System.out.println(cis.equals(s)); //returns false now System.out.println(s.equals(cis)); //returns false now } } |
...
Noncompliant Code Example
This non-compliant noncompliant example violates transitivity though it follows the symmetry condition. This is because the first two statements print true
while the third prints false
. A practical implementation issue is intermingling of java.sql.Timestamp
and java.util.Date
classes. There is a disclaimer about the erratic behavior in the documentation for the Timestamp
class.
Code Block | ||
---|---|---|
| ||
public class Card { private final int number; public Card(int number) { this.number = number; } public boolean equals(Object o) { if (!(o instanceof Card)) return false; Card c = (Card)o; return c.number == number; } } class XCard extends Card { private String type; public XCard(int number, String type) { super(number); this.type = type; } public boolean equals(Object o) { if (!(o instanceof Card)) return false; //normal Card, do not compare type if (!(o instanceof XCard)) return o.equals(this); //It is an XCard, compare type as well XCard xc = (XCard)o; return super.equals(o) && xc.type == type; } public static void main(String[] args) { XCard p1 = new XCard(1, "type1"); Card p2 = new Card(1); XCard p3 = new XCard(1, "type2"); System.out.println(p1.equals(p2)); //returns true System.out.println(p2.equals(p3)); //returns true System.out.println(p1.equals(p3)); //returns false, violating transitivity } } |
Compliant Solution
"There is simply no way to extend an instantiable class and add an aspect while preserving the equals contract." This implies that composition must be preferred over inheritance in this case. This is done by giving the XCard
class a private card
field and providing a a public viewCard
method. Effective Java Programming Language Guide
...
TODO: Add condition for hashcode
Risk Assessment
TODO
Rule | Severity | Likelihood | Remediation Cost | Priority | Level |
---|---|---|---|---|---|
MET30-J | ?? | ?? | ?? | P?? | L?? |
Automated Detection
TODO
Related Vulnerabilities
Search for vulnerabilities resulting from the violation of this rule on the CERT website.
References
EJPL Item 7, Obey the general contract when overriding equals
Java Documentation http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Object.html#equals(java.lang.Object)