get Indexed Property of Java Bean - Java Reflection

Java examples for Reflection:Java Bean

Description

get Indexed Property of Java Bean

Demo Code


//package com.java2s;
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,
            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 {/*from ww  w . ja v  a  2  s  .c  om*/
            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 (int i = 0; i < descriptors.length; i++) {
            if (name.equals(descriptors[i].getName()))
                return descriptors[i];
        }
        return null;
    }

    public static PropertyDescriptor[] getPropertyDescriptors(Object bean) {
        BeanInfo beanInfo = null;
        try {
            beanInfo = Introspector.getBeanInfo(bean.getClass());
        } catch (IntrospectionException e) {
            return (new PropertyDescriptor[0]);
        }
        PropertyDescriptor[] descriptors = beanInfo
                .getPropertyDescriptors();
        return descriptors;
    }
}

Related Tutorials