I have two instances of the same class: one has values in all its fields and the other just some fields (the other fields are null). I want to update the values of the first object using the second's values, only if it isn't null. Something like this:
class MyObject {
private void setObject(MyObject other) {
if (other.getField1() != null) this.field1 = other.getField1();
.....
}
}
The setObject method's code is really long when I have a lot of fields, and I need to implement that method in all my objects. I was wondering if it can be done on a more generic way: get object as argument -> iterate over its fields -> populate if isn't null.
I tried usage of some reflection to do this, unsuccessfully for now.
Aucun commentaire:
Enregistrer un commentaire