Might be a little late, but I was in the same situation like you and ended up creating my own library for exactly your use-case. Since I was forced to come up with a solution myself, I decided to release it on Github, to spare others the hard work. You can find it here: https://github.com/SQiShER/java-object-diff

--- Edit ---

Here is a little usage example based on the OPs code:

SomeClass a = new SomeClass();
SomeClass b = new SomeClass();

a.setProp1("A");
a.setProp2("X");

b.setProp1("B");
b.setProp2("X");

DiffNode diff = ObjectDifferBuilder.buildDefault().compare(a, b);

assert diff.hasChanges();
assert diff.childCount() == 1;
assert diff.getChild('prop1').getState() == DiffNode.State.CHANGED;
Answer from SQiShER on Stack Overflow
🌐
Readthedocs
java-object-diff.readthedocs.io
java-object-diff Documentation
java-object-diff is a simple, yet powerful library to find differences between Java objects. It takes two objects and generates a tree structure that represents any differences between the objects and their children.
🌐
GitHub
github.com › SQiShER › java-object-diff
GitHub - SQiShER/java-object-diff: Library to diff and merge Java objects with ease · GitHub
java-object-diff is a simple, yet powerful library to find differences between Java objects. It takes two objects and generates a tree structure that represents any differences between the objects and their children.
Starred by 970 users
Forked by 176 users
Languages: Java 53.4% | Groovy 46.6%
🌐
Readthedocs
java-object-diff.readthedocs.io › en › latest › getting-started
Getting Started - java-object-diff Documentation
java-object-diff provides a very simple API and tries to make everything as self-explanatory as possible. It can handle a wide variety of object structures without the need for any configuration.
Top answer
1 of 8
56

Might be a little late, but I was in the same situation like you and ended up creating my own library for exactly your use-case. Since I was forced to come up with a solution myself, I decided to release it on Github, to spare others the hard work. You can find it here: https://github.com/SQiShER/java-object-diff

--- Edit ---

Here is a little usage example based on the OPs code:

SomeClass a = new SomeClass();
SomeClass b = new SomeClass();

a.setProp1("A");
a.setProp2("X");

b.setProp1("B");
b.setProp2("X");

DiffNode diff = ObjectDifferBuilder.buildDefault().compare(a, b);

assert diff.hasChanges();
assert diff.childCount() == 1;
assert diff.getChild('prop1').getState() == DiffNode.State.CHANGED;
2 of 8
54

http://javers.org is library that does exacly what you need: has methods like compare(Object leftGraph, Object rightGraph) returning the Diff object. Diff contains a list of changes (ReferenceChange, ValueChange, PropertyChange) e.g.

given:
DummyUser user =  dummyUser("id").withSex(FEMALE).build();
DummyUser user2 = dummyUser("id").withSex(MALE).build();
Javers javers = JaversTestBuilder.newInstance()

when:
Diff diff = javers.compare(user, user2)

then:
diff.changes.size() == 1
ValueChange change = diff.changes[0]
change.leftValue == FEMALE
change.rightValue == MALE

It can handle cycles in graphs.

In addition you can get Snapshot of any graph object. Javers has JSON serializers and deserializers to snapshot, and changes so you can easily save them in database. With this library you can easily implement a module for auditing.

