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.aliyun.oss.integrationtests.ImageProcessTest.java

private static ImageInfo getImageInfo(final String bucket, final String image)
        throws IOException, IllegalAccessException, InvocationTargetException, NoSuchMethodException {
    GetObjectRequest request = new GetObjectRequest(bucketName, image);
    request.setProcess("image/info");
    OSSObject ossObject = ossClient.getObject(request);

    String jsonStr = IOUtils.readStreamAsString(ossObject.getObjectContent(), "UTF-8");
    ossObject.getObjectContent().close();

    JSONObject jsonObject = JSONObject.fromObject(jsonStr);
    Object bean = JSONObject.toBean(jsonObject);

    long height = Long.parseLong(
            (String) PropertyUtils.getProperty(PropertyUtils.getProperty(bean, "ImageHeight"), "value"));
    long width = Long.parseLong(
            (String) PropertyUtils.getProperty(PropertyUtils.getProperty(bean, "ImageWidth"), "value"));
    long size = Long.parseLong(
            (String) PropertyUtils.getProperty(PropertyUtils.getProperty(bean, "FileSize"), "value"));
    String format = (String) PropertyUtils.getProperty(PropertyUtils.getProperty(bean, "Format"), "value");

    return new ImageInfo(height, width, size, format);
}

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

private Integer ensureIntegerProperty(Object bean, String name) {
    try {//  w  w w. j a  v  a  2  s .c  om
        return (Integer) PropertyUtils.getProperty(bean, name);
    } catch (Exception exc) {
        this.log.info("Exception in getting JSON property '" + name + "': " + exc.getLocalizedMessage());
        return null;
    }
}

From source file:com.zc.orm.hibernate.HibernateDao.java

/**
 * ???./*from w w w.j  a v  a2 s .com*/
 * 
 * @param uniquePropertyNames
 *            POJO???,? "name,loginid,password"
 */
public boolean isUnique(T entity, String uniquePropertyNames) {
    Criteria criteria = getSession().createCriteria(entityClass).setProjection(Projections.rowCount());
    String[] nameList = uniquePropertyNames.split(",");
    try {
        // 
        for (int i = 0; i < nameList.length; i++) {
            criteria.add(Restrictions.eq(nameList[i], PropertyUtils.getProperty(entity, nameList[i])));
        }

        // ?update,entity.

        String idName = getSessionFactory().getClassMetadata(entity.getClass()).getIdentifierPropertyName();
        if (idName != null) {
            // ?entity
            Serializable id = (Serializable) PropertyUtils.getProperty(entity, idName);

            // id!=null,,?update,
            if (id != null)
                criteria.add(Restrictions.not(Restrictions.eq(idName, id)));
        }
    } catch (Exception e) {
        Reflector.convertReflectionExceptionToUnchecked(e);
    }
    return ((Number) criteria.uniqueResult()).intValue() == 0;
}

From source file:com.beidou.common.util.ReflectionUtils.java

/**
 * ????(getter), ??List.//ww w  .  jav  a2s.  com
 * 
 * @param collection ???.
 * @param propertyName ??????.
 */
@SuppressWarnings({ "rawtypes", "unchecked" })
public static List convertElementPropertyToList(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 convertReflectionExceptionToUnchecked(e);
    }

    return list;
}

From source file:com.silverpeas.util.CollectionUtil.java

/**
 * Value converted from a bean property.
 * @param bean bean/* ww  w.  ja v  a2  s  . c om*/
 * @param propertyName
 */
private static synchronized Object getPropertyAsObject(final Object bean, final String propertyName) {
    // Synchronized as PropertyEditor is not thread-safe.
    Object property = null;
    try {
        property = PropertyUtils.getProperty(bean, propertyName);
    } catch (final IllegalAccessException e) {
        throw new RuntimeException(e.getMessage(), e);
    } catch (final InvocationTargetException e) {
        throw new RuntimeException(e.getMessage(), e);
    } catch (final NoSuchMethodException e) {
        throw new RuntimeException(e.getMessage(), e);
    } catch (final NestedNullException e) {
        // In the case of a.b, with a == null, null is returned
        property = null;
    }
    if (property == null) {
        return null;
    }
    return property;
}

