Example usage for org.springframework.beans BeanWrapper getPropertyValue

List of usage examples for org.springframework.beans BeanWrapper getPropertyValue

Introduction

In this page you can find the example usage for org.springframework.beans BeanWrapper getPropertyValue.

Prototype

@Nullable
Object getPropertyValue(String propertyName) throws BeansException;

Source Link

Document

Get the current value of the specified property.

Usage

From source file:com.aw.support.collection.ListUtils.java

public static <E> List<E> getSubList(Collection<E> list, final String propertyName,
        final Object propertyValue) {
    return getSubList(list, new UnaryPredicate() {
        @Override/*from   w w  w  .j a  v a 2  s . c om*/
        public boolean test(Object o) {
            BeanWrapper wrapper = new BeanWrapperImpl(o);
            return propertyValue.equals(wrapper.getPropertyValue(propertyName));
        }
    });
}

From source file:net.kamhon.ieagle.util.ReflectionUtil.java

/**
 * /*  www.ja va  2s  .  c  om*/
 * get all properties and it value which is public method which name get* if obj is collection->no browser in the
 * collection. only go in object in objPath package. Other than that just get the value and no go in to it
 * preperties.
 * 
 * @param obj
 *            = object that needed to print all the properties.
 * @param objIndicator
 *            = other object indication when vo contain other vo.
 * @param objPath
 *            = only browse object which class path contain objPath string.
 * @return
 */
private static StringBuilder getAllProperties(Object obj, String objIndicator, String objPath) {

    StringBuilder retAllPreperties = new StringBuilder();
    try {
        retAllPreperties.append("\n" + objIndicator + obj + "->");

        BeanWrapper beanO1 = new BeanWrapperImpl(obj);
        PropertyDescriptor[] proDescriptorsO1 = beanO1.getPropertyDescriptors();
        int propertyLength = proDescriptorsO1.length;
        for (int i = 0; i < propertyLength; i++) {
            String variable = proDescriptorsO1[i].getName();
            // String clazz =
            // proDescriptorsO1[i].getPropertyType().toString();
            try {
                Object propertyValueO1 = beanO1.getPropertyValue(variable);
                // compare whether it is vo. if not just print prepertie
                // else go in

                // if (propertyValueO1 == null){
                //
                // continue;
                //
                // // go in vo
                // }else if (propertyValueO1 instanceof Collection){
                // Collection collection = (Collection)propertyValueO1;
                // if (!collection.isEmpty()){
                // collection.toArray()[0].getClass();
                // }
                // }

                if (propertyValueO1 == null) {
                    retAllPreperties.append(variable + "=" + propertyValueO1 + ";");

                    // /** for Set/List **/
                    // }else if (propertyValueO1 instanceof Collection){
                    // Collection collection = (Collection)propertyValueO1;
                    // if (CollectionUtil.isNotEmpty(collection))
                    // for(Object obj2 : collection){
                    // getAllProperties(obj2,
                    // objIndicator + objIndicator,objPath);
                    // }
                } else if (propertyValueO1.getClass().toString().contains(objPath)) {

                    retAllPreperties
                            .append(getAllProperties(propertyValueO1, objIndicator + objIndicator, objPath));
                    retAllPreperties.append("\n" + objIndicator);
                } else {

                    retAllPreperties.append(variable + "=" + propertyValueO1 + ";");
                }
            } catch (Exception e) {

            }
        }
    } catch (Exception e) {

    }
    return retAllPreperties;
}

From source file:com.aw.support.collection.ListUtils.java

private static Object getPropertyValue(BeanWrapper wrap, String propertyNameValue,
        boolean assumeNullValueOnError) {
    Object value;/*ww  w . j  av  a  2  s .  c  o m*/
    if (assumeNullValueOnError)
        value = wrap.isReadableProperty(propertyNameValue) ? wrap.getPropertyValue(propertyNameValue) : null;
    else
        value = wrap.getPropertyValue(propertyNameValue);
    return value;
}

From source file:com.aw.support.collection.ListUtils.java

public static <E> List<E> columnFindList(Collection<E> list, final String[] propertyNames,
        final Object[] propertyValues) {
    final List result = new ArrayList();
    apply(list, null, new UnaryFunction() {

        @Override/*from   w ww . ja va 2s .co  m*/
        public Object evaluate(Object o) {
            BeanWrapper wrapper = new BeanWrapperImpl(o);
            for (int i = 0; i < propertyNames.length; i++) {
                String propName = propertyNames[i];
                Object propVal = wrapper.getPropertyValue(propName);
                if (!BeanUtils.equals(propVal, propertyValues[i]))
                    return null;
            }
            result.add(o);
            return null;
        }
    });
    return result;
}

From source file:org.codehaus.griffon.commons.GriffonClassUtils.java

/**
 * <p>Looks for a property of the reference instance with a given name.</p>
 * <p>If found its value is returned. We follow the Java bean conventions with augmentation for groovy support
 * and static fields/properties. We will therefore match, in this order:
 * </p>//from   ww w .  j  av a  2  s .  c  o m
 * <ol>
 * <li>Standard public bean property (with getter or just public field, using normal introspection)
 * <li>Public static property with getter method
 * <li>Public static field
 * </ol>
 *
 * @return property value or null if no property found
 */
