Example usage for org.springframework.beans BeanWrapper getPropertyDescriptors

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

Introduction

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

Prototype

PropertyDescriptor[] getPropertyDescriptors();

Source Link

Document

Obtain the PropertyDescriptors for the wrapped object (as determined by standard JavaBeans introspection).

Usage

From source file:tetrad.rrd.ReflectTest.java

/**
 * @param args// ww  w.ja v a 2  s .co  m
 * @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:org.ng200.openolympus.util.Beans.java

public static <T> void copy(T from, T to) {
    final BeanWrapper src = new BeanWrapperImpl(from);
    final BeanWrapper trg = new BeanWrapperImpl(to);

    for (final String propertyName : Stream.of(src.getPropertyDescriptors()).map(pd -> pd.getName())
            .collect(Collectors.toList())) {
        if (!trg.isWritableProperty(propertyName)) {
            continue;
        }//from w w w.  j a v  a 2 s .  c  om

        trg.setPropertyValue(propertyName, src.getPropertyValue(propertyName));
    }
}

From source file:com.evolveum.midpoint.provisioning.ucf.api.UcfUtil.java

public static PropertyDescriptor findAnnotatedProperty(BeanWrapper connectorBean,
        Class<? extends Annotation> annotationClass) {
    for (PropertyDescriptor prop : connectorBean.getPropertyDescriptors()) {
        if (hasAnnotation(prop, annotationClass)) {
            return prop;
        }// w w w .ja  v  a2  s .  c o m
    }
    return null;
}

From source file:com.aw.support.beans.BeanUtils.java

public static int countPropertyFilled(Object bean) {
    BeanWrapper wrap = new BeanWrapperImpl(bean);
    int count = 0;
    for (PropertyDescriptor descriptor : wrap.getPropertyDescriptors()) {
        if (descriptor.getReadMethod() == null || descriptor.getWriteMethod() == null)
            continue;
        Object value = wrap.getPropertyValue(descriptor.getName());
        if (value instanceof String) {
            if (StringUtils.hasText((String) value))
                count++;//from w ww  .ja v a2s . co m
        } else if (value != null)
            count++;
    }
    return count;
}

From source file:gov.nih.nci.cabig.ctms.web.WebTools.java

public static SortedMap<String, Object> requestPropertiesToMap(HttpServletRequest request) {
    BeanWrapper wrapped = new BeanWrapperImpl(request);
    SortedMap<String, Object> map = new TreeMap<String, Object>();
    for (PropertyDescriptor descriptor : wrapped.getPropertyDescriptors()) {
        String name = descriptor.getName();
        if (!EXCLUDED_REQUEST_PROPERTIES.contains(name) && descriptor.getReadMethod() != null) {
            Object propertyValue;
            try {
                propertyValue = wrapped.getPropertyValue(name);
            } catch (InvalidPropertyException e) {
                log.debug("Exception reading request property " + name, e);
                propertyValue = e.getMostSpecificCause();
            }/*  www  . j a  va2 s  .  c o m*/
            map.put(name, propertyValue);
        }
    }
    return map;
}

From source file:com.aw.support.beans.BeanUtils.java

public static Object copyProperties(Object source, Object target, String[] propertyNamesToIgnore,
        boolean ignoreProxy, boolean ignoreCollections) {
    List<String> propertyNamesToIgnoreList = propertyNamesToIgnore == null ? Collections.EMPTY_LIST
            : Arrays.asList(propertyNamesToIgnore);
    BeanWrapper sourceWrap = new BeanWrapperImpl(source);
    BeanWrapper targetWrap = new BeanWrapperImpl(target);
    for (PropertyDescriptor propDescriptor : sourceWrap.getPropertyDescriptors()) {
        String propName = propDescriptor.getName();
        //chequear que no esta en la lista a ignorar
        if (propertyNamesToIgnoreList.contains(propName))
            continue;
        //chequear que se puede leer
        if (!sourceWrap.isReadableProperty(propName))
            continue;
        //chequear que se puede escribir
        if (!targetWrap.isWritableProperty(propName))
            continue;

        Object sourceValue = sourceWrap.getPropertyValue(propName);

        //chequear que objeto no es un proxy
        if (ignoreProxy && sourceValue != null && Proxy.isProxyClass(sourceValue.getClass()))
            continue;

        //chequear que objeto no una collection
        if (ignoreCollections && sourceValue instanceof Collection)
            continue;

        targetWrap.setPropertyValue(propName, sourceValue);
    }// w ww .  j  av  a 2s. com
    return target;
}

