码迷,mamicode.com
首页 > 编程语言 > 详细

java一个对象赋值给另一个对象,支持平铺类和层级类间的互转

时间:2018-05-29 12:33:03      阅读:191      评论:0      收藏:0      [点我收藏+]

标签:http   ESS   领域   场景   思路   otc   getname   图片   except   

场景:将一个层级类对象(领域驱动model对象)转换为平铺类对象(view)

src对象,(红框为子对象)

技术分享图片

target对象(平铺对象)

技术分享图片

代码思路,先递归反射遍历出所有字段,存到一个map里,再递归赋值给target对象

缺陷:不同子对象间的同名字段会被覆盖成一个值

代码

private static void getSrcALLFieldMap(Object obj,Map<String,Object> collectMap){
        Class srcClazz = obj.getClass();
        Field[] srcFields = srcClazz.getDeclaredFields();
        Stream.of(srcFields).forEach(field -> {
            field.setAccessible(true);
            try {
                if(field.getType().toString().contains("aacoin.account")  && !field.getType().isEnum()){
                    Object subObj = field.get(obj);
                    if(subObj!=null)
                        getSrcALLFieldMap(subObj,collectMap);
                }else{
                    collectMap.put(field.getName(),field.get(obj));
                }
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        });
    }
    private static void setTargetAllField(Object obj, Map<String,Object> srcMap){
        Class srcClazz = obj.getClass();
        Field[] srcFields = srcClazz.getDeclaredFields();
        Stream.of(srcFields).forEach(field -> {
            field.setAccessible(true);
            try {
                if(field.getType().toString().contains("aacoin.trade.otc") && !field.getType().isEnum()){
                    Object subObj = field.get(obj);
                    if(subObj == null) {
                        Constructor cons = field.getType().getDeclaredConstructor(null);
                        cons.setAccessible(true);
                        subObj = cons.newInstance(null);
                        field.set(obj, subObj);
                    }
                    setTargetAllField(subObj, srcMap);
                }else{
                    //collectMap.put(field,obj);
                    Object currentField = srcMap.get(field.getName());
                    if( currentField !=null && field.getType() == currentField.getClass()){
                        field.set(obj,currentField);
                    }
                }
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InstantiationException e) {
                e.printStackTrace();
            } catch (NoSuchMethodException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }
        });
    }

    public static <T> T convert(Object src,Class<T> tClass){

        Constructor<T> cons = null;
        try {
            cons = tClass.getDeclaredConstructor(null);
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        }
        cons.setAccessible(true);
        T result = null;
        try {
            result = cons.newInstance(null);
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        convert(src,result);
        return result;
    }

    public static <T> T convert(Object src,T result) {

        Map<String,Object> srcMap = new HashMap();
        getSrcALLFieldMap(src,srcMap);

        setTargetAllField(result,srcMap);
        return result;
    }

调用:

adverVO =convert(advert,AdvertVO.class);

写的比较随意,直接吞掉了异常,需自行处理,将异常统一规范抛出

java一个对象赋值给另一个对象,支持平铺类和层级类间的互转

标签:http   ESS   领域   场景   思路   otc   getname   图片   except   

原文地址:https://www.cnblogs.com/cmos/p/9104384.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!