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:tetrad.rrd.ReflectTest.java

/**
 * @param args/*from w w w . ja v  a  2 s . c om*/
 * @throws IllegalAccessException 
 * @throws IllegalArgumentException 
 * @throws InvocationTargetException 
 */
public static void main(String[] args)
        throws IllegalArgumentException, IllegalAccessException, InvocationTargetException {
    // TODO Auto-generated method stub   
    ServerStatus serverstatus = new ServerStatus();
    serverstatus.setDeviceCode(12);
    serverstatus.setType("a");

    BeanWrapper wrapper = new BeanWrapperImpl(serverstatus);

    PropertyDescriptor[] ps = wrapper.getPropertyDescriptors();

    for (PropertyDescriptor p : ps) {
        String s = p.getName();
        System.out.println(s + " : " + wrapper.getPropertyValue(s));

    }

    //      Field[] fields = serverstatus.getClass().getDeclaredFields();
    //      Method[] methods = serverstatus.getClass().getMethods();
    //      
    //      int idex = 0;
    //      for (Field field : fields) {
    //         System.out.println("name : " + field.getName());
    //         System.out.println("name : " + field.getType());
    //         Method method = methods[idex];
    //         if (method.getReturnType() == String.class) {
    //            System.out.println(method.invoke(method.getName(), ""));
    //         }

    //         Object objValue = field.get(Object.class);
    //         if (objValue instanceof java.lang.String) {
    //            System.out.println(objValue.toString());
    //         } else if (objValue instanceof java.lang.Integer) {
    //            System.out.println(((java.lang.Integer) objValue).intValue());
    //         } else if (objValue instanceof java.lang.Double) {
    //            System.out.println(((java.lang.Double) objValue).doubleValue());
    //         } else if (objValue instanceof java.lang.Float) {
    //            System.out.println(((java.lang.Float) objValue).floatValue());
    //         } else {
    //            System.out.println(objValue);
    //         }
    //         idex++;
    //      }
}

From source file:Main.java

public static int countValue(String propertyName, Object value, Collection<?> collection) {
    int count = 0;
    for (Object o : collection) {
        BeanWrapper bw = new BeanWrapperImpl(o);
        Object propertyValue = bw.getPropertyValue(propertyName);
        if (propertyValue.equals(value)) {
            count++;//from  w ww. j  a v  a2 s  . co  m
        }
    }
    return count;
}

From source file:Main.java

public static boolean valueExists(String propertyName, Object value, Collection<?> collection) {

    for (Object o : collection) {
        BeanWrapper bw = new BeanWrapperImpl(o);
        Object propertyValue = bw.getPropertyValue(propertyName);
        if (propertyValue.equals(value)) {
            return true;
        }/*from  ww w .  j  a  va  2s  .  c  o  m*/
    }
    return false;
}

From source file:Main.java

public static Collection<?> filterByValue(String propertyName, Object value, Collection<?> collection) {
    List<Object> list = new ArrayList<Object>(collection.size());
    for (Object o : collection) {
        BeanWrapper bw = new BeanWrapperImpl(o);
        Object propertyValue = bw.getPropertyValue(propertyName);
        if (propertyValue.equals(value)) {
            list.add(o);/*from ww  w .  j  av  a 2 s.  c  o  m*/
        }
    }
    return list;
}

From source file:Main.java

public static Collection<?> filterByNotValue(String propertyName, Object value, Collection<?> collection) {
    List<Object> list = new ArrayList<Object>(collection.size());
    for (Object o : collection) {
        BeanWrapper bw = new BeanWrapperImpl(o);
        Object propertyValue = bw.getPropertyValue(propertyName);
        if (!propertyValue.equals(value)) {
            list.add(o);//from   w ww.jav  a2  s .c om
        }
    }
    return list;
}

From source file:Main.java

public static int countTrue(String propertyName, Collection<?> collection) {

    int count = 0;
    for (Object o : collection) {
        BeanWrapper bw = new BeanWrapperImpl(o);
        Boolean propertyValue = (Boolean) bw.getPropertyValue(propertyName);
        if (Boolean.TRUE.equals(propertyValue)) {
            count++;//from   www  .  j  av a2  s. c  om
        }
    }
    return count;
}

From source file:Main.java

public static int countFalse(String propertyName, Collection<?> collection) {

    int count = 0;
    for (Object o : collection) {
        BeanWrapper bw = new BeanWrapperImpl(o);
        Boolean propertyValue = (Boolean) bw.getPropertyValue(propertyName);
        if (Boolean.FALSE.equals(propertyValue)) {
            count++;/*ww  w  .ja v a 2s .c om*/
        }
    }
    return count;
}

From source file:Main.java

/**
 * Returns the first element within the given {@link Collection} which has the given field set to the given value.
 *
 * @param <E> elements of the {@link Collection}
 * @param col the {@link Collection}/*w w w  .j  a  v  a  2s .  c  o  m*/
 * @param fieldName the name of the property (e.g. "key" for getKey())
 * @param value the value to compare (using equals) against the returned value of the object
 * @return the first object whose field equals the given value or null if none found
 */
public static <E> E getRealMatch(Collection<E> col, String fieldName, String value) {

    if (col != null) {
        for (E e : col) {
            BeanWrapper w = new BeanWrapperImpl(e);

            if (fieldName != null && value != null && value.equals(w.getPropertyValue(fieldName))) {
                return e;
            }
        }
    }

    return null;
}

From source file:org.lightadmin.core.util.NamingUtils.java

public static String entityId(DomainTypeAdministrationConfiguration domainTypeAdministrationConfiguration,
        Object entity) {//from  w ww . java2  s .co  m
    if (entity == null) {
        return null;
    }

    PersistentEntity persistentEntity = domainTypeAdministrationConfiguration.getPersistentEntity();
    PersistentProperty idProperty = persistentEntity.getIdProperty();

    BeanWrapper beanWrapper = new DirectFieldAccessFallbackBeanWrapper(entity);

    return String.valueOf(beanWrapper.getPropertyValue(idProperty.getName()));
}

From source file:org.jdal.util.BeanUtils.java

/**
 * Get property value null if none/*  ww w .java2 s . c om*/
 * @param bean beam
 * @param name name
 * @return the property value
 */
public static Object getProperty(Object bean, String name) {
    try {
        BeanWrapper wrapper = new BeanWrapperImpl(bean);
        return wrapper.getPropertyValue(name);
    } catch (BeansException be) {
        log.error(be);
        return null;
    }
}