集册 Java实例教程 找到Getter

找到Getter

欢马劈雪     最近更新时间:2020-01-02 10:19:05

437
找到吸气剂


import java.lang.reflect.Method;

import org.apache.log4j.Logger;
/**
来 自 时   代     Java  公  众  号 - nowjava.com
**/


public class Main{

    public static Method findGetter(Class<?> entityClass, String field) {

        String methodName = "get" + field.substring(0, 1).toUpperCase()

                + field.substring(1);

        Method[] methods = entityClass.getMethods();

        for (int i = 0; i < methods.length; i++) {

            Method method = methods[i];

            if (method.getName().equals(methodName))

                return method;

        }/*nowjava.com 提供*/

        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.l
展开阅读全文