get Class Accessor Method - Java Reflection

Java examples for Reflection:Method

Description

get Class Accessor Method

Demo Code


import java.lang.reflect.Method;

public class Main{
    public static void main(String[] argv) throws Exception{
        Class clazz = String.class;
        String propertyName = "java2s.com";
        System.out.println(getAccessor(clazz,propertyName));
    }/*www . j  a va 2s .c  o m*/
    public static final String[] ACCESSOR_PREFIXES = new String[] { "is",
            "get", "has" };
    public static Method getAccessor(Class clazz, String propertyName) {
        propertyName = StringHelper.capitalize(propertyName);
        for (String prefix : ACCESSOR_PREFIXES) {
            try {
                return clazz.getMethod(prefix + propertyName);
            } catch (NoSuchMethodException ignored) {
                // ignore
            }
        }
        return null;
    }
}

Related Tutorials