集册 Java实例教程 获取Java Bean属性图

获取Java Bean属性图

欢马劈雪     最近更新时间:2020-01-02 10:19:05

401
获取Java Bean属性图


import java.beans.BeanInfo;

import java.beans.IndexedPropertyDescriptor;

import java.beans.IntrospectionException;
/*
n o w    j a v a  . c o m 提供
*/

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 = "nowjava.com";
        /* 
         来自 
        *时 代 J     a    v  a - nowjava.com*/

        System.out.println(getPropertyMap(bean));

    }

    /**

     * @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,

            
展开阅读全文