Assume the following class:
public class TestClass {
String attr1;
String attr2;
String attr3;
}
and a client code like:
final TestClass testClassA = new TestClass();
testClassA.attr1 = "1";
testClassA.attr1 = "2";
testClassA.attr1 = "3";
final TestClass testClassB = new TestClass();
I would like to find way/method that updates testClassB with all the values of testClassA.
testClassB.updateAll(testClassA)
One such solution would be:
public void updateAll(TestClass testClass) {
this.attr1 = testClass.attr1;
this.attr2 = testClass.attr2;
this.attr3 = testClass.attr3;
}
Now, here comes the thing: I would like to not have to write this method manually for it to be less resilient when e.g. a new attribute is added. In this case I might forget to add it to the update-method.
The solution does not need to assign the values directly, in fact I'd prefer it to call setter methods.
It is also possible for me to use any 3rd party frameworks out there like Lombok. I am looking for something like the @RequiredArgsConstructor, however I need the new object to be updated and not created.
So something like a @RequiredArgsSetter or a Object.updateInto(Object1 o, Object2 o) method, but again, it should not create a new object but simply update all the fields of an existing object.
Bonus points, if it is somehow possible to annotate the fields which should be included or excluded from being set.