Example usage for org.apache.commons.beanutils PropertyUtils getProperty

List of usage examples for org.apache.commons.beanutils PropertyUtils getProperty

Introduction

In this page you can find the example usage for org.apache.commons.beanutils PropertyUtils getProperty.

Prototype

public static Object getProperty(Object bean, String name)
        throws IllegalAccessException, InvocationTargetException, NoSuchMethodException 

Source Link

Document

Return the value of the specified property of the specified bean, no matter which property reference format is used, with no type conversions.

For more details see PropertyUtilsBean.

Usage

From source file:com.gisgraphy.rest.BeanToRestParameter.java

public static String toQueryString(Object object) {
    if (object == null) {
        throw new RestClientException("Can not get queryString for null object");
    }//from   w  w w. j a  v  a  2  s.com
    StringBuilder sb = new StringBuilder(128);
    try {
        boolean first = true;
        String andValue = "&";
        for (PropertyDescriptor thisPropertyDescriptor : Introspector
                .getBeanInfo(object.getClass(), Object.class).getPropertyDescriptors()) {
            Object property = PropertyUtils.getProperty(object, thisPropertyDescriptor.getName());
            if (property != null) {
                sb.append(first ? "?" : andValue);
                sb.append(thisPropertyDescriptor.getName());
                sb.append("=");
                sb.append(URLEncoder.encode(property.toString(), Constants.CHARSET));
                first = false;
            }
        }
    } catch (Exception e) {
        throw new RestClientException("can not generate url for bean: " + e.getMessage(), e);
    }
    return sb.toString();
}

From source file:net.cloudkit.enterprises.infrastructure.utilities.Collections3Utility.java

/**
 * ????(Getter), ??Map./*from   w ww  .j  a  va  2s.  c  om*/
 *
 * @param collection ???.
 * @param keyPropertyName ????MapKey??.
 * @param valuePropertyName ????MapValue??.
 */
public static Map<Object, Object> extractToMap(final Collection<?> collection, final String keyPropertyName,
        final String valuePropertyName) {
    Map<Object, Object> map = new HashMap<Object, Object>(collection.size());

    try {
        for (Object obj : collection) {
            map.put(PropertyUtils.getProperty(obj, keyPropertyName),
                    PropertyUtils.getProperty(obj, valuePropertyName));
        }
    } catch (Exception e) {
        throw ReflectionHelper.convertReflectionExceptionToUnchecked(e);
    }

    return map;
}

From source file:de.micromata.genome.gwiki.web.tags.GWikiTagRenderUtils.java

public static Object readFormValue(PageContext pageContext, String name) {
    Object bean = pageContext.getRequest().getAttribute(FORM_ATTR_NAME);
    if (bean == null) {
        throw new RuntimeException("No form under '" + FORM_ATTR_NAME + "' defined in request attribute");
    }/*from w  w w  .j  av  a2 s .  co  m*/
    try {
        Object o = PropertyUtils.getProperty(bean, name);
        return o;
    } catch (Exception ex) {
        throw new RuntimeException(
                "Cannot read property '" + FORM_ATTR_NAME + "'." + name + ": " + ex.getMessage(), ex);
    }
}

From source file:com.hihframework.core.utils.CollectionUtils.java

/**
 * ????,??./*from   w  ww.  jav a  2 s .  co m*/
 * 
 * @param collection
 *            ???.
 * @param propertyName
 *            ??????.
 */
public static List<Object> fetchPropertyToList(Collection<?> collection, String propertyName) throws Exception {

    List<Object> list = new ArrayList<Object>();

    for (Object obj : collection) {
        list.add(PropertyUtils.getProperty(obj, propertyName));
    }

    return list;
}

From source file:com.jdy.ddj.common.utils.Collections3.java

/**
 * ????(Getter), ??List.//from   w ww .j  a  v a2s.  c o m
 *
 * @param collection ???.
 * @param propertyName ??????.
 */
public static List extractToList(final Collection collection, final String propertyName) {
    List list = new ArrayList();

    try {
        for (Object obj : collection) {
            list.add(PropertyUtils.getProperty(obj, propertyName));
        }
    } catch (Exception e) {
        throw Reflections.convertReflectionExceptionToUnchecked(e);
    }

    return list;
}

