List of usage examples for org.apache.commons.beanutils ConvertUtilsBean convert
public Object convert(String values[], Class clazz)
From source file:org.xmlactions.common.reflection.BeanManager.java
/** * Sets a property on a bean/*w ww. j a v a2 s.c o m*/ * @param bean * @param propertyName * @param value */ public void setPropertyValue(Object bean, String propertyName, Object value) { Method setMethod = findMatchingSetMethod(bean, propertyName); try { ConvertUtilsBean cub = new ConvertUtilsBean(); Object data = cub.convert(value, setMethod.getParameterTypes()[0]); setMethod.invoke(bean, data); } catch (Exception e) { throw new IllegalArgumentException("Unable to set property [" + propertyName + "] on bean [" + bean.getClass().getCanonicalName() + "] with the value [" + value + "]", e); } }
From source file:org.xmlactions.mapping.xml_to_bean.PopulateClassFromXml.java
private void setProperty(Object object, String propertyName, Object value, String fieldName, XMLObject xo) throws InvocationTargetException, NoSuchMethodException, NoSuchFieldException, IllegalAccessException { PropertyDescriptor pd = null; try {//from www . j av a 2s . c o m pd = findPropertyDescriptor(object, fieldName); if (pd != null) { BeanUtils.setProperty(object, fieldName, value); } else { Class<?> c = object.getClass(); Field field = c.getDeclaredField(fieldName); ConvertUtilsBean cub = new ConvertUtilsBean(); Object converted = cub.convert(value, field.getType()); field.set(object, converted); } } catch (ConversionException ex) { if (ignoreErrorWithEmptyValues == true && (value == null || ("" + value).length() == 0)) { // carry on processing, ignore because we have an empty value } else { throw new ConversionException("Unable to set property [" + propertyName + "] on bean [" + object.getClass().getName() + "] with value of [" + value + "] error:" + ex.getMessage(), ex); } } catch (NoSuchFieldException ex) { if (strict == true) { throw ex; } else { // log.warn(object.getClass().getName() + // " has no such field [" + propertyName + "]:" + // ex.getMessage()); } } catch (IllegalAccessException ex) { throw new IllegalAccessException("Unable to set property [" + propertyName + "] on bean [" + object.getClass().getName() + "] with value of [" + value + "] error:" + ex.getMessage()); } catch (IllegalArgumentException ex) { if (pd != null) { try { // try and match up a populator. Class<?> obj = pd.getPropertyType(); log.debug("obj:" + obj); if (obj != null) { if (List.class.getName().equals(obj.getName())) { useAction(PopulatorArrayList.class.getName(), null, object, fieldName, value, xo); } } } catch (Exception ex_ignore) { // ignore this, we'll throw the original exception throw ex; } } else { throw ex; } } }