find Getter - Java Reflection

Java examples for Reflection:Getter

Description

find Getter

Demo Code


import java.lang.reflect.Method;
import org.apache.log4j.Logger;

public class Main{
    public static Method findGetter(Class<?> entityClass, String field) {
        String methodName = "get" + field.substring(0, 1).toUpperCase()
                + field.substring(1);//from  w w w  . ja v  a  2s .  co  m
        Method[] methods = entityClass.getMethods();
        for (int i = 0; i < methods.length; i++) {
            Method method = methods[i];
            if (method.getName().equals(methodName))
                return method;
        }
        methodName = "is" + field.substring(0, 1).toUpperCase()
                + field.substring(1);
        for (int i = 0; i < methods.length; i++) {
            Method method = methods[i];
            if (method.getName().equals(methodName))
                return method;
        }
        methodName = field;
        for (int i = 0; i < methods.length; i++) {
            Method method = methods[i];
            if (method.getName().equals(methodName))
                return method;
        }
        return null;
    }
}

Related Tutorials