集册 Java实例教程 获取Java Bean的索引属性

获取Java Bean的索引属性

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

532
获取Java Bean的索引属性
/* from 
nowjava - 时  代  Java*/


//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 = "]";


    public static Object getIndexedProperty(Object bean, String name)

            throws IllegalArgumentException, IllegalAccessException,/** from 时代Java - N o w  J a v a . c o m**/

            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 PropertyDescriptor getPropertyDescriptor(Object bean,

            String name) {

        PropertyDescriptor[] descriptors = getPropertyDescriptors(bean);

        for (
展开阅读全文