An option that is more effective than Reflection is to save the fields on the map:
Map<String, Object> fields;
void merge(MyClass other){
for (String fieldName : fields.keys()){
Object thisValue = this.fields.get(key);
Object otherValue = other.fields.get(key);
if (thisValue != otherValue && otherValue != null){
this.fields.put(fieldName, otherValue);
}
}
}
This will make merging more efficient, but make sharing fields less efficient.
source
share