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:net.kamhon.ieagle.util.ReflectionUtil.java

public static void copyPropertiesWithPropertiesList(Object source, Object target, String... properties) {
    if (source == null)
        throw new SystemErrorException("source is null");
    if (target == null)
        throw new SystemErrorException("target is null");

    if (properties == null) {
        copyProperties(source, target);//from   www. j a  v a 2  s .  c om
    } else {
        for (String property : properties) {
            Object value;
            try {
                value = PropertyUtils.getProperty(source, property);
            } catch (Exception e) {
                throw new SystemErrorException("no property[" + property + "] for source[" + source + "]");
            }

            try {
                PropertyUtils.setProperty(target, property, value);
            } catch (Exception e) {
                throw new SystemErrorException("no property[" + property + "] for target[" + target + "]");
            }
        }
    }
}

From source file:com.esofthead.mycollab.vaadin.ui.MultiSelectComp.java

protected String getDisplaySelectedItemsString() {
    final StringBuilder str = new StringBuilder();
    for (int i = 0; i < selectedItems.size(); i++) {
        final Object itemObj = selectedItems.get(i);
        try {/*w ww . j a va  2s.c om*/
            String objDisplayName = (String) PropertyUtils.getProperty(itemObj, propertyDisplayField);
            if (i == selectedItems.size() - 1) {
                str.append(objDisplayName);
            } else {
                str.append(objDisplayName + ", ");
            }
        } catch (final Exception e) {
            throw new MyCollabException(e);
        }
    }
    return str.toString();
}

From source file:com.troyhisted.inputfield.field.DynaFieldTest.java

/**
 * Ensure all properties can be retrieved using {@link BeanUtils}.
 *
 * @throws InvocationTargetException/*  w  w w  .j  a  va2  s .com*/
 * @throws IllegalAccessException
 * @throws NoSuchMethodException
 */
@Test
public void testGetAllBeanProperties()
        throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
    final Field<String> field = this.construct(String.class);
    final String value = "value";
    final String label = "label";
    final Message message = new ErrorMessage("test");
    final List<String> options = Arrays.asList("one", "two");
    field.value(value).label(label).message(message).options(options);
    Assert.assertEquals(value, BeanUtils.getProperty(field, "value"));
    Assert.assertEquals(label, BeanUtils.getProperty(field, "label"));
    Assert.assertEquals(message, PropertyUtils.getProperty(field, "message"));
    Assert.assertEquals(message.getText(), PropertyUtils.getProperty(field, "messageText"));
    Assert.assertEquals(options, PropertyUtils.getProperty(field, "options"));
}

From source file:com.ewcms.publication.freemarker.directive.PropertyDirective.java

/**
 * // www  .j  a  v  a  2s  .  co  m
 * 
 * @param objectValue
 *                
 * @param property 
 *                ??
 * @return           
 */
protected Object getValue(Object objectValue, String property) throws NoSuchMethodException {
    try {
        return PropertyUtils.getProperty(objectValue, property);
    } catch (NoSuchMethodException e) {
        throw e;
    } catch (Exception e) {
        throw new NoSuchMethodException(e.toString());
    }
}

From source file:com.qperior.gsa.oneboxprovider.implementations.jive.rest.QPJiveJsonObject.java

private MorphDynaBean ensureBeanProperty(Object bean, String name) {
    try {/*from  www. ja va  2 s.c o  m*/
        return (MorphDynaBean) PropertyUtils.getProperty(bean, name);
    } catch (Exception exc) {
        this.log.info("Exception in getting JSON property '" + name + "': " + exc.getLocalizedMessage());
        return null;
    }
}

From source file:com.esofthead.mycollab.module.crm.ui.components.CrmFollowersComp.java

private int getTotalFollowers(V bean) {
    try {//from  w w  w  . j  a v  a 2 s.  c  o m
        MonitorSearchCriteria criteria = new MonitorSearchCriteria();
        criteria.setTypeId(new NumberSearchField((int) PropertyUtils.getProperty(bean, "id")));
        criteria.setType(new StringSearchField(type));
        return monitorItemService.getTotalCount(criteria);
    } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
        LOG.error("Error", e);
        return 0;
    }
}

From source file:com.mycollab.module.crm.ui.components.CrmFollowersComp.java

