1

If you know EqualsBuilder from apache commons lang you know that you can use it for implementing equals methods with reflection. It seems to work well but since I am using third party software I need to change its behavior. I need a generic comparator which checks all attributes and it can not be assumed that attribut-objects have an implemented equals method, but EqualsBuilder assumes it.

In detail I want to change public EqualsBuilder append(Object lhs, Object rhs) so that instead of calling isEquals = lhs.equals(rhs) EqualsBuilder should be used for this object too. Something like isEquals = EqualsBuilder.reflectionEquals(lhs, rhs, false);

Overriding the method is possible but it will not be called because EqualsBuilder creates an object of its own. Therefore I need to tell EqualBuilder to use my RecursiveEquilbuilder object.

Any ideas?

Mureinik
  • 297,002
  • 52
  • 306
  • 350

1 Answers1

0

I know it's a bit old, but I hope it helps.

I run into the same problem that you, and I found that the most voted answer (not the one picked by the author) of this similar question, is the most suitable solution for you.

Basically, it consist on using the library called Unitils.

This is the use:

User user1 = new User(1, "John", "Doe");
User user2 = new User(1, "John", "Doe");
assertReflectionEquals(user1, user2);

Which will pass even if the class User doesn't implement equals(). You can see more examples and a really cool assert called assertLenientEquals in their tutorial.

Community
  • 1
  • 1
lopezvit
  • 384
  • 4
  • 12