• 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;
        }
    }
  • 相关阅读:
    eaysui 引用 CSS和JS 文件
    在JSP 页面中 添加CSS 样式
    Spring Hello World
    jsp+jdbc
    eclipse插件开发:使用emf建模型时,需要注意模型的坐标和大小的保存
    eclipse插件开发:GEF 进阶: Viewer
    Asp.net之MsChart控件动态绑定温度曲线图
    根据图中的盲点坐标,弹出div层
    小结get和Post的区别
    使用JavaC编译一个Java文件
  • 原文地址:https://www.cnblogs.com/natgeo/p/3081711.html
Copyright © 2020-2023  润新知