JavaScript is not currently enabled, but is required for full CodeSonar manual search and browse functionality.
If you are viewing this file in your hub's Web GUI, enable JavaScript in your browser: you will also need it for GUI functionality.
If you opened this file directly from disk, your browser may be directly suppressing JavaScript functionality: certain browsers perform this suppression on local files (but not files delivered by web servers) for security reasons.
| CodeSonar® 9.2p0 | CONFIDENTIAL | CodeSecure Inc |
equals() is inherited but extra fields have been added.
Redefinitions of the equals()/hashCode() methods from java.lang.Object must be consistent in the sense that, for instance, if two objects are equals() then hashCode() must yield the same value on both. The validity of such a property is in general undecidable, but most incorrect definitions amount to simple cases, where one of the two methods is missing. However, it is often correct to redefine only one of those methods and the analyzer is aware of many such situations.
Inconsistent definitions of equals()/hashCode() induce unexpected behaviors when objects are put inside most Collection classes of the standard Java library.
| Class Name | Missing Equals Override (Java) | ||||||
|---|---|---|---|---|---|---|---|
| Significance | reliability | ||||||
| Mnemonic | JAVA.IDEF.NOEQUALS | ||||||
| Categories |
|
||||||
| Availability | Available for Java and Kotlin. |
||||||
| Enabling | Checks for this warning class are enabled by
default. To disable them, add the following WARNING_FILTER rule to the
project configuration file.
WARNING_FILTER += discard class="Missing Equals Override (Java)" |
// Base.java
public class Base {
public String b;
public Base(String b) {
this.b = b;
}
public boolean equals(Object other) {
if(other instanceof Base)
return b.equals(((Base) other).b);
return false;
}
}
// BPlus.java public class BPlus extends Base { public String in; public BPlus(String b, String in) { // "Missing Equals Override (Java)" warning issued here super(b); this.in = in; } }
The BPlus class extends Base class, then in addition to all the other implications of inheritance it also inherits the equals method of Base class. However, BPlus contains a in field that is not contained in Base. This means that the inherited equals does not consider in field and might be inconsistent. If in is relevant for the comparison, BPlus should overwrite the method as follows.
public class BPlus extends Base {
public String in;
public BPlus(String b, String in) {
super(b);
this.in = in;
}
public boolean equals(Object other) {
if( super.equals(other) && other instanceof BPlus)
return in.equals(((BPlus) other).in);
return false;
}
}
Make equals() consistent with hashCode(). In many cases, this just amounts to provide the missing definition for one of them.
The following configuration file parameters affect checks for this warning class.
To report problems with this documentation, please visit https://support.codesecure.com/.