private void unfollowItem(String username) {
    try {/*from w ww. j  av  a2  s . c  om*/
        MonitorSearchCriteria criteria = new MonitorSearchCriteria();
        criteria.setTypeId(new NumberSearchField((Integer) PropertyUtils.getProperty(bean, "id")));
        criteria.setType(StringSearchField.and(type));
        criteria.setUser(StringSearchField.and(username));
        monitorItemService.removeByCriteria(criteria, MyCollabUI.getAccountId());
        for (SimpleUser user : followers) {
            if (username.equals(user.getUsername())) {
                followers.remove(user);
                break;
            }
        }
    } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
        LOG.error("Error", e);
    }
}

From source file:io.milton.http.annotated.AbstractAnnotationHandler.java

protected Object attemptToReadProperty(Object source, String... propNames)
        throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
    for (String propName : propNames) {
        if (PropertyUtils.isReadable(source, propName)) {
            // found a readable property, so return it            
            Object oName = PropertyUtils.getProperty(source, propName);
            return oName;
        }/*from w  w w.j a  v a  2  s. co  m*/
    }
    return null;
}

From source file:com.discovery.darchrow.bean.PropertyUtil.java

/**
 *  {@link PropertyUtils#getProperty(Object, String)} ?.
 * /*www  . j  a va  2 s  .  c om*/
 * <h3>{@link BeanUtils#getProperty(Object, String) BeanUtils.getProperty}& {@link PropertyUtils#getProperty(Object, String)
 * PropertyUtils.getProperty}:</h3>
 * 
 * <blockquote>
 * <p>
 * {@link BeanUtils#getProperty(Object, String)} ?String,<br>
 * {@link PropertyUtils#getProperty(Object, String)} Object,???
 * </p>
 * </blockquote>
 * 
 * 
 * <h3>:</h3>
 * 
 * <pre>
 * {@code
 * getPropertysetProperty,?2?JavaBean????.
 * Company c = new Company();
 * c.setName("Simple");
 * 
 * Simple?????
 * //Simple
 * LOGGER.debug(BeanUtils.getProperty(c, "name"));
 * 
 * Map???key??
 * //Map
 *     LOGGER.debug(BeanUtils.getProperty(c, "address (A2)"));
 *     HashMap am = new HashMap();
 *     am.put("1","234-222-1222211");
 *     am.put("2","021-086-1232323");
 *     BeanUtils.setProperty(c,"telephone",am);
 * LOGGER.debug(BeanUtils.getProperty(c, "telephone (2)"));
 * 
 * Indexed??[]??ArrayList???.
 * //index
 *     LOGGER.debug(BeanUtils.getProperty(c, "otherInfo[2]"));
 *     BeanUtils.setProperty(c, "product[1]", "NOTES SERVER");
 *     LOGGER.debug(BeanUtils.getProperty(c, "product[1]"));
 * 
 * 3????
 * //nest
 *     LOGGER.debug(BeanUtils.getProperty(c, "employee[1].name"));
 * 
 * }
 * </pre>
 *
 * @param <T>
 *            the generic type
 * @param bean
 *            Bean whose property is to be extracted
 * @param name
 *            Possibly indexed and/or nested name of the property to be extracted
 * @return {@link PropertyUtils#getProperty(Object, String)} ?
 * @see com.baozun.nebulaplus.bean.BeanUtil#getProperty(Object, String)
 * @see org.apache.commons.beanutils.BeanUtils#getProperty(Object, String)
 * @see org.apache.commons.beanutils.PropertyUtils#getProperty(Object, String)
 * @see org.apache.commons.beanutils.PropertyUtilsBean
 */
public static <T> T getProperty(Object bean, String name) {
    //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.
    try {
        @SuppressWarnings("unchecked")
        T propertyValue = (T) PropertyUtils.getProperty(bean, name);
        return propertyValue;
    } catch (Exception e) {
        LOGGER.error(e.getClass().getName(), e);
        throw new BeanUtilException(e);
    }
}

From source file:de.micromata.genome.util.types.Converter.java

/**
 * Copy convert int./* w  ww.  j  a v  a  2s  . c  om*/
 *
 * @param source the source
 * @param sourceName the source name
 * @param target the target
 * @param targetName the target name
 * @return true, if successful
 */
public static boolean copyConvertInt(Object source, String sourceName, Object target, String targetName) {

    try {
        Object so = PropertyUtils.getProperty(source, sourceName);
        Integer bd = null;
        if (so instanceof Integer) {
            bd = (Integer) so;
        } else {
            String s = (String) so;
            bd = Integer.parseInt(s);
        }
        PropertyUtils.setProperty(target, targetName, bd);
        return true;
    } catch (Throwable ex) { // NOSONAR "Illegal Catch" framework
        return false;
    }
}