From source file:com.expressui.core.view.field.SelectField.java

/**
 * Gets the selected bean./* w w w  . j  av  a  2  s  .c o m*/
 *
 * @return selected bean
 */
public V getBean() {
    try {
        return (V) PropertyUtils.getProperty(typedForm.getBean(), getPropertyId());
    } catch (IllegalAccessException e) {
        throw new RuntimeException(e);
    } catch (InvocationTargetException e) {
        throw new RuntimeException(e);
    } catch (NoSuchMethodException e) {
        throw new RuntimeException(e);
    }
}

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

private void unfollowItem(String username, V bean) {
    try {//w w  w .j  av  a 2  s  . co  m
        MonitorSearchCriteria criteria = new MonitorSearchCriteria();
        criteria.setTypeId(new NumberSearchField((int) PropertyUtils.getProperty(bean, "id")));
        criteria.setType(new StringSearchField(type));
        criteria.setUser(new StringSearchField(username));
        monitorItemService.removeByCriteria(criteria, AppContext.getAccountId());
    } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
        LOG.error("Error", e);
    }
}

From source file:com.cangqu.gallery.server.base.utils.ReflectionUtils.java

/**
 * ????(getter), ??.<br/>/*from  w w w .  jav a  2s .c  o  m*/
 *
 * @param collection   ???.<br/>
 * @param propertyName key??.<br/>
 */
@SuppressWarnings("unchecked")
public static Map convertToMapWithElementProperty(final Collection collection, final String propertyName) {
    Map elementMap = new LinkedHashMap();
    try {
        for (Object obj : collection) {
            elementMap.put(PropertyUtils.getProperty(obj, propertyName), obj);
        }
    } catch (Exception e) {
        throw convertReflectionExceptionToUnchecked(e);
    }
    return elementMap;
}

From source file:com.esofthead.mycollab.common.interceptor.aspect.AuditLogAspect.java

private Integer saveAuditLog(Class<?> targetCls, Object bean, String username, Integer sAccountId,
        Integer activityStreamId) {
    Auditable auditAnnotation = targetCls.getAnnotation(Auditable.class);
    if (auditAnnotation != null) {
        String key;/*from www  . j av a  2 s .c o m*/
        String changeSet = "";
        try {

            int typeid = (Integer) PropertyUtils.getProperty(bean, "id");
            key = bean.toString() + ClassInfoMap.getType(targetCls) + typeid;

            Object oldValue = caches.get(key);
            if (oldValue != null) {
                AuditLog auditLog = new AuditLog();
                auditLog.setPosteduser(username);
                auditLog.setModule(ClassInfoMap.getModule(targetCls));
                auditLog.setType(ClassInfoMap.getType(targetCls));
                auditLog.setTypeid(typeid);
                auditLog.setSaccountid(sAccountId);
                auditLog.setPosteddate(new GregorianCalendar().getTime());
                changeSet = AuditLogUtil.getChangeSet(oldValue, bean);
                auditLog.setChangeset(changeSet);
                auditLog.setObjectClass(oldValue.getClass().getName());
                if (activityStreamId != null) {
                    auditLog.setActivitylogid(activityStreamId);
                }

                return auditLogService.saveWithSession(auditLog, "");
            }
        } catch (Exception e) {
            LOG.error("Error when save audit for save action of service " + targetCls.getName() + "and bean: "
                    + BeanUtility.printBeanObj(bean) + " and changeset is " + changeSet, e);
            return null;
        }
    }

    return null;
}

From source file:com.mycollab.aspect.AuditLogAspect.java

private String getChangeSet(Class<?> targetCls, Object bean, List<String> excludeHistoryFields,
        boolean isSelective) {
    try {//from   www .  ja  v  a2s .c om
        Integer typeId = (Integer) PropertyUtils.getProperty(bean, "id");
        String key = bean.toString() + ClassInfoMap.getType(targetCls) + typeId;

        Object oldValue = cacheService.getValue(AUDIT_TEMP_CACHE, key);
        if (oldValue != null) {
            return AuditLogUtil.getChangeSet(oldValue, bean, excludeHistoryFields, isSelective);
        }
        return null;
    } catch (Exception e) {
        LOG.error("Error while generate changeset", e);
        return null;
    }
}