@Override public void setValue(Object target, Object value) throws UpdateFailedException { setValue(target, path, value, required, autoConvert); }
private void createMutators(String[] attributePaths) { this.mutators = new Mutator[attributePaths.length]; for (int i = 0; i < attributePaths.length; i++) { String attributePath = attributePaths[i]; if ("class".equals(attributePath)) { this.mutators[i] = new EmptyMutator(); } else { this.mutators[i] = new AnyMutator(attributePath, false, true); } } }
private void init(CSVLineIterator iterator, Class<E> type, String emptyValue, String[] attributeNames) { this.iterator = iterator; this.type = type; this.emptyValue = emptyValue; this.mutators = new NamedMutator[attributeNames.length]; this.classIndex = -1; for (int i = 0; i < attributeNames.length; i++) { String attributeName = attributeNames[i]; if ("class".equals(attributeName)) { mutators[i] = null; this.classIndex = i; } else { mutators[i] = new AnyMutator(attributeName, false, true); } } }
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); } }
public static <C, V> void setValue(C target, String path, V value, boolean required, boolean autoConvert) { int sep = path.indexOf('.'); if (sep < 0) // it is a local property setLocal(target, path, value, required, autoConvert); else { // a recursive feature path needs to be resolved String localName = path.substring(0, sep); Object subTarget = FeatureAccessor.getValue(target, localName, true); if (subTarget == null) { // feature exists but is null, so create an object and assign it to the feature subTarget = setFeatureDefault(target, localName); } String remainingName = path.substring(sep + 1); setValue(subTarget, remainingName, value, required, autoConvert); } }
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; }
private Object haveTargetObject(Object bean, String featurePath) { if (featurePath == null) { return bean; } else if (featurePath.contains(".")) { String[] pathParts = StringUtil.splitOnFirstSeparator(featurePath, '.'); Object child = haveTargetObject(bean, pathParts[0]); return haveTargetObject(child, pathParts[1]); } Object value = FeatureAccessor.getValue(bean, featurePath, false); if (value != null) return value; else return AnyMutator.setFeatureDefault(bean, featurePath); }
break; case ASSERT_EQUALS: if (isEmpty(oldValue)) { realMutator.setValue(target, value); } else if (!NullSafeComparator.equals(oldValue, value)) break; case SET_IF_UNDEFINED: if (isEmpty(oldValue)) realMutator.setValue(target, value); else break; case SET_IF_GREATER: if (isEmpty(oldValue)) realMutator.setValue(target, value); else if (comparator.compare(oldValue, value) == -1)
@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); } }
public static <C, V> void setValue(C target, String path, V value) { setValue(target, path, value, true, false); }
@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); }
@Override public Object evaluate(Context context) { Object value = rhs.evaluate(context); if (lhs.length == 1) { // if lhs is a simple variable name then put the result into context by this name context.set(lhs[0], value); } else { // get last parent object of QN and set the feature denoted by the last QN part String fieldName = lhs[lhs.length - 1]; Object field = QNExpression.resolveNamePart(lhs, lhs.length - 1, context); AnyMutator.setValue(field, fieldName, value, true, true); } return value; }
public <T> T parseAsBean(String row, Class<T> beanClass) { if (row.length() != rowLength) throw new SyntaxError("Row of type '" + name + "' has illegal length. " + "Expected: " + rowLength + ", found: " + row.length(), "'" + row + "'"); T bean = BeanUtil.newInstance(beanClass); ParsePosition pos = new ParsePosition(0); for (FixedWidthColumnDescriptor columnDescriptor : columnDescriptors) { int endIndex = pos.getIndex() + columnDescriptor.getWidth(); String cellContent = row.substring(pos.getIndex(), endIndex); try { Object cellObject = columnDescriptor.parse(cellContent); AnyMutator.setValue(bean, columnDescriptor.getName(), cellObject, true, true); } catch (ParseException e) { throw new SyntaxError("Error parsing column '" + columnDescriptor + "'. " + e.getMessage(), cellContent); } pos.setIndex(endIndex); } return bean; }
@Override public E evaluate(Context context) { E bean = instantiation.evaluate(context); for (Assignment assignment : assignments) { String name = assignment.getName(); Object value = assignment.getExpression().evaluate(context); if (BeanUtil.hasProperty(bean.getClass(), name)) BeanUtil.setPropertyValue(bean, name, value, false); else AnyMutator.setValue(bean, name, value, true, true); } if (bean instanceof ContextAware) ((ContextAware) bean).setContext(context); return bean; }