Example usage for org.springframework.beans BeanUtils getPropertyDescriptors

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

Introduction

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

Prototype

public static PropertyDescriptor[] getPropertyDescriptors(Class<?> clazz) throws BeansException 

Source Link

Document

Retrieve the JavaBeans PropertyDescriptor s of a given class.

Usage

From source file:org.medici.bia.common.util.ListBeanUtils.java

/**
 * Method to obtains a plain list fomr a bean input list.
 * //from   w ww . ja  v a2  s.  c  o  m
 * @param beanList List object containing beans having the field to extract.
 * @param fieldName Field name to extract
 * @return List<Object> Result list containing the specific input field of input list.
 **/
public static List<Object> transformToPlainList(List<Object> beansList) {
    if ((beansList == null) || (beansList.size() == 0)) {
        return new ArrayList<Object>(0);
    }

    List<Object> arrayList = new ArrayList<Object>(beansList.size());

    PropertyDescriptor[] propertyDescriptors = BeanUtils.getPropertyDescriptors(beansList.get(0).getClass());

    for (int i = 0; i < beansList.size(); i++) {
        List<Object> singleBean = new ArrayList<Object>(propertyDescriptors.length);

        for (int j = 0; j < propertyDescriptors.length; j++) {
            Method method = propertyDescriptors[i].getReadMethod();
            try {
                singleBean.add(method.invoke(beansList.get(i), (Object[]) null));
            } catch (InvocationTargetException invocationTargetException) {
                logger.debug(invocationTargetException);
                singleBean.add("");
            } catch (IllegalAccessException illegalAccessException) {
                logger.debug(illegalAccessException);
                singleBean.add("");
            }
        }
    }

    return arrayList;
}

From source file:org.springframework.boot.actuate.metrics.reader.MetricRegistryMetricReader.java

private static Set<String> getNumberKeys(Object metric) {
    Set<String> result = numberKeys.get(metric.getClass());
    if (result == null) {
        result = new HashSet<String>();
    }/*from w w w .java  2 s  .  c  o m*/
    if (result.isEmpty()) {
        for (PropertyDescriptor descriptor : BeanUtils.getPropertyDescriptors(metric.getClass())) {
            if (ClassUtils.isAssignable(Number.class, descriptor.getPropertyType())) {
                result.add(descriptor.getName());
            }
        }
        numberKeys.put(metric.getClass(), result);
    }
    return result;
}

From source file:org.springframework.boot.bind.PropertiesConfigurationFactory.java

private Set<String> getNames() {
    Set<String> names = new LinkedHashSet<String>();
    if (this.target != null) {
        Iterable<String> prefixes = (StringUtils.hasLength(this.targetName) ? new RelaxedNames(this.targetName)
                : null);//from  w w  w  .  ja v  a2s  . c  o m
        PropertyDescriptor[] descriptors = BeanUtils.getPropertyDescriptors(this.target.getClass());
        for (PropertyDescriptor descriptor : descriptors) {
            String name = descriptor.getName();
            if (!name.equals("class")) {
                RelaxedNames relaxedNames = RelaxedNames.forCamelCase(name);
                if (prefixes == null) {
                    for (String relaxedName : relaxedNames) {
                        names.add(relaxedName);
                    }
                } else {
                    for (String prefix : prefixes) {
                        for (String relaxedName : relaxedNames) {
                            names.add(prefix + "." + relaxedName);
                            names.add(prefix + "_" + relaxedName);
                        }
                    }
                }
            }
        }
    }
    return names;
}

From source file:org.springframework.data.jdbc.support.oracle.BeanPropertyStructMapper.java

/**
 * Initialize the mapping metadata for the given class.
 * @param mappedClass the mapped class./*from   www  . ja  va 2 s. c o m*/
 */
protected void initialize(Class<T> mappedClass) {
    this.mappedClass = mappedClass;
    this.mappedFields = new HashMap<String, PropertyDescriptor>();
    PropertyDescriptor[] pds = BeanUtils.getPropertyDescriptors(mappedClass);
    for (int i = 0; i < pds.length; i++) {
        PropertyDescriptor pd = pds[i];
        if (pd.getWriteMethod() != null) {
            this.mappedFields.put(pd.getName().toLowerCase(), pd);
            String underscoredName = underscoreName(pd.getName());
            if (!pd.getName().toLowerCase().equals(underscoredName)) {
                this.mappedFields.put(underscoredName, pd);
            }
        }
    }
}

From source file:org.springframework.integration.json.SimpleJsonSerializer.java

/**
 * Convert the bean to JSON with the provided properties.
 * @param bean the object to serialize.//from www.  jav  a2 s.c o  m
 * @param propertiesToExclude the property names to ignore.
 * @return the JSON.
 */
public static String toJson(Object bean, String... propertiesToExclude) {
    PropertyDescriptor[] propertyDescriptors = BeanUtils.getPropertyDescriptors(bean.getClass());
    Set<String> excluded = new HashSet<>(Arrays.asList(propertiesToExclude));
    excluded.add("class");
    final StringBuilder stringBuilder = new StringBuilder("{");
    final Object[] emptyArgs = new Object[0];
    for (PropertyDescriptor descriptor : propertyDescriptors) {
        String propertyName = descriptor.getName();
        Method readMethod = descriptor.getReadMethod();
        if (!excluded.contains(propertyName) && readMethod != null) {
            stringBuilder.append(toElement(propertyName)).append(":");
            Object result;
            try {
                result = readMethod.invoke(bean, emptyArgs);
            } catch (InvocationTargetException | IllegalAccessException | IllegalArgumentException e) {
                if (logger.isDebugEnabled()) {
                    logger.debug("Failed to serialize property " + propertyName, e);
                }
                result = e.getMessage();
            }
            stringBuilder.append(toElement(result)).append(",");
        }
    }
    stringBuilder.setLength(stringBuilder.length() - 1);
    stringBuilder.append("}");
    if (stringBuilder.length() == 1) {
        return null;
    } else {
        return stringBuilder.toString();
    }
}

From source file:org.springframework.jdbc.core.BeanPropertyRowMapper.java

/**
 * Initialize the mapping metadata for the given class.
 * @param mappedClass the mapped class//from   www . ja va  2  s .c om
 */
protected void initialize(Class<T> mappedClass) {
    this.mappedClass = mappedClass;
    this.mappedFields = new HashMap<>();
    this.mappedProperties = new HashSet<>();
    PropertyDescriptor[] pds = BeanUtils.getPropertyDescriptors(mappedClass);
    for (PropertyDescriptor pd : pds) {
        if (pd.getWriteMethod() != null) {
            this.mappedFields.put(lowerCaseName(pd.getName()), pd);
            String underscoredName = underscoreName(pd.getName());
            if (!lowerCaseName(pd.getName()).equals(underscoredName)) {
                this.mappedFields.put(underscoredName, pd);
            }
            this.mappedProperties.add(pd.getName());
        }
    }
}