Java (java:S2160): If the parent class uses EqualsBuilder.reflectionEquals(this, obj), then the inheriting class does not need to override equals

In the below example we receive a violation that Child does not override equals(Object) because Child adds an additional member field.

However Parent implements a reflective equals implementation, therefor overriding the equals implementation is unnecessary,

import org.apache.commons.lang3.builder.EqualsBuilder;

class Parent {
  public boolean equals(Object obj) {
    return EqualsBuilder.reflectionEquals(this, obj);
  }
}

class Child extends Parent {
  Object additionalField;
  Child(Object additionalField) {
    this.additionalField = additionalField
  }
}

In practice, we receive a violation from Sonar that Child should override equals(Object), and we either need to supply @SuppressWarnings(java:S2160) at the Child class level, or mark the violation as a false positive within the Sonar web interface.

Hello @stevejagodzinski

I’m afraid there is not much we can do on the analyzer side currently: when the implementation is in another file, we only have access to the types, not the implementation. We can not know if it was implemented with reflectionEquals or not.

At this point, suppressing the issue somehow (mark it as FP, @SuppressWarnings, narrowing the focus, …) is the best I can suggest.

In addition, if you are consistently using reflectionEquals, you might want to consider removing the rule from the quality profile.

Hope it clarifies the situation.