集册 Java实例教程 获取Java Bean的嵌套属性

获取Java Bean的嵌套属性

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

1067
获取Java Bean的嵌套属性
/*N  o w  J a v a . c o m 提 供*/

//package com.nowjava;

import java.beans.BeanInfo;

import java.beans.IndexedPropertyDescriptor;

import java.beans.IntrospectionException;

import java.beans.Introspector;

import java.beans.PropertyDescriptor;

import java.lang.reflect.Array;

import java.lang.reflect.InvocationTargetException;

import java.lang.reflect.Method;


public class Main {

    private static final String INDEXED_DELIM = "[";

    private static final String INDEXED_DELIM2 = "]";

    private static final String MAPPED_DELIM = "(";

    private static final String MAPPED_DELIM2 = ")";

    private static final String NESTED_DELIM = ".";


    public static Object getNestedProperty(Object bean, String name)
    /*来自 
     N o w J a v a . c o m - 时  代  Java*/

            throws IllegalArgumentException, IllegalAccessException,

            InvocationTargetException {

        String[] properties = name.split("\\" + NESTED_DELIM);

        for (int i = 0; i < properties.length; i++) {

            if (properties[i].contains(INDEXED_DELIM)) {

                bean = getIndexedProperty(bean, properties[i]);

                continue;

            }

            if (properties[i].contains(MAPPED_DELIM)) {

                bean = getMappedProperty(bean, properties[i]);

                continue;

            }

            bean = getSimpleProperty(bean, properties[i]);

        }

        return bean;

    }


    public static Object getIndexedProperty(Object bean, String name)

            throws IllegalArgumentException, IllegalAccessException,

            InvocationTargetException {

        int delim = name.indexOf(INDEXED_DELIM);

        int delim2 = name.indexOf(INDEXED_DELIM2);

        if ((delim < 0) || (delim2 <= delim))

            throw new IllegalArgumentException("Invalid indexed property '"

                    + name + "'");

        int index = -1;

        try {

            String subscript = name.substring(delim + 1, delim2);

            index = Integer.parseInt(subscript);

        } catch (NumberFormatException e) {

            throw new IllegalArgumentException("Invalid indexed property '"

                    + name + "'");

        }

        name = name.substring(0, delim);

        return getIndexedProperty(bean, name, index);

    }


    public static Object getIndexedProperty(Object bean, String name,

            int index) throws IllegalArgumentException,

            IllegalAccessException, InvocationTargetException {

        PropertyDescriptor propertyDescriptor = getPropertyDescriptor(bean,

                name);

        if (propertyDescriptor == null)

            throw new IllegalArgumentException("No property:" + name);

        if (propertyDescriptor instanceof IndexedPropertyDescriptor) {

            Method readMethod = ((IndexedPropertyDescriptor) propertyDescriptor)

                    .getIndexedReadMethod();

            if (readMethod == null)

                throw new IllegalArgumentException(

                        "No readMethod for property:" + name);

            Object value = readMethod.invoke(bean,

                    new Object[] { new Integer(index) });

            return value;

        }

        Method readMethod = propertyDescriptor.getReadMethod();

        if (readMethod == null)

            throw new IllegalArgumentException(

                    "No readMethod for property:" + name);

        Object array = readMethod.invoke(bean, new Object[0]);

        if (array.getClass().isArray()) {

            return Array.get(array, index);

        }

        if (!(array instanceof java.util.List))

            throw new IllegalArgumentException("Property '" + name

                    + "' is not indexed");

        return ((java.util.List<?>) array).get(index);

    }


    public static Object getMappedProperty(Object bean, String name)

            throws IllegalArgumentException, IllegalAccessException,

            InvocationTargetException {

        int delim = name.indexOf(MAPPED_DELIM);

        int delim2 = name.indexOf(MAPPED_DELIM2);

        if ((delim < 0) || (delim2 <= delim))

            throw new IllegalArgumentException("Invalid mapped property '"

                    + name + "'");

        String subscript = name.substring(delim + 1, delim2);

        name = name.substring(0, delim);

        return getMappedProperty(bean, name, subscript);

    }


    public static Object getMappedProperty(Object bean, String name,

            String key) throws IllegalArgumentException,

            IllegalAccessException, InvocationTargetException {

        PropertyDescriptor propertyDescriptor = getPropertyDescriptor(bean,

                name);

        if (propertyDescriptor == null)

            throw new IllegalArgumentException("No property:" + name);

        Method readMethod = propertyDescriptor.getReadMethod();

        if (readMethod == null)

            throw new IllegalArgumentException(

                    "No readMethod for property:" + name);

        Object map = readMethod.invoke(bean, new Object[0]);

        if (!(map instanceof java.util.Map))

            throw new IllegalArgumentException("Property '" + name

                    + "' is not mapped");

        return ((java.util.Map<?, ?>) map).get(key);

    }


    public static Object getSimpleProperty(Object bean, String name)

            throws IllegalArgumentException, IllegalAccessException,

            InvocationTargetException {

        Method readMethod = getSimplePropertyReadMethod(bean, name);

        if (readMethod == null)

            throw new IllegalArgumentException(

                    "No readMethod for property:" + name);

        readMethod.setAccessible(true);

        Object value = readMethod.invoke(bean, new Object[0]);

        return value;

    }


    public static 
展开阅读全文