I am trying to copy all properties from one bean into another:
public void copy(MyBean bean){
setPropertyA(bean.getPropertyA());
setPropertyB(bean.getPropertyB());
[..]
}
This is error prone and a lot to write if you have a bean with lots of properties.
I was thinking of reflection to do this, but I cannot "connect" the getter from one object to the setter of the other one.
public List<Method> getAllGetters(Object object){
List<Method> result = new ArrayList<>();
for (final PropertyDescriptor propertyDescriptor : Introspector.getBeanInfo(object.getClass()).getPropertyDescriptors()) {
result.add(readMethod = propertyDescriptor.getReadMethod());
}
return result;
}
Edit:
BeanUtils.copyProperties(this, anotherBean);
Works just as expected!