get a property's value from a bean object; - Java Reflection

Java examples for Reflection:Java Bean

Description

get a property's value from a bean object;

Demo Code

/**//from   ww  w  .jav a 2  s. c om
 *all rights reserved,@copyright 2003
 */
import java.lang.reflect.Method;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.HashMap;

public class Main{
    public static void main(String[] argv) throws Exception{
        Object bean = "java2s.com";
        String propertyName = "java2s.com";
        System.out.println(getBeanProperty(bean,propertyName));
    }
    public static final String PROPERTY_GET_METHOD_PREFIX = "get";
    public static final String PROPERTY_IS_METHOD_PREFIX = "is";
    /**
     * get a property's value from a bean object;
     *
     * @param bean the bean object;
     * @param propertyName the property name;
     * @return the Value of the property;
     * @throws NoSuchPropertyException
     * @throws GetPropertyException
     */
    public static Object getBeanProperty(Object bean, String propertyName)
            throws NoSuchPropertyException, GetPropertyException {
        //try getXXX first;
        String methodName = BeanUtil.PROPERTY_GET_METHOD_PREFIX
                + Character.toUpperCase(propertyName.charAt(0))
                + propertyName.substring(1);
        Class[] clz = new Class[0];
        try {
            Method m = getMethod(bean, methodName, clz);
            return m.invoke(bean, new Object[0]);
        } catch (NoSuchMethodException e) {

        } catch (IllegalAccessException e) {
            throw new GetPropertyException("illegal access to property:"
                    + propertyName);
        } catch (InvocationTargetException e) {
            throw new GetPropertyException(e.getTargetException()
                    .getMessage());
        }

        //if no getXXX, then try isXXX;
        methodName = BeanUtil.PROPERTY_IS_METHOD_PREFIX
                + Character.toUpperCase(propertyName.charAt(0))
                + propertyName.substring(1);
        try {
            Method m = getMethod(bean, methodName, clz);
            return m.invoke(bean, new Object[0]);
        } catch (NoSuchMethodException e) {
            throw new NoSuchPropertyException(bean.getClass().getName(),
                    propertyName);
        } catch (IllegalAccessException e) {
            throw new GetPropertyException("illegal access to property:"
                    + propertyName);
        } catch (InvocationTargetException e) {
            throw new GetPropertyException(e.getTargetException()
                    .getMessage());
        }
    }
    /**
     *
     *
     * @param o
     * @param methodName
     * @param paramTypes
     * @return
     * @throws NoSuchMethodException
     */
    public static Method getMethod(Object o, String methodName,
            Class[] paramTypes) throws NoSuchMethodException {
        Class clz = o.getClass();
        return clz.getMethod(methodName, paramTypes);
    }
    /**
     *
     *
     * @param clz
     * @param methodName
     * @param paramTypes
     * @return
     * @throws NoSuchMethodException
     */
    public static Method getMethod(Class clz, String methodName,
            Class[] paramTypes) throws NoSuchMethodException {
        return clz.getMethod(methodName, paramTypes);
    }
}

Related Tutorials