🌐
GitHub
github.com › SQiShER › java-object-diff › blob › master › README.md
java-object-diff/README.md at master · SQiShER/java-object-diff
`java-object-diff` is a simple, yet powerful library to find differences between Java objects. It takes two objects and generates a tree structure that represents any differences between the objects and their children.
Author: SQiShER
🌐
Readthedocs
java-object-diff.readthedocs.io › en › latest › user-guide
User Guide - java-object-diff Documentation - Read the Docs
The objects given to the ObjectDiffer are called working and base, where working is considered to be a modified version of base. There is actually no technical reason for this. The ObjectDiffer doesn't care how the objects relate to each other. They could actually be two completely different objects that never shared a mutual history.
🌐
Sqisher
sqisher.github.io › java-object-diff
java-object-diff by SQiShER
java-object-diff is a simple, yet powerful library to find differences between Java objects. It takes two objects and generates a tree structure that represents any differences between the objects and their children.
Find elsewhere
🌐
Javers
javers.org › documentation › diff-examples
Diff Examples — JaVers Documentation
Diff: * new object: Employee/Hired One - 'boss' = 'Employee/Big Boss' - 'name' = 'Hired One' - 'salary' = '10000' * new object: Employee/Hired Second - 'boss' = 'Employee/Big Boss' - 'name' = 'Hired Second' - 'salary' = '10000' * changes in Employee/Big Boss : - 'subordinates' collection changes : 1.
🌐
Maven Repository
mvnrepository.com › artifact › de.danielbechler › java-object-diff
Maven Repository: de.danielbechler » java-object-diff
January 16, 2018 - Home » de.danielbechler » java-object-diff · Library to diff and merge Java objects with ease · LicenseApache 2.0 · CategoriesDiff and Patch Libraries · Tagsdiffdeltapatch · Ranking · #482667in MvnRepository · #14in Diff and Patch Libraries · HomePage https://github.com/SQiShER/java-object-diff 🔍 Inspect URL ·
🌐
Javadoc.io
javadoc.io › doc › de.danielbechler › java-object-diff › latest › index.html
java-object-diff 0.95 javadoc (de.danielbechler)
Latest version of de.danielbechler:java-object-diff · https://javadoc.io/doc/de.danielbechler/java-object-diff · Current version 0.95 · https://javadoc.io/doc/de.danielbechler/java-object-diff/0.95 · package-list path (used for javadoc generation -link option) https://javadoc.io/doc/de...
🌐
Maven Central
central.sonatype.com › artifact › de.danielbechler › java-object-diff
java-object-diff - de.danielbechler - Maven Central - Sonatype
<dependency> <groupId>de.danielbechler</groupId> <artifactId>java-object-diff</artifactId> <version>0.95</version> </dependency>
🌐
Maven Repository
mvnrepository.com › artifact › de.danielbechler › java-object-diff › 0.10.2
Maven Repository: de.danielbechler » java-object-diff » 0.10.2
Home » de.danielbechler » java-object-diff » 0.10.2 · Library to diff and merge Java objects with ease · Note: There is a new version for this artifact · Maven · Gradle · SBT · Mill · Ivy · Grape · Leiningen · Buildr · Scope: compile · test · provided ·
🌐
GitHub
github.com › SQiShER › java-object-diff › wiki › Getting-Started
Getting Started · SQiShER/java-object-diff Wiki · GitHub
Library to diff and merge Java objects with ease. Contribute to SQiShER/java-object-diff development by creating an account on GitHub.
Author: SQiShER
🌐
GitHub
github.com › SQiShER › java-object-diff › issues › 210
How to get the difference between two simple objects? · Issue #210 · SQiShER/java-object-diff
July 31, 2019 - Let's say I have two simple objects and I need to know if there is a difference between them and if there is I need to know where and I also need to know the previous value of that field and the new value.
Author: SQiShER
🌐
Baeldung
baeldung.com › home › java › using the apache commons lang 3 for comparing objects in java
Using the Apache Commons Lang 3 for Comparing Objects in Java | Baeldung
January 8, 2024 - Various libraries can be used for comparing objects in Java, such as JaVers or Apache Commons Lang 3, which we will cover in this article. Apache Commons Lang 3 represents the 3.0 version of the Apache Commons Lang library, which offers many functionalities. We will explore the DiffBuilder class to compare and obtain the differences between two objects of the same type.
🌐
Apache Commons
commons.apache.org › proper › commons-lang › javadocs › api-3.9 › org › apache › commons › lang3 › builder › DiffResult.html
DiffResult (Apache Commons Lang 3.9 API)
java.lang.Object · org.apache.commons.lang3.builder.DiffResult · All Implemented Interfaces: Iterable<Diff<?>> public class DiffResult extends Object implements Iterable<Diff<?>> A DiffResult contains a collection of the differences between two Diffable objects.
🌐
Netlify
mallim.netlify.app › java-tips-how-to-compare-the-difference-for-two-given-java-objects
Java Tips: How to compare the difference for two given Java objects? | Mallim's Site
September 24, 2020 - JaVers Homepage: https://javers.org/documentation/ Quick intro: https://www.baeldung.com/javers java-object-dff Homepage: http://sqisher.github.io/java-object-diff/ Example 1 : https://morioh.com/p/c98c7721a9ed Example 2 (Chinese) : ...
🌐
GitHub
github.com › SQiShER › java-object-diff › issues › 42
How to diff Lists? · Issue #42 · SQiShER/java-object-diff
November 12, 2012 - While List order is relevant, the differ does not detect changes in the order of the items. ... List<String> left = Arrays.asList("aaa", "bbb", "ccc"); List<String> right = Arrays.asList("aaa", "ccc", "bbb"); ObjectDiffer objectDiffer = ObjectDifferFactory.getInstance(); final Node root = objectDiffer.compare(left, right); System.out.println("Equals: " + left.equals(right)); root.visit(new PrintingVisitor(left, right));
Author: SQiShER