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.framework.infrastructure.utils.Collections3.java

/**
 * (Getter), List.//from   ww  w  .  j  a  v a2 s  . co  m
 * 
 * @param collection .
 * @param propertyName .
 */
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 ReflectionUtils.convertReflectionExceptionToUnchecked(e);
    }

    return list;
}

From source file:com.eryansky.common.utils.collections.Collections3.java

/**
 * ????(Getter), ??List./*www.  j ava 2  s.  co m*/
 * 
 * @param collection ???.
 * @param propertyName ??????.
 */
@SuppressWarnings({ "rawtypes", "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 ReflectionUtils.convertReflectionExceptionToUnchecked(e);
    }

    return list;
}

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

static public String getChangeSet(Object oldObj, Object newObj, List<String> excludeFields,
        boolean isSelective) {
    Class cl = oldObj.getClass();
    List<AuditChangeItem> changeItems = new ArrayList<>();

    try {//ww  w .  j  a  v a 2 s  . c o  m
        BeanInfo beanInfo = Introspector.getBeanInfo(cl, Object.class);

        for (PropertyDescriptor propertyDescriptor : beanInfo.getPropertyDescriptors()) {
            String fieldName = propertyDescriptor.getName();
            if (excludeFields.contains(fieldName)) {
                continue;
            }
            String oldProp = getValue(PropertyUtils.getProperty(oldObj, fieldName));

            Object newPropVal;
            try {
                newPropVal = PropertyUtils.getProperty(newObj, fieldName);
            } catch (Exception e) {
                continue;
            }
            String newProp = getValue(newPropVal);

            if (!oldProp.equals(newProp)) {
                if (isSelective && newProp.equals("")) {
                } else {
                    AuditChangeItem changeItem = new AuditChangeItem();
                    changeItem.setField(fieldName);
                    changeItem.setNewvalue(newProp);
                    changeItem.setOldvalue(oldProp);
                    changeItems.add(changeItem);
                }
            }
        }
    } catch (Exception e) {
        LOG.error("There is error when convert changeset", e);
        return null;
    }

    return (changeItems.size() > 0) ? JsonDeSerializer.toJson(changeItems) : null;
}

From source file:com.pmarlen.model.controller.EntityJPAToPlainPojoTransformer.java

public static void copyPlainValues(Entity entity, Object plain) {
    final Class entityClass;
    entityClass = entity.getClass();/*  w ww . j  a  va 2s.  c  om*/
    final String entityClassName = entityClass.getName();
    Hashtable<String, Object> propertiesToCopy = new Hashtable<String, Object>();
    Hashtable<String, Object> propertiesM2MToCopy = new Hashtable<String, Object>();
    List<String> propertiesWithNULLValueToCopy = new ArrayList<String>();
    List<String> propertiesKey = new ArrayList<String>();
    try {
        final Field[] declaredFields = entityClass.getDeclaredFields();
        for (Field f : declaredFields) {
            logger.trace("->create: \tcopy: " + entityClassName + "." + f.getName() + " accesible ? "
                    + (f.isAccessible()));
            if (!f.isAccessible()) {

                if (f.isAnnotationPresent(javax.persistence.Id.class)) {
                    propertiesKey.add(f.getName());
                }
                if (f.isAnnotationPresent(javax.persistence.Id.class)
                        && !f.isAnnotationPresent(javax.persistence.GeneratedValue.class)
                        && !Modifier.isStatic(f.getModifiers())) {

                    Object valueCopyed = PropertyUtils.getProperty(entity, f.getName());
                    if (valueCopyed != null) {
                        propertiesToCopy.put(f.getName(), valueCopyed);
                    } else {
                        propertiesWithNULLValueToCopy.add(f.getName());
                    }
                    logger.trace("->create:\t\t ID elegible 2 be copied, added to copy list: " + f.getName()
                            + " = " + valueCopyed + ", is really null?" + (valueCopyed == null));
                } else if (!f.isAnnotationPresent(javax.persistence.Id.class)
                        && !f.isAnnotationPresent(javax.persistence.OneToMany.class)
                        && !f.isAnnotationPresent(javax.persistence.ManyToMany.class)
                        && !Modifier.isStatic(f.getModifiers())) {

                    Object valueCopyed = PropertyUtils.getProperty(entity, f.getName());
                    if (valueCopyed != null) {
                        propertiesToCopy.put(f.getName(), valueCopyed);
                    } else {
                        propertiesWithNULLValueToCopy.add(f.getName());
                    }
                    logger.trace("->create:\t\t elegible 2 be copied, added to copy list: " + f.getName()
                            + " = " + valueCopyed + ", is really null?" + (valueCopyed == null));
                } else if (!f.isAnnotationPresent(javax.persistence.Id.class)
                        && f.isAnnotationPresent(javax.persistence.ManyToMany.class)) {

                    Object valueCopyed = PropertyUtils.getProperty(entity, f.getName());
                    if (valueCopyed != null) {
                        propertiesM2MToCopy.put(f.getName(), valueCopyed);
                    } else {
                        propertiesWithNULLValueToCopy.add(f.getName());
                    }
                    logger.trace("->create:\t\t M2M elegible 2 be copied, added to copy list: " + f.getName()
                            + " = " + valueCopyed + ", is really null?" + (valueCopyed == null));
                }
            }
        }
        logger.trace("->create:copy values ?");
        for (String p2c : propertiesToCopy.keySet()) {
            Object valueCopyed = propertiesToCopy.get(p2c);
            logger.trace("->create:\t\t copy value with SpringUtils: " + p2c + " = " + valueCopyed
                    + ", is null?" + (valueCopyed == null));
            BeanUtils.copyProperty(plain, p2c, valueCopyed);
        }
        for (String p2c : propertiesWithNULLValueToCopy) {
            logger.trace("->create:\t\t copy null with SpringUtils");
            BeanUtils.copyProperty(plain, p2c, null);
        }
    } catch (Exception e) {
        logger.error("..in copy", e);
    }
}

