ReflectUtil反射工具类2

public class ReflectUtil {

    /*
        *获取字段的值
        *o 类对象
        *fieldName 字段名称
        */
    public static Object getValueByFieldName(Object o,String fieldName) throws Exception {
        return getValueByFieldName(o.getClass(),o,filedName);
    }

    /*
        *通过字段名称获取字段值(反射)
        *classType 类名称
        *o 类对象
        *fieldName 字段名称
        */
    public static Object getValueByFieldName(Class classType,Object o,String fieldName) throws Exception {
        if (fieldName.startsWith("#")) {
            //格式为#empList.id
            String fieldList = fieldName.substring(1,fieldName.indexOf("."));
            String subField = fieldName.substring(fieldName.indexOf(".") + 1);
            Object l = getValueByGetter(classType,o,"get" + upperFirstLetter(fieldList));
            List list = new ArrayList();
            if (l instanceof List) {
                for (Object object : (List)l) {
                    list.add(getValueByFieldName(object.getClass(),object,subField));
                }
            } else {
                throw new RuntimeException("[" + fieldName + "]参数解析失败,非集合类型数据");
            }
            return list;
        } else if(fieldName.contains(".")) {
            String[] fieldNames = fieldName.split("\\.");
            String getName = "get" + upperFirstLetter(fieldNames[fieldNames.length - 1]);
            for (int i = 1;i < fieldNames.length;i++) {
                String getSubNamePre = "get" + upperFirstLetter(fieldNames[i-1]);
                String getSubName = "get" + upperFirstLetter(fieldNames[i]);
                o = getValueByGetter(classType,o,getSubNamePre,getSubName);
            }
            return getValueByGetter(classType,o,getName);
        } else {
            String getName = "get" + upperFirstLetter(fieldName);
            return getValueByGetter(classType,o,getName);
        }
    }

    /*
        *通过getter方法反射获取字段值
        *classType 类名称
        *o 类对象
        *String 类中属性的get方法名称
        */
    public static Object getValueByGetter(Class classType,Object o,String getMethodName) throws NoSuchMethodException,IllegalAccessException,InvocationTargetException {
        //获取相应的方法
        Method getMethod = classType.getMethod(getMethodName,new Class[]{});
        //调用源对象的get方法
        Object value = getMethod.invoke(o);
        return value;
    }

    /*
        *通过getter方法反射获取对象对应的字段值
        *classType 类类别
        */
    public static Object getValueByGetter(Class classType,Object o,String getMethodName,String getSubMethodName) throws NoSuchMethodException,IllegalAccessException,InvocationTargetException {
        //获取相应的方法
        Method getMethod = classType.getMethod(getMethodName,new Class[]{});
        //调用方法
        Object value = getMethod.invoke(o);
        return value;
    }

    /*
        *将首字母转大写
        */
    public static String upperFirstLetter(String str) {
        return str.substring(0,1).toUpperCase() + str.substring(1);
    }
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值