public static Object getPropertyOrStaticPropertyOrFieldValue(Object obj, String name) //throws BeansException
{
    BeanWrapper ref = new BeanWrapperImpl(obj);
    if (ref.isReadableProperty(name)) {
        return ref.getPropertyValue(name);
    } else {
        // Look for public fields
        if (isPublicField(obj, name)) {
            return getFieldValue(obj, name);
        }

        // Look for statics
        Class clazz = obj.getClass();
        if (isStaticProperty(clazz, name)) {
            return getStaticPropertyValue(clazz, name);
        } else {
            return null;
        }
    }
}

From source file:com.aw.support.collection.ListUtils.java

public static StringBuffer concatenarSepPorStr(Collection cols, String propertyName, String sep,
        String enclosingStr) {/*  ww  w  .  j a v  a2 s .c om*/
    StringBuffer colsStr = new StringBuffer();
    BeanWrapper wrap = new BeanWrapperImpl();
    for (Iterator iterator = cols.iterator(); iterator.hasNext();) {
        Object col = iterator.next();
        if (propertyName != null) {
            wrap = new BeanWrapperImpl(col);
            col = wrap.getPropertyValue(propertyName);
        }
        if (enclosingStr != null)
            colsStr.append(enclosingStr);
        colsStr.append(col);
        if (enclosingStr != null)
            colsStr.append(enclosingStr);
        if (iterator.hasNext())
            colsStr.append(sep);
    }
    return colsStr;
}

From source file:net.solarnetwork.node.util.ClassUtils.java

/**
 * Get a Map of non-null <em>simple</em> bean properties for an object.
 * /*from w  w  w  . j a va 2  s  . c  o m*/
 * @param o
 *        the object to inspect
 * @param ignore
 *        a set of property names to ignore (optional)
 * @return Map (never <em>null</em>)
 * @since 1.1
 */
public static Map<String, Object> getSimpleBeanProperties(Object o, Set<String> ignore) {
    if (ignore == null) {
        ignore = DEFAULT_BEAN_PROP_NAME_IGNORE;
    }
    Map<String, Object> result = new LinkedHashMap<String, Object>();
    BeanWrapper bean = new BeanWrapperImpl(o);
    PropertyDescriptor[] props = bean.getPropertyDescriptors();
    for (PropertyDescriptor prop : props) {
        if (prop.getReadMethod() == null) {
            continue;
        }
        String propName = prop.getName();
        if (ignore != null && ignore.contains(propName)) {
            continue;
        }
        Class<?> propType = bean.getPropertyType(propName);
        if (!(propType.isPrimitive() || propType.isEnum() || String.class.isAssignableFrom(propType)
                || Number.class.isAssignableFrom(propType) || Character.class.isAssignableFrom(propType)
                || Byte.class.isAssignableFrom(propType) || Date.class.isAssignableFrom(propType))) {
            continue;
        }
        Object propValue = bean.getPropertyValue(propName);
        if (propValue == null) {
            continue;
        }
        if (propType.isEnum()) {
            propValue = propValue.toString();
        } else if (Date.class.isAssignableFrom(propType)) {
            propValue = ((Date) propValue).getTime();
        }
        result.put(propName, propValue);
    }
    return result;
}

From source file:org.springmodules.validation.util.condition.bean.PropertyBeanCondition.java

/**
 * Checks the value of the property of the given bean using the property condition associated with this condition. The
 * property to be checked is resolved by the property name associated with this condition.
 *
 * @param bean The bean to be checked.//from   ww  w.  j  av a2 s . c  om
 * @return <code>true</code> if the property condition associated with this condition returns <code>true</code> when
 *         checking the bean's property, <code>false</code> otherwise.
 */
protected boolean checkBean(BeanWrapper bean) {
    Object value = bean.getPropertyValue(propertyName);
    return propertyCondition.check(value);
}

From source file:com.oreilly.springdata.neo4j.PropertyPathValueMatcher.java

@Override
protected boolean matchesSafely(Object object) {

    BeanWrapper wrapper = new BeanWrapperImpl(object);
    Object value = wrapper.getPropertyValue(propertyPath);

    return expected == null ? value == null : expected.equals(value);
}

From source file:org.springmodules.validation.util.condition.bean.EqualPropertiesBeanCondition.java

/**
 * Checks whether a set of properties of the given bean have the same value. The properties are resolved based on
 * the property names associated with this condition.
 *
 * @param bean The bean to be checked.//from   w w  w.ja  v  a 2s.c  o m
 * @return <code>true</code> if all the compared properties are equal, <code>false</code> otherwise.
 */
protected boolean checkBean(BeanWrapper bean) {
    Object value = bean.getPropertyValue(propertyNames[0]);
    for (int i = 1; i < propertyNames.length; i++) {
        Object currentValue = bean.getPropertyValue(propertyNames[i]);
        if (!ObjectUtils.nullSafeEquals(value, currentValue)) {
            return false;
        }
    }
    return true;
}