反射克隆对象,将Map转化成JavaBean

反射初应用

利用反射克隆一个对象

 public static <T> T clone(Object obj,Class<T> t){
        T newObj = null;
        try {
            //基于传递的Class对象实例化一个空对象(数据为空)
            newObj = t.newInstance();
            //获取Class中的所有属性
            Field[] fields = t.getDeclaredFields();
            for (Field f : fields) {
                //获取属性名
                String fname = f.getName();
                //获取属性类型
                Class<?> ftype = f.getType();

                //获取属性对应的setter/getter方法名称
                String setMethodName = "set"+fname.substring(0,1).toUpperCase()+fname.substring(1);
                String getMethodName = "get"+fname.substring(0,1).toUpperCase()+fname.substring(1);
                //对于布尔类型的get方法,前缀以is开头
                if(Objects.equals("boolean",ftype.getName())){
                    getMethodName = "is"+fname.substring(0,1).toUpperCase()+fname.substring(1);
                }

                //根据提供的方法名称和类型获取方法对象
                Method methodSet = t.getMethod(setMethodName,ftype);
                Method methodGet = t.getMethod(getMethodName);

                //执行参数对象的get方法取出属性值,作为目标对象的set方法参数值
                Object returnVal = methodGet.invoke(obj);
                methodSet.invoke(newObj,returnVal);
            }
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        return newObj;
    }
 public static <T> T clone1(T obj) throws Exception {
        Class<T> clazz = (Class<T>) obj.getClass();

        T o = null;
        o = clazz.newInstance();

        Field[] fields = clazz.getDeclaredFields();
        for (Field field : fields) {
            String name = field.getName();
            Class<?> type = field.getType();

            String suffix = name.substring(0,1).toUpperCase()+name.substring(1);
            String getName = "get"+suffix;
            String setName = "set"+suffix;

            Method methodGet = clazz.getMethod(getName);
            Method methodSet = clazz.getMethod(setName, type);

            methodSet.invoke(o,methodGet.invoke(obj));
        }

        return o;
    }

利用反射将map转化成javabean

 public static <T> T mapToBean1(Map<String,? super Object> map,Class<T> t)throws Exception{
        T instance = t.newInstance();

        for (Map.Entry<String, ? super Object> entry : map.entrySet()) {
            String key = entry.getKey();
            Object value = entry.getValue();
            Field field = t.getDeclaredField(key);

            field.setAccessible(true);

            field.set(instance,value);

        }
        return instance;
    }
public static <T> T mapToBean(Map<String,Object> map, Class<T> t){
        T target = null;
        try {
            //1. 实例化一个空对象
            target = t.newInstance();
            //2.获取Class中的所有属性名称
            Field[] fields = t.getDeclaredFields();
            for (Field f : fields) {
                //获取属性名称(map集合的键)
                String fieldName = f.getName();
                //设置属性的可访问性为true(暴力反射)
                f.setAccessible(true);
                //设置属性的值  sname=XXX
                f.set(target,map.get(fieldName));
            }
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        //返回目标对象
        return target;
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

西伯利亚大熊猫

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值