rharing
Jan 15th, 2005, 02:34 PM
Hi all,
small question (or request for change when deemed necessary). I use the BeanUtils.copyProperties method to populate a domain object from a form. The problem is that the form has properties for 3 specific domain objects, and thus when using BeanUtils.copyProperties method, I receive an error because not each object has all the properties. So while looking around, I saw that the BeanWrapperImpl throws an invalidPropertyException when a descriptor is missing. I understand why this is needed, but right now I can't use the copyProperties method. Is it possible to add another method that will catch the exception and just continue?
e.g.
/**
* Shameless rip of BeanUtils.copyProperties, but this method will catch the exceptions that are thrown when the
* descriptor's don't match in either bean, populating only those properties that are both present.
*
* Copy the property values of the given source bean into the given target bean,
* ignoring the given ignoreProperties.
* @param source the source bean
* @param target the target bean
* @param ignoreProperties array of property names to ignore
* @throws IllegalArgumentException if the classes of source and target do not match
*/
public static void copyProperties(Object source, Object target, String[] ignoreProperties)
throws IllegalArgumentException, BeansException {
if (source == null || target == null) {
throw new IllegalArgumentException("Source and target must not be null");
}
List ignoreList = (ignoreProperties != null) ? Arrays.asList(ignoreProperties) : null;
BeanWrapper sourceBw = new BeanWrapperImpl(source);
BeanWrapper targetBw = new BeanWrapperImpl(target);
MutablePropertyValues values = new MutablePropertyValues();
for (int i = 0; i < sourceBw.getPropertyDescriptors().length; i++) {
PropertyDescriptor sourceDesc = sourceBw.getPropertyDescriptors()[i];
String name = sourceDesc.getName();
PropertyDescriptor targetDesc = null;
try {
targetDesc = targetBw.getPropertyDescriptor(name);
}
catch (InvalidPropertyException e) {
continue;
}
if (targetDesc.getWriteMethod() != null && targetDesc.getReadMethod() != null &&
(ignoreProperties == null || (!ignoreList.contains(name)))) {
values.addPropertyValue(new PropertyValue(name, sourceBw.getPropertyValue(name)));
}
}
targetBw.setPropertyValues(values);
}
Or should I use another class/method for this scenario?
Regards
Ronald
small question (or request for change when deemed necessary). I use the BeanUtils.copyProperties method to populate a domain object from a form. The problem is that the form has properties for 3 specific domain objects, and thus when using BeanUtils.copyProperties method, I receive an error because not each object has all the properties. So while looking around, I saw that the BeanWrapperImpl throws an invalidPropertyException when a descriptor is missing. I understand why this is needed, but right now I can't use the copyProperties method. Is it possible to add another method that will catch the exception and just continue?
e.g.
/**
* Shameless rip of BeanUtils.copyProperties, but this method will catch the exceptions that are thrown when the
* descriptor's don't match in either bean, populating only those properties that are both present.
*
* Copy the property values of the given source bean into the given target bean,
* ignoring the given ignoreProperties.
* @param source the source bean
* @param target the target bean
* @param ignoreProperties array of property names to ignore
* @throws IllegalArgumentException if the classes of source and target do not match
*/
public static void copyProperties(Object source, Object target, String[] ignoreProperties)
throws IllegalArgumentException, BeansException {
if (source == null || target == null) {
throw new IllegalArgumentException("Source and target must not be null");
}
List ignoreList = (ignoreProperties != null) ? Arrays.asList(ignoreProperties) : null;
BeanWrapper sourceBw = new BeanWrapperImpl(source);
BeanWrapper targetBw = new BeanWrapperImpl(target);
MutablePropertyValues values = new MutablePropertyValues();
for (int i = 0; i < sourceBw.getPropertyDescriptors().length; i++) {
PropertyDescriptor sourceDesc = sourceBw.getPropertyDescriptors()[i];
String name = sourceDesc.getName();
PropertyDescriptor targetDesc = null;
try {
targetDesc = targetBw.getPropertyDescriptor(name);
}
catch (InvalidPropertyException e) {
continue;
}
if (targetDesc.getWriteMethod() != null && targetDesc.getReadMethod() != null &&
(ignoreProperties == null || (!ignoreList.contains(name)))) {
values.addPropertyValue(new PropertyValue(name, sourceBw.getPropertyValue(name)));
}
}
targetBw.setPropertyValues(values);
}
Or should I use another class/method for this scenario?
Regards
Ronald