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:it.geosolutions.geoserver.jms.impl.utils.BeanUtils.java
/** * This is a 'smart' (perform checks for some special cases) update function which should be used to copy of the properties for objects * of the catalog and configuration./*from w ww . ja v a2s . c o m*/ * * @param <T> the type of the bean to update * @param info the bean instance to update * @param properties the list of string of properties to update * @param values the list of new values to update * * @throws IllegalAccessException * @throws InvocationTargetException * @throws NoSuchMethodException */ public static <T> void smartUpdate(final T info, final List<String> properties, final List<Object> values) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException { final Iterator<String> itPropertyName = properties.iterator(); final Iterator<Object> itValue = values.iterator(); while (itPropertyName.hasNext() && itValue.hasNext()) { String propertyName = itPropertyName.next(); final Object value = itValue.next(); PropertyDescriptor pd = PropertyUtils.getPropertyDescriptor(info, propertyName); // return null if there is no such descriptor if (pd == null) { // this is a special case used by the NamespaceInfoImpl setURI // the propertyName coming from the ModificationProxy is set to 'uRI' // lets set it to uri propertyName = propertyName.toUpperCase(); pd = PropertyUtils.getPropertyDescriptor(info, propertyName); if (pd == null) { return; } } if (pd.getWriteMethod() != null) { PropertyUtils.setProperty(info, propertyName, value); } else { // T interface do not declare setter method for this property // lets use getter methods to get the property reference final Object property = PropertyUtils.getProperty(info, propertyName); // check type of property to apply new value if (Collection.class.isAssignableFrom(pd.getPropertyType())) { final Collection<?> liveCollection = (Collection<?>) property; liveCollection.clear(); liveCollection.addAll((Collection) value); } else if (Map.class.isAssignableFrom(pd.getPropertyType())) { final Map<?, ?> liveMap = (Map<?, ?>) property; liveMap.clear(); liveMap.putAll((Map) value); } else { if (CatalogUtils.LOGGER.isLoggable(java.util.logging.Level.SEVERE)) CatalogUtils.LOGGER.severe("Skipping unwritable property " + propertyName + " with property type " + pd.getPropertyType()); } } } }
From source file:com.usefullc.platform.common.utils.ArrayUtils.java
/** * ??/*from w w w . j a v a 2 s. c om*/ * * @param objList * @param propName * @return */ @SuppressWarnings("unchecked") public static <T1, T2> List<T2> objListToPrimitiveList(List<T1> objList, String propName) { Assert.notEmpty(objList); List<T2> destList = new ArrayList<T2>(); try { for (T1 obj : objList) { PropertyDescriptor propDes = PropertyUtils.getPropertyDescriptor(obj, propName); Method m = PropertyUtils.getReadMethod(propDes); Object resultValue = m.invoke(obj); if (resultValue != null) { destList.add((T2) resultValue); } } } catch (Exception e) { throw new RuntimeException(e); } return destList; }
From source file:com.hengyi.japp.sap.convert.impl.FieldCopyBoolean.java
@Override protected void initBeanPropertyWriteMethod(Object bean, JCoRecord record) throws Exception { PropertyDescriptor descriptor = PropertyUtils.getPropertyDescriptor(bean, beanPropertyName); Method m = descriptor.getWriteMethod(); if (m == null) { beanPropertyWriteMethod = null;/*w w w.j av a2 s . c o m*/ } else { beanPropertyWriteMethod = MethodUtils.getMatchingAccessibleMethod(bean.getClass(), m.getName(), BOOLEAN_CLASS); } }
From source file:com.ixcode.framework.javabean.format.JavaBeanFormatter.java
/** * This needs a map of parsers to type so we dont have to have this big if statement. */// ww w .j a v a2 s.c o m public Object parseStringToPropertyValue(Object model, String propertyName, Locale locale, String sourceValue) throws JavaBeanParseException, FormatterException { try { PropertyDescriptor propertyDescriptor = PropertyUtils.getPropertyDescriptor(model, propertyName); if (propertyDescriptor == null) { throw new IllegalStateException("Could not find a property in model " + model.getClass().getName() + " for property name '" + propertyName + "'"); } Class propertyType = propertyDescriptor.getPropertyType(); IJavaBeanValueFormat format = getFormat(locale, propertyType); return format.parse(sourceValue); } catch (IllegalAccessException e) { throw new FormatterException(e); } catch (InvocationTargetException e) { throw new FormatterException(e); } catch (NoSuchMethodException e) { throw new FormatterException(e); } }
From source file:name.martingeisse.wicket.autoform.describe.DefaultAutoformPropertyDescriptor.java
/** * /* w w w . j a va 2 s . c om*/ */ private void readObject(java.io.ObjectInputStream in) throws IOException, ClassNotFoundException { in.defaultReadObject(); try { this.propertyDescriptor = PropertyUtils.getPropertyDescriptor(bean, getName()); } catch (Exception e) { throw new RuntimeException(e); } }
From source file:de.mogwai.common.web.fieldinformationresolver.jpa.JPAAnnotationFieldInformationResolver.java
public Integer getMaxLengthInformationProvided(Application aApplication, FacesContext aContext, Object aBase, String aProperty) {//from ww w .java2s .co m try { PropertyDescriptor theDescriptor = PropertyUtils.getPropertyDescriptor(aBase, aProperty); if (theDescriptor == null) { return null; } Method theReadMethod = theDescriptor.getReadMethod(); if (theReadMethod != null) { Column theColumn = theReadMethod.getAnnotation(Column.class); if (theColumn != null) { return theColumn.length(); } } return null; } catch (Exception e) { LOGGER.error("Error", e); return null; } }
From source file:hudson.util.ReflectionUtils.java
public static Object getPublicProperty(Object o, String p) throws InvocationTargetException, NoSuchMethodException, IllegalAccessException { PropertyDescriptor pd = PropertyUtils.getPropertyDescriptor(o, p); if (pd == null) { // field? try {/* ww w . j a va 2 s . c o m*/ Field f = o.getClass().getField(p); return f.get(o); } catch (NoSuchFieldException e) { throw new IllegalArgumentException("No such property " + p + " on " + o.getClass()); } } else { return PropertyUtils.getProperty(o, p); } }
From source file:ca.sqlpower.object.undo.PropertyChangeEdit.java
/** * Sets the value of the property to be the old value *///from w w w. j av a 2 s.c om @Override public void undo() throws CannotUndoException { logger.debug("Undoing Property change: Setting " + sourceEvent.getPropertyName() + " from " + sourceEvent.getNewValue() + " to " + sourceEvent.getOldValue()); super.undo(); try { final PropertyDescriptor propertyDescriptor = PropertyUtils .getPropertyDescriptor(sourceEvent.getSource(), sourceEvent.getPropertyName()); logger.debug("Found property descriptor " + propertyDescriptor); if (logger.isDebugEnabled()) { PropertyDescriptor[] propertyDescriptors = PropertyUtils .getPropertyDescriptors(sourceEvent.getSource()); logger.debug("Descriptor has write method " + propertyDescriptor.getWriteMethod()); } Method setter = PropertyUtils.getWriteMethod(propertyDescriptor); logger.info("Found setter: " + setter.getName()); setter.invoke(sourceEvent.getSource(), sourceEvent.getOldValue()); } catch (Exception ex) { CannotUndoException wrapper = new CannotUndoException(); wrapper.initCause(ex); throw wrapper; } }
From source file:com.subakva.formicid.converters.FileSetConverter.java
public FileSet convert(Class type, Object value) { if (value == null || value.equals(Undefined.instance)) { throw new ConversionException("No value specified"); }// ww w . j a v a2 s . c om if (value instanceof FileSet) { return (FileSet) value; } else if (value instanceof Scriptable) { FileSet fileSet = (FileSet) container.getProject().createDataType("fileset"); Scriptable options = (Scriptable) value; Object[] ids = options.getIds(); for (int i = 0; i < ids.length; i++) { String id = (String) ids[i]; Object val = options.get(id, options); try { PropertyDescriptor descriptor = PropertyUtils.getPropertyDescriptor(fileSet, id); Class<?> propertyType = descriptor.getPropertyType(); Converter converter = container.getConverter(propertyType); Object converted = converter.convert(propertyType, val); PropertyUtils.setProperty(fileSet, id, converted); } catch (IllegalAccessException e) { throw new RuntimeException(e); } catch (InvocationTargetException e) { throw new RuntimeException(e); } catch (NoSuchMethodException e) { throw new RuntimeException(e); } } return fileSet; } else if (value instanceof String) { FileSet fileSet = (FileSet) container.getProject().createDataType("fileset"); fileSet.setDir(new File(".")); FilenameSelector selector = new FilenameSelector(); selector.setName((String) value); fileSet.add(selector); return fileSet; } else { throw new ConversionException("" + value); } }
From source file:de.micromata.genome.util.bean.PropertyAttrSetterGetter.java
/** * Inits the.// w w w . jav a 2 s . c om * * @param bean the bean */ private void init(BEAN bean) { if (propDescriptor != null) { return; } try { propDescriptor = PropertyUtils.getPropertyDescriptor(bean, propertyName); } catch (Exception ex) { throw new RuntimeException( "Cannot retrieve property " + propertyName + " from bean class " + bean.getClass().getName()); } }