List of usage examples for org.apache.commons.beanutils PropertyUtils getPropertyDescriptor
public static PropertyDescriptor getPropertyDescriptor(Object bean, String name) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException
Retrieve the property descriptor for the specified property of the specified bean, or return null
if there is no such descriptor.
For more details see PropertyUtilsBean
.
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 v a 2s .co 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:ar.com.fdvs.dj.domain.builders.ReflectiveReportBuilder.java
/** * Adds a column for every property specified in the array. * @param _data the data collection. A not null and nor empty collection should be passed. * @param _propertiesNames and array with the names of the desired properties. *///from w w w .j a va 2 s. c o m public ReflectiveReportBuilder(final Collection _data, final String[] _propertiesNames) { if (_data == null || _data.isEmpty()) { throw new IllegalArgumentException("Parameter _data is null or empty"); } if (_propertiesNames == null || _propertiesNames.length == 0) { throw new IllegalArgumentException("Parameter _propertiesNames is null or empty"); } final Object item = _data.iterator().next(); final PropertyDescriptor[] properties = new PropertyDescriptor[_propertiesNames.length]; try { for (int i = 0; i < _propertiesNames.length; i++) { final String propertiesName = _propertiesNames[i]; properties[i] = PropertyUtils.getPropertyDescriptor(item, propertiesName); } addProperties(properties); } catch (Exception ex) { LOGGER.error("", ex); } }
From source file:com.blackbear.flatworm.ParseUtils.java
/** * Determine how best to add the {@code toAdd} instance to the collection found in {@code target} by seeing if either the {@code * Segment.addMethod} has a value or if {@code Segment.propertyName} has a value. If neither values exist then no action is taken. * * @param cardinality The {@link CardinalityBO} instance containing the configuration information. * @param target The instance with the collection to which the {@code toAdd} instance is to be added. * @param toAdd The instance to be added to the specified collection. * @throws FlatwormParserException should the attempt to add the {@code toAdd} instance to the specified collection fail for any * reason. *///from ww w . j a v a 2 s. c om public static void addValueToCollection(CardinalityBO cardinality, Object target, Object toAdd) throws FlatwormParserException { if (cardinality.getCardinalityMode() != CardinalityMode.SINGLE) { boolean addToCollection = true; PropertyDescriptor propDesc; try { propDesc = PropertyUtils.getPropertyDescriptor(target, cardinality.getPropertyName()); } catch (Exception e) { // This should only happen via the XML configuration as the annotation configuration uses reflection to // determine the property name. throw new FlatwormParserException(String.format( "Failed to read property %s on bean %s. Make sure the specified " + "property-name is correct in the configuration for the record-element.", cardinality.getPropertyName(), target.getClass().getName()), e); } if (cardinality.getCardinalityMode() == CardinalityMode.STRICT || cardinality.getCardinalityMode() == CardinalityMode.RESTRICTED) { try { Object currentValue = PropertyUtils.getProperty(target, cardinality.getPropertyName()); int currentSize; if (Collection.class.isAssignableFrom(propDesc.getPropertyType())) { currentSize = Collection.class.cast(currentValue).size(); } else if (propDesc.getPropertyType().isArray()) { currentSize = Array.getLength(currentValue); } else { throw new FlatwormParserException(String.format( "Bean %s has a Cardinality Mode of %s for property %s, " + "suggesting that it is an Array or some instance of java.util.Collection. However, the property type " + "is %s, which is not currently supported.", target.getClass().getName(), cardinality.getCardinalityMode().name(), cardinality.getPropertyName(), propDesc.getPropertyType().getName())); } addToCollection = currentSize < cardinality.getMaxCount() || cardinality.getMaxCount() < 0; } catch (Exception e) { throw new FlatwormParserException(String.format( "Failed to load property %s on bean %s when determining if a " + "value could be added to the collection.", cardinality.getPropertyName(), target.getClass().getName()), e); } if (!addToCollection && cardinality.getCardinalityMode() == CardinalityMode.STRICT) { throw new FlatwormParserException(String.format( "Cardinality limit of %d exceeded for property %s of bean %s " + "with Cardinality Mode set to %s.", cardinality.getMaxCount(), cardinality.getPropertyName(), target.getClass().getName(), cardinality.getCardinalityMode().name())); } } // Add it if we have determined that's allowed. if (addToCollection) { // Need to make sure we have an add method for Arrays. // TODO - add ability to automatically expand an array - for now, use an addMethod or collections. if (StringUtils.isBlank(cardinality.getAddMethod()) && propDesc.getPropertyType().isArray()) { throw new FlatwormParserException(String.format( "Bean %s with property %s is an Array and therefore an Add Method " + "must be specified in the configuration so that an element can be properly added to the array. " + "Auto-expanding an array is not yet supported.", target.getClass().getName(), cardinality.getPropertyName())); } if (!StringUtils.isBlank(cardinality.getAddMethod())) { invokeAddMethod(target, cardinality.getAddMethod(), toAdd); } else if (!StringUtils.isBlank(cardinality.getPropertyName())) { addValueToCollection(target, cardinality.getPropertyName(), toAdd); } } } else { throw new FlatwormParserException(String.format( "Object %s attempted to be added to Object %s as part of a collection," + " but the configuration has it configured as a %s Cardinality Mode.", toAdd.getClass().getName(), target.getClass().getName(), cardinality.getCardinalityMode().name())); } }
From source file:at.molindo.notify.model.BeanParams.java
private PropertyDescriptor getDescriptor(String propertyName) { try {/*from w w w . j av a 2 s.c om*/ PropertyDescriptor pd = PropertyUtils.getPropertyDescriptor(_bean, propertyName); return pd != null && pd.getWriteMethod() != null && pd.getReadMethod() != null ? pd : null; } catch (IllegalAccessException e) { throw new NotifyRuntimeException( "failed to get PropertyDescriptor for property " + propertyName + " from bean " + _bean, e); } catch (InvocationTargetException e) { throw new NotifyRuntimeException( "failed to get PropertyDescriptor for property " + propertyName + " from bean " + _bean, e); } catch (NoSuchMethodException e) { // ignore return null; } }
From source file:gr.interamerican.bo2.utils.TestJavaBeanUtils.java
/** * Unit test for getProperty.//from ww w . jav a 2s .c om * @throws IllegalAccessException * @throws InvocationTargetException * @throws NoSuchMethodException */ @Test public void testGetProperty() throws IllegalAccessException, InvocationTargetException, NoSuchMethodException { BeanWith2Fields bean = new BeanWith2Fields(); PropertyDescriptor pd = PropertyUtils.getPropertyDescriptor(bean, "field1"); //$NON-NLS-1$ String value = "string"; //$NON-NLS-1$ bean.setField1(value); String actual = (String) JavaBeanUtils.getProperty(pd, bean); assertEquals(value, actual); }
From source file:com.hengyi.japp.sap.convert.impl.FieldCopyBase.java
protected void initBeanPropertyReadMethod(Object bean, JCoRecord record) throws Exception { PropertyDescriptor descriptor = PropertyUtils.getPropertyDescriptor(bean, beanPropertyName); beanPropertyReadMethod = descriptor.getReadMethod(); }
From source file:eagle.storage.jdbc.entity.JdbcEntitySerDeserHelper.java
/** * * @param obj//w w w . j av a2s. c om * @param fieldName * @return * @throws IllegalAccessException * @throws NoSuchMethodException * @throws InvocationTargetException */ public static PropertyDescriptor getPropertyDescriptor(Object obj, String fieldName) throws IllegalAccessException, NoSuchMethodException, InvocationTargetException { String key = obj.getClass().getName() + "." + fieldName; PropertyDescriptor propertyDescriptor = _propertyDescriptorCache.get(key); if (propertyDescriptor == null) { propertyDescriptor = PropertyUtils.getPropertyDescriptor(obj, fieldName); _propertyDescriptorCache.put(key, propertyDescriptor); } return propertyDescriptor; }
From source file:edu.ku.brc.af.ui.forms.FormHelper.java
/** * XXX This needs to be moved! This references the specify packge * /*from w w w . jav a2s.c o m*/ * Sets the "timestampModified" and the "lastEditedBy" by fields if the exist, if they don't then * then it just ignores the request (no error is thrown). The lastEditedBy use the value of the string * set by the method currentUserEditStr. * @param dataObj the data object to have the fields set * @param doCreatedTime indicates it should set the created time also * @return true if it was able to set the at least one of the fields */ public static boolean updateLastEdittedInfo(final Object dataObj, final boolean doCreatedTime) { log.debug( "updateLastEdittedInfo for [" + (dataObj != null ? dataObj.getClass() : "dataObj was null") + "]"); if (dataObj != null) { if (dataObj instanceof Collection<?>) { boolean retVal = false; for (Object o : (Collection<?>) dataObj) { if (updateLastEdittedInfo(o)) { retVal = true; } } return retVal; } try { DataObjectSettable setter = DataObjectSettableFactory.get(dataObj.getClass().getName(), DATA_OBJ_SETTER); if (setter != null) { Timestamp timestamp = new Timestamp(System.currentTimeMillis()); boolean foundOne = false; PropertyDescriptor descr = PropertyUtils.getPropertyDescriptor(dataObj, "timestampModified"); if (descr != null) { setter.setFieldValue(dataObj, "timestampModified", timestamp); foundOne = true; } if (doCreatedTime) { descr = PropertyUtils.getPropertyDescriptor(dataObj, "timestampCreated"); if (descr != null) { setter.setFieldValue(dataObj, "timestampCreated", timestamp); foundOne = true; } } descr = PropertyUtils.getPropertyDescriptor(dataObj, "modifiedByAgent"); if (descr != null) { setter.setFieldValue(dataObj, "modifiedByAgent", Agent.getUserAgent()); foundOne = true; } return foundOne; } } catch (NoSuchMethodException ex) { edu.ku.brc.af.core.UsageTracker.incrHandledUsageCount(); edu.ku.brc.exceptions.ExceptionTracker.getInstance().capture(FormHelper.class, ex); ex.printStackTrace(); } catch (IllegalAccessException ex) { edu.ku.brc.af.core.UsageTracker.incrHandledUsageCount(); edu.ku.brc.exceptions.ExceptionTracker.getInstance().capture(FormHelper.class, ex); ex.printStackTrace(); } catch (InvocationTargetException ex) { edu.ku.brc.af.core.UsageTracker.incrHandledUsageCount(); edu.ku.brc.exceptions.ExceptionTracker.getInstance().capture(FormHelper.class, ex); ex.printStackTrace(); } } log.debug("updateLastEdittedInfo object is NULL"); return false; }
From source file:com.blackbear.flatworm.converters.ConversionHelper.java
/** * Use an alternate method that attempts to use reflection to figure out which conversion routine to use. * * @param bean The {@link Object} that contains the property. * @param beanName The name of the bean as configured. * @param propertyName The name of the property that is to be set. * @param fieldChars The value./*www . jav a 2s .co m*/ * @param options The {@link ConversionOptionBO}s. * @return The {@link Object} constructed from the {@code fieldChars} value. * @throws FlatwormParserException should parsing the value to a {@link Object} fail for any reason. */ public Object convert(Object bean, String beanName, String propertyName, String fieldChars, Map<String, ConversionOptionBO> options) throws FlatwormParserException { Object value; try { PropertyDescriptor propDescriptor = PropertyUtils.getPropertyDescriptor(bean, propertyName); value = ConverterFunctionCache.convertFromString(propDescriptor.getPropertyType(), fieldChars, options); } catch (Exception e) { throw new FlatwormParserException( String.format("Failed to convert and set value '%s' on bean %s [%s] for property %s.", fieldChars, beanName, bean.getClass().getName(), propertyName), e); } return value; }
From source file:fr.mael.microrss.service.impl.UserServiceImpl.java
@Override public void updateOption(String name, String value) { UserConfig conf = new UserConfig(); try {/*from w w w.j a v a2s . c om*/ PropertyDescriptor desc = PropertyUtils.getPropertyDescriptor(conf, name); if (desc != null) { userDao.updateConfig(securityUtil.getCurrentUser(), name, value); } } catch (Exception e) { LOG.error("Error reading property " + name, e); } }