From source file:com.allinfinance.system.util.BeanUtils.java

/**
 * null??//from  ww w.j a va2 s. c om
* @param source
* @return
*/
public static String[] getNullPropertyNames(Object source) {
    final BeanWrapper src = new BeanWrapperImpl(source);
    java.beans.PropertyDescriptor[] pds = src.getPropertyDescriptors();

    Set<String> emptyNames = new HashSet<String>();
    for (java.beans.PropertyDescriptor pd : pds) {
        Object srcValue = src.getPropertyValue(pd.getName());
        if (srcValue == null)
            emptyNames.add(pd.getName());
    }
    String[] result = new String[emptyNames.size()];
    return emptyNames.toArray(result);
}

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

/**
 * Get a Map of non-null bean properties for an object.
 * /*  w w w  .  j  a  v a 2s.  c  om*/
 * @param o
 *        the object to inspect
 * @param ignore
 *        a set of property names to ignore (optional)
 * @return Map (never null)
 */
public static Map<String, Object> getBeanProperties(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;
        }
        Object propValue = bean.getPropertyValue(propName);
        if (propValue == null) {
            continue;
        }
        result.put(propName, propValue);
    }
    return result;
}

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

/**
 * Get a Map of non-null <em>simple</em> bean properties for an object.
 * //from ww  w .  jav a2s  . co 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.shept.util.BeanUtilsExtended.java

/**
 * Find all occurrences of targetClass in targetObject.
 * Be careful. This stuff may be recursive !
 * Should be improved to prevent endless recursive loops.
 * /*from  w ww  .  ja va 2 s.  c om*/
 * @param
 * @return
 *
 * @param targetObject
 * @param targetClass
 * @param nestedPath
 * @return
 * @throws Exception 
 */
public static Map<String, ?> findNestedPaths(Object targetObject, Class<?> targetClass, String nestedPath,
        List<String> ignoreList, Set<Object> visited, int maxDepth) throws Exception {

    Assert.notNull(targetObject);
    Assert.notNull(targetClass);
    Assert.notNull(ignoreList);
    HashMap<String, Object> nestedPaths = new HashMap<String, Object>();
    if (maxDepth <= 0) {
        return nestedPaths;
    }

    nestedPath = (nestedPath == null ? "" : nestedPath);
    BeanWrapper bw = PropertyAccessorFactory.forBeanPropertyAccess(targetObject);
    PropertyDescriptor[] props = bw.getPropertyDescriptors();

    for (PropertyDescriptor pd : props) {
        Class<?> clazz = pd.getPropertyType();
        if (clazz != null && !clazz.equals(Class.class) && !clazz.isAnnotation() && !clazz.isPrimitive()) {
            Object value = null;
            String pathName = pd.getName();
            if (!ignoreList.contains(pathName)) {
                try {
                    value = bw.getPropertyValue(pathName);
                } catch (Exception e) {
                } // ignore any exceptions here
                if (StringUtils.hasText(nestedPath)) {
                    pathName = nestedPath + PropertyAccessor.NESTED_PROPERTY_SEPARATOR + pathName;
                }
                // TODO break up this stuff into checking and excecution a la ReflectionUtils
                if (targetClass.isAssignableFrom(clazz)) {
                    nestedPaths.put(pathName, value);
                }
                // exclude objects already visited from further inspection to prevent circular references
                // unfortunately this stuff isn't fool proof as there are ConcurrentModificationExceptions
                // when adding objects to the visited list
                if (value != null && !isInstanceVisited(visited, value)) {
                    nestedPaths.putAll(
                            findNestedPaths(value, targetClass, pathName, ignoreList, visited, maxDepth - 1));
                }
            }
        }
    }
    return nestedPaths;
}