From source file:ca.sqlpower.architect.TestUtils.java

/**
 * Sets all the settable properties on the given target object
 * which are not in the given ignore set.
 * /*from   ww w  .  j a v  a  2 s.c  o  m*/
 * @param target The object to change the properties of
 * @param propertiesToIgnore The properties of target not to modify or read
 * @return A Map describing the new values of all the non-ignored, readable 
 * properties in target.
 */
public static Map<String, Object> setAllInterestingProperties(Object target, Set<String> propertiesToIgnore)
        throws Exception {

    PropertyDescriptor props[] = PropertyUtils.getPropertyDescriptors(target);
    for (int i = 0; i < props.length; i++) {
        Object oldVal = null;
        if (PropertyUtils.isReadable(target, props[i].getName()) && props[i].getReadMethod() != null
                && !propertiesToIgnore.contains(props[i].getName())) {
            oldVal = PropertyUtils.getProperty(target, props[i].getName());
        }
        if (PropertyUtils.isWriteable(target, props[i].getName()) && props[i].getWriteMethod() != null
                && !propertiesToIgnore.contains(props[i].getName())) {

            NewValueMaker valueMaker = new ArchitectValueMaker(new SPObjectRoot());
            Object newVal = valueMaker.makeNewValue(props[i].getPropertyType(), oldVal, props[i].getName());

            System.out.println("Changing property \"" + props[i].getName() + "\" to \"" + newVal + "\"");
            PropertyUtils.setProperty(target, props[i].getName(), newVal);

        }
    }

    // read them all back at the end in case there were dependencies between properties
    return ca.sqlpower.testutil.TestUtils.getAllInterestingProperties(target, propertiesToIgnore);
}

From source file:com.brsanthu.dataexporter.model.BeanRow.java

public Object getCellValue(String columnName) {
    try {/*from  ww w  . j  av a  2s  .  c o m*/
        return PropertyUtils.getProperty(bean, columnName);
    } catch (Exception e) {
        throw new DataExportException("Exception while reading the property " + columnName + " from bean "
                + bean + " of type " + bean.getClass().getName(), e);
    }
}

From source file:com.fursel.util.validator.FieldMatchValidator.java

@Override
public boolean isValid(final Object value, final ConstraintValidatorContext context) {
    try {/*ww w. jav a  2 s.c o  m*/
        final Object firstObj = PropertyUtils.getProperty(value, firstFieldName);
        final Object secondObj = PropertyUtils.getProperty(value, secondFieldName);
        boolean valid = type.isValid(firstObj, secondObj);
        if (!valid) {
            context.buildConstraintViolationWithTemplate(errorMessage).addPropertyNode(secondFieldName)
                    .addConstraintViolation();
        }
        return valid;
    } catch (final Exception ignore) {
        return false;
    }
}

From source file:com.ms.commons.lang.BeanUtils.java

@SuppressWarnings("rawtypes")
public static <T extends Object> void copyProperties(Collection<T> target, Collection beans, String key,
        Collection<? extends ValueEditable> defaultValues) {
    Map<String, List<T>> result = CollectionUtils.toListMap(target, key);
    for (Object bean : beans) {
        try {// ww  w  .j  a  v a2s.  c  om
            Object keyProperty = PropertyUtils.getProperty(bean, key);
            List<T> keywords = result.get(keyProperty);
            if (keywords == null) {
                continue;
            }
            //
            for (T keyword : keywords) {
                copyProperties(keyword, bean, defaultValues);
            }
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
    }
}

From source file:apm.common.utils.Collections3.java

/**
 * ????(Getter), ??List.//from w  w  w.j a  v a 2  s .co m
 * 
 * @param collection ???.
 * @param propertyName ??????.
 */
@SuppressWarnings("unchecked")
public static List extractToList(final Collection collection, final String propertyName) {
    List list = new ArrayList(collection.size());

    try {
        for (Object obj : collection) {
            list.add(PropertyUtils.getProperty(obj, propertyName));
        }
    } catch (Exception e) {
        throw Reflections.convertReflectionExceptionToUnchecked(e);
    }

    return list;
}