2
donuts
4y

How should you override Equals in Java?

We have model classes with lots of fields and the we override the details equals to compare all the fields. I guess like doing a deep comparison.

And in all these classes SONAR is complaining about lots of ifs, complexity, etc.

And it's killing the analysis time... Old issue never fixed because whoever setup sonar was too incompetent to bother asking, researching, or fixing...

Is there a better way to override the equals to get the same result but without triggering SONAR issues?

Pretty sure this is a solved problem. And well if the top of my head, is just create a Util method that uses reflection like
Boolean equals( Object a, b, Class class)
foreach (String f: class, getFields()) {if !compare(class.getField(f,a),class.getField(f,b)) return false; }

return true

Comments
Add Comment