Question

I would like to copy the property values from Class A to Class B with BeanUtils which has same fields but with different names. Is it possible to provide a map of property name to differentName, age to differentAge etc., and achieve the copying? I am interested to know if this is possible by any means using only Apache Commons utilities (not any other tools).

class ClassA{
    private String name;
    private Integer age;
    ... // Setter and Getter methods
} 

class ClassB{
    private String differentName;
    private Integer differentAge;
    ... // Setter and Getter methods for the private fields
}
Was it helpful?

Solution

Apache Commons BeanUtils has method the method populate(Object bean, Map properties) accepts a map to populate the Bean with key value pairs.

NOTE: I just saw the limitation on Apache-Commons - but it may still be useful for other people and as far as I am concerned the better solution.

Use Dozer when the names match it will automatically copy the values. Or as in your case you can specify source and target Members in an xml mapping file.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top