List of usage examples for org.apache.commons.beanutils PropertyUtils setProperty
public static void setProperty(Object bean, String name, Object value) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException
Set the value of the specified property of the specified bean, no matter which property reference format is used, with no type conversions.
For more details see PropertyUtilsBean
.
From source file:org.settings4j.config.DOMConfigurator.java
/** * Sets a parameter based from configuration file content. * * @param elem// w ww .ja v a 2 s . c om * param element, may not be null. * @param propSetter * property setter, may not be null. * @param props * properties */ private void setParameter(final Element elem, final Object bean, final Connector[] connectors) { final String name = elem.getAttribute(NAME_ATTR); final String valueStr = elem.getAttribute(VALUE_ATTR); try { final PropertyDescriptor propertyDescriptor = PropertyUtils.getPropertyDescriptor(bean, name); final Method setter = PropertyUtils.getWriteMethod(propertyDescriptor); Object value; if (connectors != null) { value = subst(valueStr, connectors, setter.getParameterTypes()[0]); } else { value = subst(valueStr, null, setter.getParameterTypes()[0]); } PropertyUtils.setProperty(bean, name, value); } catch (final IllegalAccessException e) { LOG.warn("Cannnot set Property: {}", name, e); } catch (final InvocationTargetException e) { LOG.warn("Cannnot set Property: {}", name, e); } catch (final NoSuchMethodException e) { LOG.warn("Cannnot set Property: {}", name, e); } }
From source file:org.sinekartads.dto.BaseDTO.java
protected void formatValues(Map<DTOPropertyType, String> dtoPropertyFormats) throws IllegalArgumentException { // Mapping annotation -> dtoPropertyType restricted on the managed annotations Map<Class<? extends Annotation>, DTOPropertyType> annotationTypes = new HashMap<Class<? extends Annotation>, DTOPropertyType>(); for (Map.Entry<DTOPropertyType, String> entry : dtoPropertyFormats.entrySet()) { annotationTypes.put(entry.getKey().getAnnot(), entry.getKey()); }//from w ww .jav a 2 s.c o m try { String newFormat; DateFormat newDateFormat = null; DateFormat newTimeFormat = null; DateFormat newDateTimeFormat = null; NumberFormat newIntegerFormat = null; NumberFormat newDecimalFormat = null; // create the the custom formatters newFormat = dtoPropertyFormats.get(DTOPropertyType.Date); if (newFormat != null) { newDateFormat = new SimpleDateFormat(newFormat); } newFormat = dtoPropertyFormats.get(DTOPropertyType.Time); if (newFormat != null) { newTimeFormat = new SimpleDateFormat(newFormat); } newFormat = dtoPropertyFormats.get(DTOPropertyType.DateTime); if (newFormat != null) { newDateTimeFormat = new SimpleDateFormat(newFormat); } newFormat = dtoPropertyFormats.get(DTOPropertyType.Integer); if (newFormat != null) { newIntegerFormat = new DecimalFormat(newFormat); } newFormat = dtoPropertyFormats.get(DTOPropertyType.Decimal); if (newFormat != null) { newDecimalFormat = new DecimalFormat(newFormat); } // change formats into the dto and all its children Field[] fields = getClass().getDeclaredFields(); String property; Class<?> fieldType; DTOPropertyType dtoPropertyType; for (Field field : fields) { // ignore the static field if ((field.getModifiers() & java.lang.reflect.Modifier.STATIC) > 0) continue; property = field.getName(); fieldType = field.getType(); if (BaseDTO.class.isAssignableFrom(fieldType)) { // recursion on the children (as single member) BaseDTO dto = (BaseDTO) PropertyUtils.getProperty(this, property); dto.formatValues(dtoPropertyFormats); } else if (BaseDTO[].class.isAssignableFrom(fieldType)) { // recursion on the children (as an array) for (BaseDTO dto : (BaseDTO[]) PropertyUtils.getProperty(this, property)) { dto.formatValues(dtoPropertyFormats); } } else { // format the other (String) values String strValue = (String) PropertyUtils.getProperty(this, property); if (StringUtils.isNotBlank(strValue)) { Object value = null; for (Annotation annot : field.getAnnotations()) { // dtoPropertyType of the current field (or null) dtoPropertyType = annotationTypes.get(annot.annotationType()); // newFormat to be applied to the current field's dtoPropertyType newFormat = dtoPropertyFormats.get(dtoPropertyType); // if not null (the annotation is owned by a managed DtoPropertyType) if (newFormat != null) { // in every managed case, parse the value and apply to it the new format switch (dtoPropertyType) { case Flag: { value = BooleanUtils.toBoolean(strValue); PropertyUtils.setProperty(this, property, value); break; } case Date: { value = dateFormat.parse(strValue); PropertyUtils.setProperty(this, property, newDateFormat.format(value)); break; } case Time: { value = timeFormat.parse(strValue); PropertyUtils.setProperty(this, property, newTimeFormat.format(value)); break; } case DateTime: { value = dateTimeFormat.parse(strValue); PropertyUtils.setProperty(this, property, newDateTimeFormat.format(value)); break; } case Integer: { value = integerFormat.parse(strValue).intValue(); PropertyUtils.setProperty(this, property, newIntegerFormat.format(value)); break; } case Decimal: { value = decimalFormat.parse(strValue); PropertyUtils.setProperty(this, property, newDecimalFormat.format(value)); } default: throw new IllegalArgumentException( "unimplemented format: " + dtoPropertyType.name()); } // end switch } } // non-annotated fields are not modified } } } // update the internal formatters if (newDateFormat != null) dateFormat = newDateFormat; if (newTimeFormat != null) timeFormat = newTimeFormat; if (newDateTimeFormat != null) dateTimeFormat = newDateTimeFormat; if (newIntegerFormat != null) integerFormat = newIntegerFormat; if (newDecimalFormat != null) decimalFormat = newDecimalFormat; } catch (RuntimeException e) { throw e; } catch (Exception e) { // errors that should happen only during the test phase throw new RuntimeException(e); } }
From source file:org.slc.sli.api.migration.strategy.impl.AddFieldStrategy.java
@Override public List<EntityBody> migrate(List<EntityBody> entityList) throws MigrationException { for (EntityBody entityBody : entityList) { try {/*from ww w . j a va2s . c o m*/ if (!entityBody.containsKey(fieldName)) { PropertyUtils.setProperty(entityBody, fieldName, resolveRules(entityBody)); } } catch (IllegalAccessException e) { throw new MigrationException(e); } catch (InvocationTargetException e) { throw new MigrationException(e); } catch (NoSuchMethodException e) { throw new MigrationException(e); } } return entityList; }
From source file:org.slc.sli.dal.migration.strategy.impl.AddStrategy.java
@Override public Entity migrate(Entity entity) throws MigrationException { try {//from www . ja v a 2 s .c o m PropertyUtils.setProperty(entity.getBody(), fieldName, defaultValue); } catch (IllegalAccessException e) { throw new MigrationException(e); } catch (InvocationTargetException e) { throw new MigrationException(e); } catch (NoSuchMethodException e) { throw new MigrationException(e); } return entity; }
From source file:org.slc.sli.dal.migration.strategy.impl.CardinalityStrategy.java
@Override public Entity migrate(Entity entity) throws MigrationException { // the only 2 cases that impact cardinality are if a field has // gone from minCount of 0 to 1, or if maxCount goes from many to 1 Object valueObject = entity.getBody().get(fieldName); if (valueObject == null) { if (minCount.equals("1")) { // this is the case for a field that has gone from optional to required - either 1 or many if (maxCount.equals("many")) { List<String> fieldValues = new ArrayList<String>(); fieldValues.add(DEFAULT_VALUE); try { PropertyUtils.setProperty(entity.getBody(), fieldName, fieldValues); } catch (IllegalAccessException e) { throw new MigrationException(e); } catch (InvocationTargetException e) { throw new MigrationException(e); } catch (NoSuchMethodException e) { throw new MigrationException(e); }/*from ww w. j a v a 2 s. c o m*/ } else { try { PropertyUtils.setProperty(entity.getBody(), fieldName, defaultValue); } catch (IllegalAccessException e) { throw new MigrationException(e); } catch (InvocationTargetException e) { throw new MigrationException(e); } catch (NoSuchMethodException e) { throw new MigrationException(e); } } } } else if (valueObject instanceof List) { if (maxCount.equals("1")) { // this is the case where we've gone from many to 1 List valueList = (List) valueObject; Object fieldValue = defaultValue; if (valueList.size() > 0) { fieldValue = valueList.get(0); } try { PropertyUtils.setProperty(entity.getBody(), fieldName, fieldValue); } catch (IllegalAccessException e) { throw new MigrationException(e); } catch (InvocationTargetException e) { throw new MigrationException(e); } catch (NoSuchMethodException e) { throw new MigrationException(e); } } } else if (valueObject instanceof String) { if (maxCount.equals("many")) { // this is the case where we've gone from 1 to many String valueString = (String) valueObject; List<String> fieldValues = new ArrayList<String>(); fieldValues.add(valueString); try { PropertyUtils.setProperty(entity.getBody(), fieldName, fieldValues); } catch (IllegalAccessException e) { throw new MigrationException(e); } catch (InvocationTargetException e) { throw new MigrationException(e); } catch (NoSuchMethodException e) { throw new MigrationException(e); } } } return entity; }
From source file:org.snaker.designer.utils.BeanUtil.java
/** * /*from w w w. j a va 2s.co m*/ * @param bean * @param propertyName ?? * @param value */ public static void setPropertyValue(Object bean, String propertyName, Object value) throws Exception { try { PropertyUtils.setProperty(bean, propertyName, value); } catch (Exception e) { throw e; } }
From source file:org.snowfk.util.ObjectUtil.java
@Deprecated public static void copyNotNull(Object src, Object dest) { try {/*from w w w . j a va 2 s.co m*/ PropertyDescriptor[] pds = PropertyUtils.getPropertyDescriptors(src); for (PropertyDescriptor pd : pds) { String name = pd.getName(); if (pd.getWriteMethod() != null) { Object value = PropertyUtils.getProperty(src, name); if (value != null) { PropertyUtils.setProperty(dest, name, value); } } } } catch (Exception e) { logger.error(e.getMessage()); throw (new RuntimeException(e)); } }
From source file:org.snowfk.util.ObjectUtil.java
/** * Copy all the properties are that not null, or not empty (for List and * array objects) to the dest object./* w w w. j a v a 2 s.co m*/ * * @param src * @param dest */ @Deprecated public static void copyNotNullNotEmpty(Object src, Object dest) { try { PropertyDescriptor[] pds = PropertyUtils.getPropertyDescriptors(src); for (PropertyDescriptor pd : pds) { String name = pd.getName(); if (pd.getWriteMethod() != null) { Object value = PropertyUtils.getProperty(src, name); boolean copy = true; if (value == null) { copy = false; } else if (value instanceof List && ((List) value).size() < 1) { copy = false; } else if (value.getClass().isArray() && ((Object[]) value).length < 1) { copy = false; } if (copy) { PropertyUtils.setProperty(dest, name, value); } } } // BeanUtils.copyProperties(dest, src); } catch (Exception e) { logger.error(e.getMessage()); throw (new RuntimeException(e)); } }
From source file:org.sonar.plugins.web.checks.AbstractCheckTester.java
private void configureDefaultParams(AbstractPageCheck check, Rule rule) { WebRuleFinder ruleFinder = new WebRuleFinder(rule); ValidationMessages validationMessages = ValidationMessages.create(); ProfileDefinition profileDefinition; if (rule.getKey().equals("OGNLExpressionCheck")) { profileDefinition = new StrutsProfile(new XMLProfileParser(ruleFinder), ruleFinder); } else {//from w w w .jav a 2 s . co m profileDefinition = new DefaultWebProfile(new XMLProfileParser(ruleFinder)); } RulesProfile rulesProfile = profileDefinition.createProfile(validationMessages); ActiveRule activeRule = rulesProfile.getActiveRule(rule); assertNotNull("Could not find activeRule", activeRule); try { if (activeRule.getActiveRuleParams() != null) { for (ActiveRuleParam param : activeRule.getActiveRuleParams()) { Object value = PropertyUtils.getProperty(check, param.getRuleParam().getKey()); if (value instanceof Integer) { value = Integer.parseInt(param.getValue()); } else { value = param.getValue(); } PropertyUtils.setProperty(check, param.getRuleParam().getKey(), value); } } } catch (IllegalAccessException e) { throw new SonarException(e); } catch (InvocationTargetException e) { throw new SonarException(e); } catch (NoSuchMethodException e) { throw new SonarException(e); } }
From source file:org.sonar.plugins.xml.checks.AbstractCheckTester.java
private void configureDefaultParams(AbstractPageCheck check, Rule rule) { ValidationMessages validationMessages = ValidationMessages.create(); RulesProfile rulesProfile = getProfileDefinition().createProfile(validationMessages); ActiveRule activeRule = rulesProfile.getActiveRule(rule); assertNotNull("Could not find activeRule", activeRule); try {//from w ww . j a va2 s .co m if (activeRule.getActiveRuleParams() != null) { for (ActiveRuleParam param : activeRule.getActiveRuleParams()) { Object value = PropertyUtils.getProperty(check, param.getRuleParam().getKey()); if (value instanceof Integer) { value = Integer.parseInt(param.getValue()); } else { value = param.getValue(); } PropertyUtils.setProperty(check, param.getRuleParam().getKey(), value); } } } catch (IllegalAccessException e) { throw new SonarException(e); } catch (InvocationTargetException e) { throw new SonarException(e); } catch (NoSuchMethodException e) { throw new SonarException(e); } }