public static Object newBean(String beanClassName, Map<String, Object> properties, ClassProvider factory) { Object bean = BeanUtil.newInstance(factory.forName(beanClassName)); for (Map.Entry<String, Object> entry : properties.entrySet()) { String propertyName = entry.getKey(); NamedMutator mutator = PropertyMutatorFactory.getPropertyMutator(bean.getClass(), propertyName, false, true); try { mutator.setValue(bean, entry.getValue()); } catch (UpdateFailedException e) { throw new RuntimeException("Unable to set property " + propertyName + " on class " + beanClassName, e); } } return bean; }
public DataContainer<E> next(DataContainer<E> wrapper) { int i = 0; try { DataContainer<String[]> tmp = iterator.next(dataContainer.get()); if (tmp == null) return null; String[] line = tmp.getData(); E bean = BeanUtil.newInstance(type); int columns = Math.min(line.length, mutators. length); for (i = 0; i < columns; i++) { String value = line[i]; if (value.length() == 0) value = emptyValue; mutators[i].setValue(bean, value); } return wrapper.setData(bean); } catch (UpdateFailedException e) { throw new ConfigurationError("Failed to set property '" + mutators[i].getName() + "' on class " + type); } }
@SuppressWarnings("unchecked") @Override public DataContainer<E> next(DataContainer<E> wrapper) { int i = 0; String value = null; try { DataContainer<String[]> tmp = nextRaw(dataContainer.get()); if (tmp == null) return null; String[] line = tmp.getData(); if (line.length == 0) return null; Class<E> beanClass = (classIndex >= 0 ? (Class<E>) BeanUtil.forName(line[classIndex]) : type); E bean = BeanUtil.newInstance(beanClass); int columns = Math.min(line.length, mutators.length); for (i = 0; i < columns; i++) { if (i != classIndex) { value = line[i]; if (value != null && value.length() == 0) value = emptyValue; mutators[i].setValue(bean, value); } } return wrapper.setData(bean); } catch (Exception e) { e.printStackTrace(); throw new ConfigurationError("Failed to set property '" + mutators[i].getName() + "' to '" + value + "' on class " + type, e); } }
@Override public void setValue(Object bean, Object propertyValue) throws UpdateFailedException { if (bean == null) if (required) throw new IllegalArgumentException("Cannot set a property on null"); else return; logger.debug("setting property '" + getName() + "' to '" + propertyValue + "' on bean " + bean); Object superBean = bean; if (subAccessors != null) { for (PropertyAccessor<Object, ?> subAccessor : subAccessors) { Object subBean = subAccessor.getValue(superBean); if (subBean == null && propertyValue != null) { // bean is null but since there is something to set create one Class<?> propertyType = subAccessor.getValueType(); subBean = BeanUtil.newInstance(propertyType); BeanUtil.setPropertyValue(superBean, subAccessor.getPropertyName(), subBean); } superBean = subBean; } } lastMutator.setValue(superBean, propertyValue); }