List of usage examples for org.apache.commons.beanutils PropertyUtils setSimpleProperty
public static void setSimpleProperty(Object bean, String name, Object value) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException
Set the value of the specified simple property of the specified bean, with no type conversions.
For more details see PropertyUtilsBean
.
From source file:com.mb.framework.util.property.PropertyUtilExt.java
/** * Copy property values from the origin bean to the destination bean for all * cases where the property names are the same. For each property, a * conversion is attempted as necessary. All combinations of standard * JavaBeans and DynaBeans as origin and destination are supported. * Properties that exist in the origin bean, but do not exist in the * destination bean (or are read-only in the destination bean) are silently * ignored./*from w w w . j a v a 2 s.co m*/ * <p> * In addition to the method with the same name in the * <code>org.apache.commons.beanutils.PropertyUtils</code> class this method * can also copy properties of the following types: * <ul> * <li>java.lang.Integer</li> * <li>java.lang.Double</li> * <li>java.lang.Long</li> * <li>java.lang.Short</li> * <li>java.lang.Float</li> * <li>java.lang.String</li> * <li>java.lang.Boolean</li> * <li>java.sql.Date</li> * <li>java.sql.Time</li> * <li>java.sql.Timestamp</li> * <li>java.math.BigDecimal</li> * <li>a container-managed relations field.</li> * </ul> * * @param dest * Destination bean whose properties are modified * @param orig * Origin bean whose properties are retrieved * @throws IllegalAccessException * if the caller does not have access to the property accessor * method * @throws InvocationTargetException * if the property accessor method throws an exception * @throws NoSuchMethodException * if an accessor method for this propety cannot be found * @throws ClassNotFoundException * if an incorrect relations class mapping exists. * @throws InstantiationException * if an object of the mapped relations class can not be * constructed. */ public static void copyProperties(Object dest, Object orig) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException, Exception { PropertyDescriptor origDescriptors[] = PropertyUtils.getPropertyDescriptors(orig); for (int i = 0; i < origDescriptors.length; i++) { String name = origDescriptors[i].getName(); if (PropertyUtils.getPropertyDescriptor(dest, name) != null) { Object origValue = PropertyUtils.getSimpleProperty(orig, name); String origParamType = origDescriptors[i].getPropertyType().getName(); try { // edited // if (origValue == null)throw new NullPointerException(); PropertyUtils.setSimpleProperty(dest, name, origValue); } catch (Exception e) { try { String destParamType = PropertyUtils.getPropertyType(dest, name).getName(); if (origValue instanceof String) { if (destParamType.equals("java.lang.Integer")) { Integer intValue = null; String sValue = ((String) origValue).trim(); if (sValue.length() > 0) { intValue = new Integer(sValue); } PropertyUtils.setSimpleProperty(dest, name, intValue); } else if (destParamType.equals("java.lang.Byte")) { Byte byteValue = null; String sValue = ((String) origValue).trim(); if (sValue.length() > 0) { byteValue = new Byte(sValue); } PropertyUtils.setSimpleProperty(dest, name, byteValue); } else if (destParamType.equals("java.lang.Double")) { Double doubleValue = null; String sValue = ((String) origValue).trim(); if (sValue.length() > 0) { doubleValue = new Double(sValue); } PropertyUtils.setSimpleProperty(dest, name, doubleValue); } else if (destParamType.equals("java.lang.Long")) { Long longValue = null; String sValue = ((String) origValue).trim(); if (sValue.length() > 0) { longValue = new Long(sValue); } PropertyUtils.setSimpleProperty(dest, name, longValue); } else if (destParamType.equals("java.lang.Short")) { Short shortValue = null; String sValue = ((String) origValue).trim(); if (sValue.length() > 0) { shortValue = new Short(sValue); } PropertyUtils.setSimpleProperty(dest, name, shortValue); } else if (destParamType.equals("java.lang.Float")) { Float floatValue = null; String sValue = ((String) origValue).trim(); if (sValue.length() > 0) { floatValue = new Float(sValue); } PropertyUtils.setSimpleProperty(dest, name, floatValue); } else if (destParamType.equals("java.sql.Date")) { java.sql.Date dateValue = null; String sValue = ((String) origValue).trim(); if (sValue.length() > 0) { dateValue = new java.sql.Date(DATE_FORMATTER.parse(sValue).getTime()); } PropertyUtils.setSimpleProperty(dest, name, dateValue); } else if (destParamType.equals("java.sql.Time")) { java.sql.Time dateValue = null; String sValue = ((String) origValue).trim(); if (sValue.length() > 0) { dateValue = new java.sql.Time(TIME_FORMATTER.parse(sValue).getTime()); } PropertyUtils.setSimpleProperty(dest, name, dateValue); } else if (destParamType.equals("java.sql.Timestamp")) { java.sql.Timestamp dateValue = null; String sValue = ((String) origValue).trim(); if (sValue.length() > 0) { dateValue = new java.sql.Timestamp(TIMESTAMP_FORMATTER.parse(sValue).getTime()); } PropertyUtils.setSimpleProperty(dest, name, dateValue); } else if (destParamType.equals("java.lang.Boolean")) { Boolean bValue = null; String sValue = ((String) origValue).trim(); if (sValue.length() > 0) { bValue = Boolean.valueOf(sValue); } PropertyUtils.setSimpleProperty(dest, name, bValue); } else if (destParamType.equals("java.math.BigDecimal")) { BigDecimal bdValue = null; String sValue = ((String) origValue).trim(); if (sValue.length() > 0) { bdValue = new BigDecimal(sValue); } PropertyUtils.setSimpleProperty(dest, name, bdValue); } } else if ((origValue != null) && (destParamType.equals("java.lang.String"))) { // we're transferring a business-layer value object // into a String-based Struts form bean.. if ("java.sql.Date".equals(origParamType)) { PropertyUtils.setSimpleProperty(dest, name, DATE_FORMATTER.format(origValue)); } else if ("java.sql.Timestamp".equals(origParamType)) { PropertyUtils.setSimpleProperty(dest, name, TIMESTAMP_FORMATTER.format(origValue)); } else if ("java.sql.Blob".equals(origParamType)) { // convert a Blob to a String.. Blob blob = (Blob) origValue; BufferedInputStream bin = null; try { int bytesRead; StringBuffer result = new StringBuffer(); byte[] buffer = new byte[READ_BUFFER_LENGTH]; bin = new BufferedInputStream(blob.getBinaryStream()); do { bytesRead = bin.read(buffer); if (bytesRead != -1) { result.append(new String(buffer, 0, bytesRead)); } } while (bytesRead == READ_BUFFER_LENGTH); PropertyUtils.setSimpleProperty(dest, name, result.toString()); } finally { if (bin != null) try { bin.close(); } catch (IOException ignored) { } } } else { PropertyUtils.setSimpleProperty(dest, name, origValue.toString()); } } } catch (Exception e2) { throw e2; } } } } }
From source file:com.square.core.model.util.FormatObjectHibernateListener.java
/** * Formate un objet.//from w ww .j a v a 2s . c om * @param objet l'objet formater. */ private void udpateEntity(Object entity, EntityPersister persister, Object[] state) { if (classesConcernees.contains(entity.getClass())) { final String[] properties = persister.getPropertyNames(); for (int i = 0; i < properties.length; i++) { if (!champsAExclure.contains(properties[i])) { if (state[i] instanceof String && state[i] != null) { final String value = formaterChaine(state[i].toString()).toLowerCase(); state[i] = value; try { PropertyUtils.setSimpleProperty(entity, properties[i], value); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } catch (NoSuchMethodException e) { e.printStackTrace(); } } } } } }
From source file:arena.utils.ReflectionUtils.java
public static void setAttributeUsingSetter(String attributeName, Object entity, Object attribute) { try {//from ww w . j av a 2 s. c om PropertyUtils.setSimpleProperty(entity, attributeName, attribute); } catch (NoSuchMethodException err) { LogFactory.getLog(ReflectionUtils.class).error(err); throw new RuntimeException("Error in setter", err); } catch (IllegalAccessException err) { LogFactory.getLog(ReflectionUtils.class).error(err); throw new RuntimeException("Error in setter", err); } catch (InvocationTargetException err) { LogFactory.getLog(ReflectionUtils.class).error(err); throw new RuntimeException("Error in setter", err); } }
From source file:com.bstek.dorado.data.entity.PropertyPathUtils.java
@SuppressWarnings({ "unchecked", "rawtypes" }) public static void setValueByPath(EntityDataType dataType, Object object, String propertyPath, Object value) throws Exception { String[] paths = StringUtils.split(propertyPath, '.'); Object currentEntity = object; EntityDataType currentDataType = dataType; for (int i = 0; i < paths.length - 1; i++) { String path = paths[i];/*from w w w . j av a2 s.c o m*/ Object tempEntity; boolean isMap = currentEntity instanceof Map; if (EntityUtils.isEntity(currentEntity)) { tempEntity = EntityUtils.getValue(currentEntity, path); } else if (currentEntity instanceof Map) { tempEntity = ((Map) currentEntity).get(path); } else { tempEntity = PropertyUtils.getSimpleProperty(currentEntity, path); } if (tempEntity == null) { Class<?> subEntityType = null; if (currentDataType != null) { PropertyDef propertyDef = currentDataType.getPropertyDef(path); if (propertyDef != null) { DataType propertyDataType = propertyDef.getDataType(); if (propertyDataType instanceof EntityDataType) { currentDataType = (EntityDataType) propertyDataType; subEntityType = currentDataType.getCreationType(); if (subEntityType == null) { subEntityType = currentDataType.getMatchType(); } } } } else if (isMap) { tempEntity = new HashMap(); currentDataType = null; } if (tempEntity == null) { if (subEntityType == null) { subEntityType = PropertyUtils.getPropertyType(currentEntity, path); } if (subEntityType.isAssignableFrom(Map.class)) { tempEntity = new HashMap(); } else if (!subEntityType.isInterface()) { tempEntity = subEntityType.newInstance(); } currentDataType = null; } if (tempEntity != null) { if (isMap) { ((Map) currentEntity).put(path, tempEntity); } else { PropertyUtils.setSimpleProperty(currentEntity, path, tempEntity); } } else { throw new IllegalArgumentException("Can not write value to [" + StringUtils.join(paths, '.') + "] on [" + ObjectUtils.identityToString(object) + "]."); } } currentEntity = tempEntity; } String path = paths[paths.length - 1]; if (EntityUtils.isEntity(currentEntity)) { EntityUtils.setValue(currentEntity, path, value); } else if (currentEntity instanceof Map) { ((Map) currentEntity).put(path, value); } else { PropertyUtils.setSimpleProperty(currentEntity, path, value); } }
From source file:com.qrmedia.commons.persistence.hibernate.clone.property.AbstractValueAwareCollectionCloner.java
@SuppressWarnings("unchecked") @Override// www .j a va2 s.c om protected <T> boolean cloneValue(Object source, Object target, String propertyName, Object propertyValue, HibernateEntityGraphCloner entityGraphCloner) throws IllegalArgumentException { if (!(propertyValue instanceof Collection<?>)) { return false; } Collection<T> clonedCollection = cloneCollection(source, target, propertyName, (Collection<T>) propertyValue, entityGraphCloner); boolean cloneSuccessful = (clonedCollection != null); /* * The cloned collection should only be linked to the target if the cloner * actually processed it! */ if (cloneSuccessful) { try { PropertyUtils.setSimpleProperty(target, propertyName, clonedCollection); } catch (Exception exception) { throw new IllegalArgumentException("Unable to set collection '" + propertyName + "' on " + target + " due to " + exception.getClass().getSimpleName() + ": " + exception.getMessage()); } } return cloneSuccessful; }
From source file:com.bstek.dorado.config.xml.SubNodeToPropertyParser.java
public void execute(Object object, CreationContext context) throws Exception { if (object instanceof Definition) { if (element instanceof Operation) { if (element instanceof DefinitionInitOperation) { ((DefinitionInitOperation) element).execute(object, context); } else if (object instanceof ObjectDefinition) { ((ObjectDefinition) object).addInitOperation((Operation) element); }/*from w w w . j a va 2s .c o m*/ } else { PropertyDescriptor propertyDescriptor = PropertyUtils.getPropertyDescriptor(object, property); if (propertyDescriptor != null && propertyDescriptor.getWriteMethod() != null && !NATIVE_DEFINITION_PROPERTIES.contains(property)) { PropertyUtils.setSimpleProperty(object, property, element); } else if (object instanceof ObjectDefinition) { ((ObjectDefinition) object).setProperty(property, element); } } } else { PropertyUtils.setSimpleProperty(object, property, element); } }
From source file:fr.mtlx.odm.ClassAssistant.java
public void setSimpleProperty(final String propertyName, T entry, final Object singleValue) throws MappingException { try {/*from w ww . j av a 2 s. c o m*/ PropertyUtils.setSimpleProperty(entry, propertyName, singleValue); } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) { throw new MappingException(e); } }
From source file:cern.accsoft.steering.jmad.domain.knob.bean.BeanPropertyKnob.java
/** * set the property to a given value/*from w w w . j a v a 2s. c o m*/ * * @param value the value to set the property to */ private void setBeanValue(double value) { try { PropertyUtils.setSimpleProperty(getBean(), this.propertyName, value); } catch (Exception e) { LOGGER.error("Cannot set property '" + this.propertyName + "' of bean '" + getBean() + "'.", e); } }
From source file:com.sqewd.open.dal.core.persistence.db.EntityHelper.java
public static <T extends AbstractEntity> Object createListInstance(final AbstractEntity entity, final StructAttributeReflect attr) throws Exception { Object vo = PropertyUtils.getSimpleProperty(entity, attr.Field.getName()); if (vo == null) { vo = new ArrayList<T>(); PropertyUtils.setSimpleProperty(entity, attr.Field.getName(), vo); }//from w w w.j a v a2 s . c o m return vo; }
From source file:com.bstek.dorado.config.xml.CollectionToPropertyParser.java
@SuppressWarnings({ "unchecked", "rawtypes" }) public void execute(Object object, CreationContext context) throws Exception { Collection collection = null; boolean useNativeProperty = false, isDefinition = false, shouldSetCollection = false; if (object instanceof Definition) { isDefinition = true;/*from w w w. j a v a2 s. com*/ try { useNativeProperty = Collection.class .isAssignableFrom(PropertyUtils.getPropertyType(object, property)); } catch (Exception e) { // do nothing } if (!useNativeProperty) { collection = (Collection) ((Definition) object).getProperty(property); if (collection == null) { collection = new DefinitionSupportedList(); shouldSetCollection = true; } } } else { useNativeProperty = true; } if (useNativeProperty) { collection = (Collection) PropertyUtils.getSimpleProperty(object, property); if (collection == null) { if (List.class.isAssignableFrom(PropertyUtils.getPropertyType(object, property))) { collection = new ArrayList(); } else { collection = new HashSet(); } shouldSetCollection = true; } } if (collection != null) { if (isDefinition) { for (Object element : elements) { if (element instanceof Operation) { if (element instanceof DefinitionInitOperation) { ((DefinitionInitOperation) element).execute(object, context); } else { ((Definition) object).addInitOperation((Operation) element); } } else { collection.add(element); } } } else { collection.addAll(elements); } if (shouldSetCollection && !collection.isEmpty()) { if (useNativeProperty) { PropertyUtils.setSimpleProperty(object, property, collection); } else { ((Definition) object).setProperty(property, collection); } } } }