From source file:com.doculibre.constellio.utils.EqualityUtils.java

public static boolean isEqualOrBothNullProperty(String propertyName, Object bean1, Object bean2) {
    boolean result;
    try {/*from   www  .ja  v a 2  s . c  o  m*/
        Object propertyValue1 = PropertyUtils.getProperty(bean1, propertyName);
        Object propertyValue2 = PropertyUtils.getProperty(bean2, propertyName);
        if (propertyValue1 == null && propertyValue2 == null) {
            result = true;
        } else if ((propertyValue1 == null && propertyValue2 != null)
                || (propertyValue1 != null && propertyValue2 == null)) {
            result = false;
        } else {
            result = propertyValue1.equals(propertyValue2);
        }
    } catch (IllegalAccessException e) {
        throw new RuntimeException(e);
    } catch (InvocationTargetException e) {
        throw new RuntimeException(e);
    } catch (NoSuchMethodException e) {
        throw new RuntimeException(e);
    }
    return result;
}

From source file:io.milton.cloud.process.BeanPropertyExpression.java

@Override
public Object eval(ProcessContext context) {
    Object val = null;
    try {/* w  ww .java 2  s . c  o m*/
        val = PropertyUtils.getProperty(context, beanPath);
    } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException ex) {
        throw new RuntimeException(beanPath, ex);
    }
    return val;
}

From source file:info.donsun.core.utils.Collections3.java

/**
 * ????(Getter), ??Map.//from  w  w w.  jav  a 2 s .  c  o m
 * 
 * @param collection ???.
 * @param keyPropertyName ????MapKey??.
 * @param valuePropertyName ????MapValue??.
 */

public static Map extractToMap(final Collection collection, final String keyPropertyName,
        final String valuePropertyName) {
    Map map = new HashMap(collection.size());
    try {
        for (Object obj : collection) {
            map.put(PropertyUtils.getProperty(obj, keyPropertyName),
                    PropertyUtils.getProperty(obj, valuePropertyName));
        }
    } catch (Exception e) {
        throw Reflections.convertReflectionExceptionToUnchecked(e);
    }

    return map;
}

From source file:cc.sion.core.utils.Collections3.java

/**
 * ????(Getter), ??List./* w  w  w .  jav  a2 s  . c  o m*/
 *
 * @param collection ???.
 * @param propertyName ??????.
 */
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;
}

From source file:com.wavemaker.json.BeanUtilsTest.java

public void testBasic() throws Exception {

    SampleClass sc = new SampleClass();

    assertEquals(sc.getSingleString(), PropertyUtils.getProperty(sc, "singleString"));
    assertEquals(sc.getStrings().get(1), PropertyUtils.getProperty(sc, "strings[1]"));
}

From source file:com.cnksi.core.tools.utils.Collections3.java

/**
 * ????(Getter), ??List.//from w w  w.  j a  va2  s .  c o m
 * 
 * @param collection ???.
 * @param propertyName ??????.
 */
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;
}