class Foo(){
private String x,y;
//getters setters
}
main(){
Foo bar1 = new Foo();
Foo bar2 = new Foo();
bar1.setX("hey");
bar2.setX("hi");
bar2.setY(" there");
setNewValuesFromLeftToRight(bar1,bar2);//left:bar1
System.out.print(bar2.getX+bar2.getY)// hey there
}
setNewValuesFromLeftToRight : this method would get any 2 object with the same class and set field values of bar2 using field values,that are not null,of bar1
What is the best way to write the method setNewValuesFromLeftToRight ? sure it should be generic solution.
Will I use the Reflections?
The way I read these requirements that any property in the right (target) bean should be overwritten iff there is a corresponding non-null value in the left (source) bean. So that’s slightly different from PropertyUtils.copyProperties which would overwrite all properties (including null source values).
One possibility would be to use Jakarta Commons BeanUtils, then you can use