There are two similiar question on SO:
Is there a Java reflection utility to do a deep comparison of two objects?
Deep reflective compare equals
but, it is funny, none of them gives fully correct answer to the question.
What me and other questions's authors really want, is some method in some library which will just tell are given two objects equal or not:
boolean deepEquals(Object obj1, Object obj2)
i.e. without throwing any exception and so on.
apache
's EqualsBuilder
is not the way, because it doesn't deep compare.
Unitils
seems also be bad decision, because its method do not return true
or false
; it just throws an exception if comparison was failed. Of course, it could be used like this:
Difference difference = ReflectionComparatorFactory.createRefectionComparator(new ReflectionComparatorMode[]{ReflectionComparatorMode.LENIENT_ORDER, ReflectionComparatorMode.IGNORE_DEFAULTS}).getDifference(oldObject, newObject);
but it seems very ugly, and unitils
library entirely seems to be too hard for required compare purposes.
Furthermore, it is quite clear how to create such deepEquals
by self, but it is unlikely that there aren't common used libraries which contains already implemented method like this. Any ideas?
Aucun commentaire:
Enregistrer un commentaire