False positive on squid:S2160 override equals

java

(Jason Horton) #1

Hello!

I believe I found a bug causing overridden equals methods not being recognized by sonar, when the overridden equals is declared as final.

  • Versions used:
    SonarQube: 7.2 (build 13530)
    maven scanner: 3.4.1.1168

  • Error observed:
    SonarQube says to ‘Override the “equals” method in this class.’, but the method is overridden

  • Code example:

    import java.util.Objects;
    public abstract class A {
        private int field;
        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (!(o instanceof A)) return false;
            A a = (A)o;
            return field == a.field;
        }
    }
    
    public class B extends A {
        private String name;
        @Override
        public final boolean equals(Object o) {
            if (this == o) return true;
            if (!(o instanceof B)) return false;
            if (!super.equals(o)) return false;
            B b = (B)o;
            return Objects.equals(name, b.name);
        }
    }
    
  • Screenshot of the message:

Thanks!


(Nicolas Peru) #2

This is indeed a false positive in that case : https://jira.sonarsource.com/browse/SONARJAVA-2818

Thanks for the reproducer, it is always appreciated.