对象数据自动替换方法

/**
     * 迭代处理对象属性值替换
     * @param vo
     * @param attriMap
     * @param unchangeNameSet
     */
    private void changeBaseInfo(Object vo,Map attriMap,Set<String> unchangeNameSet) {
        try {
            if(vo == null || attriMap == null || attriMap.size() == 0) return;
            //map 数据中内容替换
            if (vo instanceof Map) {
                Map map = (Map) vo;
                if (map != null && map.size() > 0) {
                    Set set = map.keySet();
                    Iterator iterator = set.iterator();
                    //获取map中所有value值
                    while (iterator.hasNext()) {
                        Object key = iterator.next();
                        if(key == null) continue;
                        Object value = map.get(key);
                        //map中String类型内容可直接替换
                        if (value != null && value instanceof String) {
                            ZhuanquDataMultilingualDTO dtoObjB = (ZhuanquDataMultilingualDTO) attriMap.get(value.toString().toLowerCase());
                            if (dtoObjB != null) {
                                String otherB = dtoObjB.getTextValue();
                                if (StringUtils.isNotBlank(otherB)) {
                                    //不需要替换内容校验
                                    if(unchangeNameSet.contains(key.toString())) continue;
                                    map.put(key, otherB);
                                }
                            }
                        }
                        //其他类型需要迭代类型处理
                        else {
                            //迭代分析
                            changeBaseInfo(value, attriMap,unchangeNameSet);
                        }
                    }
                }
            }
            //列表中数据替换
            else if (vo instanceof List) {
                List list = (List) vo;
                if (!CollectionUtils.isEmpty(list)) {
                    for (Object value : list) {
                        //迭代分析(字符类型暂不做处理,只处理对象类型)
                        changeBaseInfo(value, attriMap,unchangeNameSet);
                    }
                }
            }
            //对象数据替换
            else {
                Field[] declaredFields = vo.getClass().getDeclaredFields();
                //只处理有属性值得对象数据
                if (declaredFields != null && declaredFields.length > 0) {
                    for (Field field : declaredFields) {
                        //过滤掉配置中不需要修改的值
                        if(field == null && unchangeNameSet.contains(field.getName())){
                            continue;
                        }
                        //对象中字符类型数据使用get\set方法替换
                        if (field.getType() == String.class) {
                            String methodName = com.dhgate.mobile.util.StringUtils.toUpperCaseFirstOne(field.getName());
                            Method getMethod = vo.getClass().getMethod("get" + methodName);
                            if (getMethod != null) {
                                Object value = getMethod.invoke(vo);
                                if (value != null && attriMap != null) {
                                    ZhuanquDataMultilingualDTO dtoObjB = (ZhuanquDataMultilingualDTO) attriMap.get(value.toString().toLowerCase());
                                    if (dtoObjB != null) {
                                        String otherB = dtoObjB.getTextValue();
                                        if (StringUtils.isNotBlank(otherB)) {
                                            Method setMethod = vo.getClass().getMethod("set" + methodName, String.class);
                                            setMethod.invoke(vo, otherB);
                                        }
                                    }
                                }
                            }
                        }
                        //对象中map和list对象继续迭代处理
                        else if(field.getType() == Map.class || field.getType() == List.class){
                            String methodName = com.dhgate.mobile.util.StringUtils.toUpperCaseFirstOne(field.getName());
                            Method getMethod = vo.getClass().getMethod("get" + methodName);
                            Object value = getMethod.invoke(vo);
                            if (value != null) {
                                //迭代分析
                                changeBaseInfo(value, attriMap,unchangeNameSet);
                            }
                        }

                    }
                }
            }
        } catch (Exception e) {
            logger.error("changeBaseInfo error ",e);
        }
    }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值