Reputation: 435
Is there a simple way to copy one objects content into a new object of a different class/target type. the objects share at least a superclass and also a lot properties/attributes.
I used this approach with a different target class: https://stackoverflow.com/a/3899882/1949775. I don't get an exception but somehow I get a wrong target type as a result...
What I'm looking for is an easy way to shovel one objects content into a new instance of a different target type then the source is.
Thanks for your help.
Upvotes: 0
Views: 122
Reputation: 136042
You can try Apache commons-beanutils
BeanUtils.copyProperties(source, target);
API
Copies the property values of the given source bean into the target bean.
Note: The source and target classes do not have to match or even be derived from each other, as long as the properties match. Any bean properties that the source bean exposes but the target bean does not will silently be ignored.
This is just a convenience method. For more complex transfer needs, consider using a full BeanWrapper.
Upvotes: 2