get Java Bean Property by Name - Java Reflection

Java examples for Reflection:Java Bean

Description

get Java Bean Property by Name

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";
        String name = "java2s.com";
        System.out.println(getProperty(bean,name));
    }/*from w  w w . j  a v a  2 s.c o m*/
    public static Object getProperty(Object bean, String name)
            throws NoSuchMethodException, IllegalArgumentException,
            IllegalAccessException, InvocationTargetException,
            IntrospectionException {
        Debug.isNotNull("bean", bean);
        final Class<?> clazz = bean.getClass();
        final PropertyDescriptor pd = BeanHelper.getPropertyDescriptor(
                clazz, name);
        if (pd == null) {
            final String msg = BeanHelper.getNoSuchPropertyMessage(name,
                    clazz);
            throw new NoSuchMethodException(msg);
        } else if (pd instanceof IndexedPropertyDescriptor) {
            final String msg = BeanHelper.getUnsupportedPropertyMessage(
                    name, clazz);
            throw new IllegalArgumentException(msg);
        }
        Method getter = pd.getReadMethod();
        if (getter == null) {
            final String msg = BeanHelper.getNoSuchPropertyMessage(name,
                    clazz);
            throw new NoSuchMethodException(msg);
        }
        return getter.invoke(bean, (Object[]) null);
    }
    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;
    }
    protected static String getNoSuchPropertyMessage(String name,
            Class<?> clazz) {
        return "could not find poperty=" + name + " of class="
                + (clazz != null ? clazz.getCanonicalName() : null);
    }
    protected static String getUnsupportedPropertyMessage(String name,
            Class<?> clazz) {
        return "not supported poperty=" + name + " of class="
                + (clazz != null ? clazz.getCanonicalName() : null);
    }
    public static PropertyDescriptor[] getPropertyDescriptors(Class<?> clazz)
            throws IntrospectionException {
        final BeanInfo info = Introspector.getBeanInfo(clazz);
        return info.getPropertyDescriptors();
    }
}

Related Tutorials