1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
|
Object.equals() contract states that "For any non-null reference value x,
x.equals(null) should return false". Thus all classes implementing equals()
function should gracefully handle null as an argument and return false.
Unfortunately, many classes throw NullPointerException when null is passed to
equals(). For example:
```java
@Override
public boolean equals(Object obj) {
if (!getClass().equals(obj.getClass())) { // NPE dereferencing obj
return false;
}
// … more member equality checking …
return true;
}
```
EqualsBrokenForNull check modifies these equals() methods to add a null-check to
bring them into compliance for the equals() contract and avoid
NullPointerExceptions.
```java
@Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (!getClass().equals(obj.getClass())) {
return false;
}
// … more member equality checking …
return true;
}
```
|