get Java Bean Property Map - Java Reflection

Java examples for Reflection:Java Bean

Description

get Java Bean Property Map

Demo Code


import java.beans.BeanInfo;
import java.beans.IndexedPropertyDescriptor;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Collections;
import java.util.Map;
import java.util.TreeMap;

public class Main{
    public static void main(String[] argv) throws Exception{
        Object bean = "java2s.com";
        System.out.println(getPropertyMap(bean));
    }// w w w  . j  av a  2s  .  c  om
    /**
     * @param bean
     * @param properties
     * @return Map
     * @throws NoSuchMethodException
     * @throws IllegalArgumentException
     * @throws IllegalAccessException
     * @throws InvocationTargetException
     * @throws IntrospectionException
     */
    public static Map<String, Object> getPropertyMap(Object bean,
            String[] properties) throws NoSuchMethodException,
            IllegalArgumentException, IllegalAccessException,
            InvocationTargetException, IntrospectionException {
        properties = properties != null ? properties
                : ArrayHelper.EMPTY_STRING_ARRAY;
        final Class<?> clazz = bean.getClass();
        Map<String, Object> map = null;
        for (int i = 0, n = properties.length; i < n; ++i) {
            final String name = properties[i];
            final PropertyDescriptor pd = BeanHelper.getPropertyDescriptor(
                    clazz, name);
            if (pd == null) {
                String msg = "could not get property=" + name;
                throw new NoSuchMethodException(msg);
            }
            final Method method = pd.getReadMethod();
            if (method == null) {
                String msg = "could not get getter=" + name;
                throw new NoSuchMethodException(msg);
            }
            final Object value = method.invoke(bean, (Object[]) null);
            if (map == null) {
                map = new TreeMap<String, Object>();
            }
            map.put(name, value);
        }
        if (map == null) {
            return Collections.emptyMap();
        }
        return map;
    }
    public static Map<String, Object> getPropertyMap(Object bean)
            throws IntrospectionException {
        final Class<?> clazz = bean.getClass();
        final PropertyDescriptor[] array = BeanHelper
                .getPropertyDescriptors(clazz);
        Map<String, Object> map = null;
        for (int i = 0, n = array.length; i < n; ++i) {
            PropertyDescriptor pd = array[i];
            Method method = pd.getReadMethod();
            if (method == null) {
                continue;
            }
            try {
                Object value = method.invoke(bean, (Object[]) null);
                if (map == null) {
                    map = new TreeMap<String, Object>();
                }
                map.put(pd.getName(), value);
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
        if (map == null) {
            return Collections.emptyMap();
        }
        return map;
    }
    public static PropertyDescriptor getPropertyDescriptor(Class<?> clazz,
            String name) throws IntrospectionException {
        final PropertyDescriptor[] array = BeanHelper
                .getPropertyDescriptors(clazz);
        for (int i = 0, n = array.length; i < n; ++i) {
            if (array[i].getName().equals(name)) {
                return array[i];
            }
        }
        return null;
    }
    public static PropertyDescriptor[] getPropertyDescriptors(Class<?> clazz)
            throws IntrospectionException {
        final BeanInfo info = Introspector.getBeanInfo(clazz);
        return info.getPropertyDescriptors();
    }
}

Related Tutorials