List of usage examples for org.apache.commons.beanutils PropertyUtils isWriteable
public static boolean isWriteable(Object bean, String name)
Return true
if the specified property name identifies a writeable property on the specified bean; otherwise, return false
.
For more details see PropertyUtilsBean
.
From source file:org.ms123.common.data.MultiOperations.java
private static void setStateToNew(Object object) { try {/*from w ww.j a v a 2s . com*/ boolean hasState = PropertyUtils.isWriteable(object, STATE_FIELD); if (hasState) { PropertyUtils.setProperty(object, STATE_FIELD, null); } } catch (Exception e) { } }
From source file:org.openscada.da.server.spring.DataItemInputOutputProperty.java
@Override public void afterPropertiesSet() throws Exception { // contract//from ww w .j av a2s . c om Assert.notNull(this.bean, "'bean' must not be null"); Assert.notNull(this.property, "'property' must not be null"); Assert.isTrue(PropertyUtils.isReadable(this.bean, this.property)); Assert.isTrue(PropertyUtils.isWriteable(this.bean, this.property)); final Object value = PropertyUtils.getProperty(this.bean, this.property); updateData(Variant.valueOf(value), new HashMap<String, Variant>(), AttributeMode.SET); notifyData(Variant.valueOf(value), new HashMap<String, Variant>(), true); }
From source file:org.rhq.enterprise.server.plugins.rhnhosted.certificate.SimpleExtractor.java
/** * {@inheritDoc}// w w w .j a v a 2 s. co m * @throws JDOMException */ public void extract(Certificate target, Element field) throws JDOMException { if (!PropertyUtils.isWriteable(target, propertyName)) { throw new JDOMException("Property " + propertyName + " is not writable in target " + target); } try { BeanUtils.setProperty(target, propertyName, field.getTextTrim()); } catch (IllegalAccessException e) { throw new JDOMException("Could not set value of property " + propertyName, e); } catch (InvocationTargetException e) { throw new JDOMException("Could not set value of property " + propertyName, e); } }
From source file:org.thiesen.helenaorm.HelenaDAO.java
private T applyColumns(final String key, final Iterable<Column> slice) { try {/* ww w . j a v a2s. c om*/ final T newInstance = _clz.newInstance(); PropertyUtils.setProperty(newInstance, _keyPropertyDescriptor.getName(), _typeConverter.convertByteArrayToValueObject( _keyPropertyDescriptor.getReadMethod().getReturnType(), _typeConverter.stringToBytes(key))); for (final Column c : slice) { final String name = _typeConverter.bytesToString(c.name); if (PropertyUtils.isWriteable(newInstance, name)) { final PropertyDescriptor propertyDescriptor = PropertyUtils.getPropertyDescriptor(newInstance, name); final Class<?> returnType = propertyDescriptor.getReadMethod().getReturnType(); PropertyUtils.setProperty(newInstance, name, _typeConverter.convertByteArrayToValueObject(returnType, c.value)); } } return newInstance; } catch (final InstantiationException e) { throw new HelenaRuntimeException("Could not instanciate " + _clz.getName(), e); } catch (final IllegalAccessException e) { throw new HelenaRuntimeException("Could not instanciate " + _clz.getName(), e); } catch (final InvocationTargetException e) { throw new HelenaRuntimeException(e); } catch (final NoSuchMethodException e) { throw new HelenaRuntimeException(e); } }
From source file:org.tinygroup.commons.beanutil.BeanUtil.java
public static Object deepCopy(Object orig) throws Exception { Object dest = orig.getClass().newInstance(); PropertyDescriptor[] origDescriptors = PropertyUtils.getPropertyDescriptors(orig); for (PropertyDescriptor propertyDescriptor : origDescriptors) { String name = propertyDescriptor.getName(); if (PropertyUtils.isReadable(orig, name) && PropertyUtils.isWriteable(dest, name)) { Object value = PropertyUtils.getSimpleProperty(orig, name); Object valueDest = null; if (value != null && canDeepCopyObject(value)) { if (value instanceof Collection) { Collection coll = (Collection) value; Collection newColl = createApproximateCollection(value); Iterator it = coll.iterator(); while (it.hasNext()) { newColl.add(deepCopy(it.next())); }//from w ww. j a v a 2 s . co m valueDest = newColl; } else if (value.getClass().isArray()) { Object[] values = (Object[]) value; Object[] newValues = new Object[values.length]; for (int i = 0; i < newValues.length; i++) { newValues[i] = deepCopy(values[i]); } valueDest = newValues; } else if (value instanceof Map) { Map map = (Map) value; Map newMap = createApproximateMap(map); for (Object key : map.keySet()) { newMap.put(key, deepCopy(map.get(key))); } valueDest = newMap; } else { valueDest = deepCopy(value); } } else { valueDest = value; } PropertyUtils.setSimpleProperty(dest, name, valueDest); } } return dest; }
From source file:org.tinygroup.jdbctemplatedslsession.SimpleDslSession.java
private <T> Map<String, Object> convertMap(T param) throws Exception { Map<String, Object> description = new LinkedHashMap<String, Object>(); PropertyDescriptor[] descriptors = PropertyUtils.getPropertyDescriptors(param); for (int i = 0; i < descriptors.length; i++) { String name = descriptors[i].getName(); if (PropertyUtils.isReadable(param, name) && PropertyUtils.isWriteable(param, name)) { if (descriptors[i].getReadMethod() != null) { Object value = PropertyUtils.getProperty(param, name); description.put(name, value); description.put(nameStrategy.getFieldName(name), value); }//from w w w. j a va 2 s .co m } } return description; }
From source file:pt.ist.maidSyncher.domain.SynchableObject.java
/** * //from w w w . j a va 2 s . com * @param orig * @return a collection with the {@link PropertyDescriptor} s that changed * @throws IllegalAccessException * @throws InvocationTargetException * @throws NoSuchMethodException * @throws TaskNotVisibleException */ public Collection<String> copyPropertiesFrom(Object orig) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException, TaskNotVisibleException { Set<PropertyDescriptor> propertyDescriptorsThatChanged = new HashSet<PropertyDescriptor>(); Object dest = this; if (orig == null) { throw new IllegalArgumentException("No origin bean specified"); } PropertyDescriptor origDescriptors[] = PropertyUtils.getPropertyDescriptors(orig); for (PropertyDescriptor origDescriptor : origDescriptors) { String name = origDescriptor.getName(); if (PropertyUtils.isReadable(orig, name)) { if (PropertyUtils.isWriteable(dest, name)) { Object valueDest = PropertyUtils.getSimpleProperty(dest, name); Object valueOrigin = PropertyUtils.getSimpleProperty(orig, name); if (valueOrigin != null && valueOrigin.getClass().getPackage().getName() .equalsIgnoreCase("org.eclipse.egit.github.core")) continue; //let's skip the properties with egit core objects (they shall be copied from a custom overriden version of this method) if (valueOrigin instanceof Date) valueOrigin = new DateTime(valueOrigin); if (Objects.equal(valueDest, valueOrigin) == false) propertyDescriptorsThatChanged.add(origDescriptor); try { //let's see if this is actually a Date, if so, let's convert it PropertyUtils.setSimpleProperty(dest, name, valueOrigin); } catch (IllegalArgumentException ex) { throw new Error("setSimpleProperty returned an exception, dest: " + dest.getClass().getName() + " oid: " + ((DomainObject) dest).getExternalId() + " name : " + name + " valueOrig: " + valueOrigin, ex); } LOGGER.trace("Copied property " + name + " from " + orig.getClass().getName() + " object to a " + dest.getClass().getName() + " oid: " + getExternalId()); } } } return Collections2.transform(propertyDescriptorsThatChanged, new Function<PropertyDescriptor, String>() { @Override public String apply(PropertyDescriptor propertyDescriptor) { if (propertyDescriptor == null) { return null; } return propertyDescriptor.getName(); } }); }
From source file:pt.ist.maidSyncher.domain.SynchableObject.java
public void copyPropertiesTo(Object dest) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException, TaskNotVisibleException { Set<PropertyDescriptor> propertyDescriptorsThatChanged = new HashSet<PropertyDescriptor>(); Object orig = this; checkNotNull(dest);// w w w. jav a 2 s. c om PropertyDescriptor origDescriptors[] = PropertyUtils.getPropertyDescriptors(orig); for (PropertyDescriptor origDescriptor : origDescriptors) { String name = origDescriptor.getName(); if (PropertyUtils.isReadable(orig, name)) { PropertyDescriptor destDescriptor = PropertyUtils.getPropertyDescriptor(dest, origDescriptor.getName()); if (PropertyUtils.isWriteable(dest, name) || (destDescriptor != null && MiscUtils.getWriteMethodIncludingFlowStyle(destDescriptor, dest.getClass()) != null)) { Object valueDest = PropertyUtils.getSimpleProperty(dest, name); Object valueOrigin = PropertyUtils.getSimpleProperty(orig, name); LOGGER.debug("OrigDescriptor PropertyType: " + origDescriptor.getPropertyType().getName()); // System.out.println("OrigDescriptor PropertyType: " + origDescriptor.getPropertyType().getName()); //let's ignore the properties were the values are our domain packages if (valueOrigin != null && (SynchableObject.class.isAssignableFrom(valueOrigin.getClass()))) { // System.out.println("Skipping"); continue; //let's skip these properties } if (SynchableObject.class.isAssignableFrom(origDescriptor.getPropertyType())) { // System.out.println("Skipping"); continue; } if (origDescriptor instanceof IndexedPropertyDescriptor) { IndexedPropertyDescriptor indexedPropertyDescriptor = (IndexedPropertyDescriptor) origDescriptor; // System.out.println("OrigDescriptor IndexedPropertyDescriptor: " + indexedPropertyDescriptor.getName()); if (SynchableObject.class .isAssignableFrom(indexedPropertyDescriptor.getIndexedPropertyType())) { // System.out.println("Skipping"); continue; } } //let's ignore all of the dates - as they should be filled by //the system if (valueOrigin instanceof LocalTime) continue; if (Objects.equal(valueDest, valueOrigin) == false) propertyDescriptorsThatChanged.add(origDescriptor); try { if (PropertyUtils.isWriteable(dest, name) == false) { //let's use the flow version Class<?> origPropertyType = origDescriptor.getPropertyType(); Method writeMethodIncludingFlowStyle = MiscUtils .getWriteMethodIncludingFlowStyle(destDescriptor, dest.getClass()); if (Arrays.asList(writeMethodIncludingFlowStyle.getParameterTypes()) .contains(origPropertyType)) { writeMethodIncludingFlowStyle.invoke(dest, valueOrigin); } else { continue; } } else { PropertyUtils.setSimpleProperty(dest, name, valueOrigin); } } catch (IllegalArgumentException ex) { throw new Error("setSimpleProperty returned an exception, dest: " + dest.getClass().getName() + " name : " + name + " valueOrig: " + valueOrigin, ex); } LOGGER.trace("Copied property " + name + " from " + orig.getClass().getName() + " object to a " + dest.getClass().getName() + " oid: " + getExternalId()); } // System.out.println("--"); } } }
From source file:see.properties.impl.BeanPropertyResolver.java
@Override public boolean canSet(@Nullable Object target, @Nonnull PropertyAccess propertyAccess, Object value) { return isBeanProperty(propertyAccess) && PropertyUtils.isWriteable(target, getPropertyName(propertyAccess)); }