Are there any utils in java which allow to check objects equality without equals overriding? For some reasons I don't want to provide my class with equals method. I need something like SomeUtils.equals(a,b) in my unit test which compares all object fields (via reflection I guess).
Asked
Active
Viewed 5,322 times
3
-
1I think you got your answer, do it with reflection.... I don't really see what is the question here. – gaRos May 12 '15 at 15:27
-
1I don't want to do this by myself. I looking for prepared solution. – Normal May 12 '15 at 15:29
-
And what if your fields are not primitives? Should these objects also be compared by fields? – Tagir Valeev May 12 '15 at 15:29
-
In that case the question is off-topic as a recommendation question... – Jon Skeet May 12 '15 at 15:30
-
1Why don't you want to override [i]equals[/i], it can do no harm to your application in anyway and other methods then can check properly if object a equals object b. Doing it via reflection is a hassle and is everything but efficient – engineercoding May 12 '15 at 15:33
-
https://commons.apache.org/proper/commons-lang/javadocs/api-release/index.html?org/apache/commons/lang3/StringEscapeUtils.html – JB Nizet May 12 '15 at 15:34
-
1This is a duplicate question http://stackoverflow.com/questions/1449001/is-there-a-java-reflection-utility-to-do-a-deep-comparison-of-two-objects – bhspencer May 12 '15 at 15:35
-
@TagirValeev equals should be used for not primitives fields in my case – Normal May 12 '15 at 15:39
-
@engineercoding I m not owner of that class and can't access it source code – Normal May 12 '15 at 15:40
-
So? Apply `equals` recursively if needed. – chrylis -cautiouslyoptimistic- May 12 '15 at 15:40
1 Answers
6
You could use EqualsBuilder.reflectionEquals(this, obj);
in Apache Commons EqualsBuilder

Syam S
- 8,421
- 1
- 26
- 36
-
Thanks! That is I'm looking for. As mention by @bhspencer, my question is duplication for http://stackoverflow.com/questions/1449001/is-there-a-java-reflection-utility-to-do-a-deep-comparison-of-two-objects – Normal May 12 '15 at 15:47
-
-