parse Getter - Java Reflection

Java examples for Reflection:Getter

Description

parse Getter

Demo Code


//package com.java2s;
import java.lang.reflect.Method;

public class Main {
    private static final String GET_PREFIX = "get";
    private static final String IS_PREFIX = "is";

    public static Method parseGetter(Object bean, String propertyName,
            Class<?> propertyClass) throws SecurityException,
            NoSuchMethodException {
        return bean.getClass().getMethod(
                getGetMethodName(propertyName, propertyClass));

    }/*from  w  w  w.j a v  a 2s .  c om*/

    public static String getGetMethodName(String propertyName,
            Class<?> propertyClass) {
        if (propertyClass == boolean.class) {
            return IS_PREFIX + capitalizeMethodName(propertyName);
        } else if (propertyClass == Boolean.class) {
            return IS_PREFIX + capitalizeMethodName(propertyName);
        } else {
            return GET_PREFIX + capitalizeMethodName(propertyName);
        }
    }

    private static String capitalizeMethodName(String propertyName) {
        StringBuilder sb = new StringBuilder();
        sb.append(Character.toUpperCase(propertyName.charAt(0)));
        sb.append(propertyName.substring(1));
        return sb.toString();
    }
}

Related Tutorials