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:jp.go.nict.langrid.p2pgridbasis.data.langrid.converter.ConvertUtil.java
public static void decode(DataAttributes from, Object to) throws DataConvertException { setLangridConverter();/*from w ww . ja v a 2s. c o m*/ logger.debug("##### decode #####"); try { for (PropertyDescriptor descriptor : PropertyUtils.getPropertyDescriptors(to)) { logger.debug( "Name : " + descriptor.getName() + " / PropertyType : " + descriptor.getPropertyType()); Object value = from.getValue(descriptor.getName()); if (PropertyUtils.isWriteable(to, descriptor.getName()) && value != null) { //Write OK try { Converter converter = ConvertUtils.lookup(descriptor.getPropertyType()); if (!ignoreProps.contains(descriptor.getName())) { if (converter == null) { logger.error("no converter is registered : " + descriptor.getName() + " " + descriptor.getPropertyType()); } else { Object obj = converter.convert(descriptor.getPropertyType(), value); PropertyUtils.setProperty(to, descriptor.getName(), obj); } } } catch (IllegalArgumentException e) { e.printStackTrace(); logger.info(e.getMessage()); } catch (NoSuchMethodException e) { logger.info(e.getMessage()); } } else { if (value != null) { //Write NG logger.debug("isWriteable = false"); } else { //Write NG logger.debug("value = null"); } } } } catch (IllegalAccessException e) { throw new DataConvertException(e); } catch (InvocationTargetException e) { throw new DataConvertException(e); } }
From source file:nl.strohalm.cyclos.utils.access.PermissionsDescriptor.java
/** * Recursively fetch the path until the member *//*w w w. j av a 2 s .c o m*/ private Element fetchMember(Object bean, final String path) { final String name = path; String first = PropertyHelper.firstProperty(name); String nested = PropertyHelper.nestedPath(name); String previousProperty = null; Object previousBean = null; while (bean != null && first != null) { if (bean instanceof Entity) { try { final Entity entity = (Entity) bean; if (!entity.isTransient()) { bean = fetchService.fetch(entity, RelationshipHelper.forName(first)); } } catch (final UnexpectedEntityException e) { // Ignore - It's just null or transient } } final Object value = PropertyHelper.get(bean, first); if (PropertyUtils.isWriteable(bean, first)) { PropertyHelper.set(bean, first, value); } previousBean = bean; previousProperty = first; // Process the next property in the properties list bean = value; first = PropertyHelper.firstProperty(nested); nested = PropertyHelper.nestedPath(nested); } if (bean instanceof Entity) { try { final Entity entity = (Entity) bean; if (!entity.isTransient()) { bean = fetchService.fetch(entity); if (previousBean != null) { if (PropertyUtils.isWriteable(previousBean, previousProperty)) { PropertyHelper.set(previousBean, previousProperty, bean); } } } } catch (final UnexpectedEntityException e) { // Ignore - It's just null or transient } } return (Element) bean; }
From source file:nl.tue.bimserver.citygml.CityGmlSerializer.java
private <T extends AbstractCityObject> T buildBoundarySurface(IfcProduct ifcProduct, T cityObject) throws SerializerException { setName(cityObject.getName(), ifcProduct.getName()); setGlobalId(cityObject, ifcProduct); MultiSurface multiSurface = gml.createMultiSurface(); {// w ww . j a va 2 s.com CompositeSurface compositeSurface = gml.createCompositeSurface(); setGeometry(compositeSurface, ifcProduct); materialManager.assign(compositeSurface, ifcProduct); multiSurface.addSurfaceMember(gml.createSurfaceProperty(compositeSurface)); } LinkedList<IfcObjectDefinition> decompose = new LinkedList<IfcObjectDefinition>( Collections.singletonList(ifcProduct)); while (!decompose.isEmpty()) { for (IfcRelDecomposes ifcRelDecomposes : decompose.removeFirst().getIsDecomposedBy()) { for (IfcObjectDefinition ifcObjectDef : ifcRelDecomposes.getRelatedObjects()) { CompositeSurface compositeSurface = gml.createCompositeSurface(); setGeometry(compositeSurface, ifcObjectDef); materialManager.assign(compositeSurface, ifcObjectDef); multiSurface.addSurfaceMember(gml.createSurfaceProperty(compositeSurface)); decompose.add(ifcObjectDef); } } } MultiSurfaceProperty multiSurfaceProperty = gml.createMultiSurfaceProperty(multiSurface); try { if (PropertyUtils.isWriteable(cityObject, "lod4MultiSurface")) { PropertyUtils.setProperty(cityObject, "lod4MultiSurface", multiSurfaceProperty); } else { PropertyUtils.setProperty(cityObject, "lod4Geometry", multiSurfaceProperty); } } catch (Exception e) { e.printStackTrace(); } return cityObject; }
From source file:org.alfresco.repo.content.transform.ComplexContentTransformer.java
/** * Sets any transformation option overrides it can. */// ww w . j av a2 s .c o m private void overrideTransformationOptions(TransformationOptions options) { // Set any transformation options overrides if we can if (options != null && transformationOptionOverrides != null) { for (String key : transformationOptionOverrides.keySet()) { if (PropertyUtils.isWriteable(options, key)) { try { PropertyDescriptor pd = PropertyUtils.getPropertyDescriptor(options, key); Class<?> propertyClass = pd.getPropertyType(); Object value = transformationOptionOverrides.get(key); if (value != null) { if (propertyClass.isInstance(value)) { // Nothing to do } else if (value instanceof String && propertyClass.isInstance(Boolean.TRUE)) { // Use relaxed converter value = TransformationOptions.relaxedBooleanTypeConverter.convert((String) value); } else { value = DefaultTypeConverter.INSTANCE.convert(propertyClass, value); } } PropertyUtils.setProperty(options, key, value); } catch (MethodNotFoundException mnfe) { } catch (NoSuchMethodException nsme) { } catch (InvocationTargetException ite) { } catch (IllegalAccessException iae) { } } else { logger.warn("Unable to set override Transformation Option " + key + " on " + options); } } } }
From source file:org.androidtransfuse.processor.Merger.java
private <T extends Mergeable> T mergeMergeable(Class<? extends T> targetClass, T target, T source) throws MergerException { try {/*from ww w. ja v a2s . c o m*/ BeanInfo beanInfo = Introspector.getBeanInfo(targetClass); PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors(); for (PropertyDescriptor propertyDescriptor : propertyDescriptors) { Method getter = propertyDescriptor.getReadMethod(); Method setter = propertyDescriptor.getWriteMethod(); String propertyName = propertyDescriptor.getDisplayName(); if (PropertyUtils.isWriteable(target, propertyName)) { //check for mergeCollection MergeCollection mergeCollection = findAnnotation(MergeCollection.class, getter, setter); if (Collection.class.isAssignableFrom(propertyDescriptor.getPropertyType())) { PropertyUtils.setProperty(target, propertyName, mergeList(mergeCollection, propertyName, target, source)); } //check for merge Merge mergeAnnotation = findAnnotation(Merge.class, getter, setter); PropertyUtils.setProperty(target, propertyName, mergeProperties(mergeAnnotation, propertyName, target, source)); } } } catch (NoSuchMethodException e) { throw new MergerException("NoSuchMethodException while trying to merge", e); } catch (IntrospectionException e) { throw new MergerException("IntrospectionException while trying to merge", e); } catch (IllegalAccessException e) { throw new MergerException("IllegalAccessException while trying to merge", e); } catch (InvocationTargetException e) { throw new MergerException("InvocationTargetException while trying to merge", e); } return target; }
From source file:org.andromda.presentation.gui.FormPopulator.java
/** * Copies the properties from the <code>fromForm</code> to the <code>toForm</code>. Only passes not-null values to the toForm. * * @param fromForm the form from which we're populating * @param toForm the form to which we're populating * @param override whether or not properties that have already been copied, should be overridden. *//*from w w w . j a v a 2 s . c o m*/ @SuppressWarnings("unchecked") // apache commons-beanutils PropertyUtils has no generics public static final void populateForm(final Object fromForm, final Object toForm, boolean override) { if (fromForm != null && toForm != null) { try { final Map<String, Object> parameters = PropertyUtils.describe(fromForm); for (final Iterator<String> iterator = parameters.keySet().iterator(); iterator.hasNext();) { final String name = iterator.next(); if (PropertyUtils.isWriteable(toForm, name)) { // - the property name used for checking whether or not the property value has been set final String isSetPropertyName = name + "Set"; Boolean isToFormPropertySet = null; Boolean isFromFormPropertySet = null; // - only if override isn't true do we care whether or not the to property has been populated if (!override) { if (PropertyUtils.isReadable(toForm, isSetPropertyName)) { isToFormPropertySet = (Boolean) PropertyUtils.getProperty(toForm, isSetPropertyName); } } // - only if override is set to true, we check to see if the from form property has been set if (override) { if (PropertyUtils.isReadable(fromForm, isSetPropertyName)) { isFromFormPropertySet = (Boolean) PropertyUtils.getProperty(fromForm, isSetPropertyName); } } if (!override || (override && isFromFormPropertySet != null && isFromFormPropertySet.booleanValue())) { if (override || (isToFormPropertySet == null || !isToFormPropertySet.booleanValue())) { final PropertyDescriptor toDescriptor = PropertyUtils.getPropertyDescriptor(toForm, name); if (toDescriptor != null) { Object fromValue = parameters.get(name); // - only populate if not null if (fromValue != null) { final PropertyDescriptor fromDescriptor = PropertyUtils .getPropertyDescriptor(fromForm, name); // - only attempt to set if the types match if (fromDescriptor.getPropertyType() == toDescriptor.getPropertyType()) { PropertyUtils.setProperty(toForm, name, fromValue); } } } } } } } } catch (final Throwable throwable) { throw new RuntimeException(throwable); } } }
From source file:org.andromda.presentation.gui.FormPopulator.java
/** * Populates the form from the given map of properties. If a matching property is null or an empty * string, then null is placed on the form. * * @param form the form to populate.//from w w w . j a va 2 s . c o m * @param formatters any date or time formatters. * @param properties the properties to populate from. * @param ignoreProperties names of any properties to ignore when it comes to populating on the form. * @param override whether or not to override properties already set on the given form. * @param assignableTypesOnly whether or not copying should be attempted only if the property types are assignable. */ @SuppressWarnings("unchecked") // apache commons-beanutils PropertyUtils has no generics public static final void populateFormFromPropertyMap(final Object form, final Map<String, DateFormat> formatters, final Map<String, ?> properties, final String[] ignoreProperties, boolean override, boolean assignableTypesOnly) { if (properties != null) { try { final Collection<String> ignoredProperties = ignoreProperties != null ? Arrays.asList(ignoreProperties) : new ArrayList<String>(); final Map<String, Object> formProperties = PropertyUtils.describe(form); for (final Iterator<String> iterator = formProperties.keySet().iterator(); iterator.hasNext();) { final String name = iterator.next(); if (PropertyUtils.isWriteable(form, name) && !ignoredProperties.contains(name)) { final PropertyDescriptor descriptor = PropertyUtils.getPropertyDescriptor(form, name); if (descriptor != null) { boolean populateProperty = true; if (!override) { final String isSetPropertyName = name + "Set"; if (PropertyUtils.isReadable(form, isSetPropertyName)) { final Boolean isPropertySet = (Boolean) PropertyUtils.getProperty(form, isSetPropertyName); if (isPropertySet.booleanValue()) { populateProperty = false; } } } if (populateProperty) { final Object property = properties.get(name); // - only convert if the string is not empty if (property != null) { Object value = null; if (property instanceof String) { final String propertyAsString = (String) property; if (propertyAsString.trim().length() > 0) { DateFormat formatter = formatters != null ? formatters.get(name) : null; // - if the formatter is available we use that, otherwise we attempt to convert if (formatter != null) { try { value = formatter.parse(propertyAsString); } catch (ParseException parseException) { // - try the default formatter (handles the default java.util.Date.toString() format) formatter = formatters != null ? formatters.get(null) : null; value = formatter.parse(propertyAsString); } } else { value = ConvertUtils.convert(propertyAsString, descriptor.getPropertyType()); } } // - don't attempt to set null on primitive fields if (value != null || !descriptor.getPropertyType().isPrimitive()) { PropertyUtils.setProperty(form, name, value); } } else { value = property; try { if (!assignableTypesOnly || descriptor.getPropertyType() .isAssignableFrom(value.getClass())) { PropertyUtils.setProperty(form, name, value); } } catch (Exception exception) { final String valueTypeName = value.getClass().getName(); final String propertyTypeName = descriptor.getPropertyType().getName(); final StringBuilder message = new StringBuilder( "Can not set form property '" + name + "' of type: " + propertyTypeName + " with value: " + value); if (!descriptor.getPropertyType().isAssignableFrom(value.getClass())) { message.append("; " + valueTypeName + " is not assignable to " + propertyTypeName); } throw new IllegalArgumentException( message + ": " + exception.toString()); } } } } } } } } catch (final Throwable throwable) { throw new RuntimeException(throwable); } } }
From source file:org.apache.myfaces.test.AbstractTagLibTestCase.java
private void assertSetters(final Tag tag, final String path, final Object object) { TagAttribute[] attributes = tag.getAttributes(); String tagName = tag.getName(); for (int a = 0; a < attributes.length; a++) { TagAttribute attribute = attributes[a]; String name = attribute.getAttributeName(); if (name == null || "".equals(name.trim())) throw new IllegalStateException(path + ":" + tagName + " has a nameless attribute "); String msg = path + ":" + tagName + "@" + name + " exists, but " + object.getClass().getName() + " has no setter."; assertTrue(msg, PropertyUtils.isWriteable(object, name)); } // end for attributes }
From source file:org.apache.torque.generator.configuration.outlet.JavaOutletSaxHandler.java
/** * {@inheritDoc}//from www. ja v a2 s. c o m */ @Override public void endElement(String uri, String localName, String rawName) throws SAXException { level--; if (propertyName != null) { if (!PropertyUtils.isWriteable(getOutlet(), propertyName)) { throw new SAXException("No setter found for property " + propertyName + " in class " + getOutlet().getClass().getName()); } try { BeanUtils.copyProperty(getOutlet(), propertyName, propertyValue.toString()); } catch (InvocationTargetException e) { throw new SAXException("error while setting Property " + propertyName + " for java outlet " + getOutlet().getName() + " with value " + propertyValue.toString(), e); } catch (IllegalAccessException e) { throw new SAXException("error while setting Property " + propertyName + " for java outlet " + getOutlet().getName() + " with value " + propertyValue.toString(), e); } propertyName = null; propertyValue = null; } else { super.endElement(uri, localName, rawName); } }
From source file:org.apache.torque.generator.configuration.source.SourceTransformerSaxHandler.java
/** * {@inheritDoc}/*from w w w.j a v a 2 s.c om*/ */ @Override public void endElement(String uri, String localName, String rawName) throws SAXException { level--; if (level == 2) { listPropertyValue.add(listPropertyEntry.toString()); listPropertyEntry = null; } else if (level == 1) { if (!PropertyUtils.isWriteable(sourceTransformer, propertyName)) { throw new SAXException("No setter found for property " + propertyName + " in class " + sourceTransformer.getClass().getName()); } Object propertyValue; if (simplePropertyValue != null) { propertyValue = simplePropertyValue.toString(); } else { propertyValue = listPropertyValue; } try { BeanUtils.copyProperty(sourceTransformer, propertyName, propertyValue); } catch (InvocationTargetException e) { throw new SAXException("error while setting Property " + propertyName + " for java transformer " + sourceTransformer.getClass().getName() + " with value " + propertyValue.toString(), e); } catch (IllegalAccessException e) { throw new SAXException("error while setting Property " + propertyName + " for java transformer " + sourceTransformer.getClass().getName() + " with value " + propertyValue.toString(), e); } propertyName = null; propertyValue = null; } else if (level != 0) { throw new SAXException("endElemend reached Level " + level); } }