zoukankan      html  css  js  c++  java
  • java通过反射,泛型将定义的对象与对象之间互转

    在做项目的时候,有时需要将定义的对象转换成Map对象,将Map对象转换成定义的对象,这个赋值过程很烦琐,于是写的个工具类,记录一下。

    public class AssignReflect {
        private Logger log = Logger.getLogger(this.getClass());
        /**
         * 将map对象的值赋值给T对象
         * @param cz T 类型
         * @param map 健值对
         * @return T对象
         */
        public <T> T convertObj(Class<T> cz, Map<String, Object> map) {
            if (map == null || map.size() == 0) {
                log.warn("The incoming map object is empty or size is zero");
                return null;
            }
            T t = null;
            try {
                t = cz.newInstance();
                Method[] methods = cz.getDeclaredMethods();
                for (Method m : methods) {
                    if (m.getName().contains("set")
                            && !m.getName().equals("equals")
                            && !m.getName().equals("toString")
                            && !m.getName().equals("hashCode")) {
                        try {
                            m.invoke(t, map.get(getString(m.getName().substring(3))));
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            return t;
        }
    
        /**
         * 将R对象的值赋值给T对象
         * @param des T 类型
         * @param raw R 值
         * @return T对象
         */
        public <T, R> T convertObj(T des, R raw) {
            if (raw == null) {
                log.warn("The incoming raw is empty");
                return null;
            }
            try {
                Method[] desMs = des.getClass().getDeclaredMethods();
                Method[] rawMs = raw.getClass().getDeclaredMethods();
                for (Method m : rawMs) {
                    String mn = m.getName();
                    if (mn.contains("get")
                            && !mn.equals("equals")
                            && !mn.equals("toString")
                            && !mn.equals("hashCode")) {
                            for(Method d : desMs){
                                String dn = d.getName();
                                if (dn.contains("set")
                                        && !dn.equals("equals")
                                        && !dn.equals("toString")
                                        && !dn.equals("hashCode")) {
                                    if(mn.substring(3).equals(dn.substring(3))){
                                        d.invoke(des, m.invoke(raw));
                                    }
                                }
                            }
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            return des;
        }
        
        /**
         *  将T对象的值赋值给map对象
         * @param object T对象
         * @return map对象
         */
        public <T> Map<String, Object> convertMap(T object) {
            if (object == null) {
                log.warn("The incoming object is empty");
                return null;
            }
            Map<String, Object> map = null;
            try {
                map = new HashMap<String, Object>();
                Class<?> cz = object.getClass();
                Method[] methods = cz.getDeclaredMethods();
                for (Method m : methods) {
                    if (m.getName().contains("get")
                            && !m.getName().equals("equals")
                            && !m.getName().equals("toString")
                            && !m.getName().equals("hashCode")) {
                        map.put(getString(m.getName().substring(3)), m.invoke(object));
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            return map;
        }
        
        /**
         * 将传入的英文字母,首个字母小写
         * @param string 要处理的字符串
         * @return 首个字母小写的字符串
         */
        private String  getString(String string){
            if(string == "" || string == null){
                return null;
            }
            String s = string.substring(0, 1);
            string  = string.replace(s, s.toLowerCase());
            return string;
        }
    }
  • 相关阅读:
    Python
    Html body的滚动条禁止与启用
    wampserver 更改www目录
    Linux下的tar压缩解压缩命令详解
    Linux 重置root密码
    Ubuntu增加一个用户并给普通用户赋予root权限的方法
    redhat系统下三种主要的软件包安装方法
    Linux 添加yum命令
    如何退出 Vim
    Linux ssh开启服务
  • 原文地址:https://www.cnblogs.com/natgeo/p/3081711.html
Copyright © 2011-2022 走看看