根据字段配置 动态获取对象属性


        /**3.根据资金方字段配置展示对应的字段*/
        /**3.1获取资金方字段配置*/
        ClearProofFieldConfig clearProofFieldConfig = clearProofFieldConfigManager.selectByLoanChannelId(clearProofBatch.getLoanChannelId());
        if(null == clearProofFieldConfig){
            result.setCode(RespEnum.FAIL.code);
            result.setMsg("获取资金方字段配置失败");
            return result;
        }

        /**结清证明类型处理*/
        result.setProofType(Integer.valueOf(clearProofFieldConfig.getProofType()));
        /**字段配置值*/
        result.setFieldConfig(clearProofFieldConfig.getFieldConfig());

        /**3.2字段配置值动态处理*/
        String fieldConfig = clearProofFieldConfig.getFieldConfig();

        /**返回数据list*/
        List> dataList = Lists.newArrayList();

        if (StringUtils.isNotBlank(fieldConfig)) {
            Map reslutMap = null;
            /**获取对象属性值*/
            for(PortalClearProofDetailVo clearProofDetailVo :list){
                /**初始化Map*/
                reslutMap = Maps.newHashMap();
                /**必须有的值的特殊处理*/
                reslutMap.put("id",String.valueOf(clearProofDetailVo.getId()));
                reslutMap.put("logisticsNo",String.valueOf(clearProofDetailVo.getLogisticsNo()));
                reslutMap.put("proofStatus",String.valueOf(clearProofDetailVo.getProofStatus()));
                reslutMap.put("proofMemo",String.valueOf(clearProofDetailVo.getProofMemo()));
                /**动态展示值的处理*/
                /**得到class*/
                Class cls =  clearProofDetailVo.getClass();
                /**得到所有属性*/
                Field[] fields = cls.getDeclaredFields();
                for(Field field : fields){
                    /**设置对象的访问权限,保证对private的属性的访问*/
                    field.setAccessible(true);
                    if(fieldConfig.contains(field.getName())){
                        try {
                            reslutMap.put(field.getName(), String.valueOf(field.get(clearProofDetailVo)));
                        } catch (IllegalAccessException e) {
                            e.printStackTrace();
                            log.info("对象属性转换异常{}",e);
                            result.setCode(RespEnum.FAIL.code);
                            result.setMsg(e.getMessage());
                            return result;
                        }
                    }
                }
                dataList.add(reslutMap);
            }
        }

 

你可能感兴趣的:(java)