Details about the known misuse from the MUBench dataset.
| Description: | The covariant version of equals() does not override the version in the Object class. |
| Fix Description: |
Overriding method should have the same syntax as of the Object class or use the override annotation. |
| Violation Types: |
- superfluous/condition/value_or_state
|
| In File: | Count.java |
| In Method: | main(String[]) |
| Code with Misuse: |
class Count {
public static void main(String[] args) {
Count c1 = new Count(42);
Count c2 = new Count(42);
Object obj = c2;
System.out.println(c1.equals(c2)); // prints "true"
System.out.println(c1.equals(obj)); // prints "false"
System.out.println(obj.equals(c1)); // prints "false"
}
}
|
| Code with Pattern(s): |
class CorrectedCount {
int value;
public CorrectedCount(int value) {
this.value = value;
}
public boolean equals(Object o) {
if (o == this) return true;
if (!(o instanceof CorrectedCount)) {
return false;
}
CorrectedCount count = (CorrectedCount) o;
return this.value == count.value;
}
public int hashCode() {
int result = 17;
result = 31 * result + value;
return result;
}
public static void main(String[] args) {
CorrectedCount c1 = new CorrectedCount(42);
CorrectedCount c2 = new CorrectedCount(42);
Object obj = c2;
System.out.println(c1.equals(c2)); // prints "true"
System.out.println(c1.equals(obj)); // prints "false"
System.out.println(obj.equals(c1)); // prints "false"
}
}
|