Java对象转换工具类

Java 对象转换工具类

package com.xxx.xxxxxx.xxxx;
/**
 * @ClassName BeanUtil
 * @Description: bean util 处理工具类
 * @Author sxs
 * @Date 2020/7/15 18:19
 */

public class BeanUtil<T> {

    private static Logger logger = LoggerFactory.getLogger(BeanUtil.class);


    /**
     * Map -> Object
     *
     * @param des
     * @param params
     * @param 
     * @return
     */
    public static <T> T convertMapToObj(T des, Map<String, Object> params) {

        if (null == des || MapUtils.isEmpty(params)) {
            throw new BusinessException(ErrorCode.SERVER_ERROR, "param is null");
        }

        try {
            Optional.ofNullable(params).ifPresent(ps -> {

                ps.forEach((k, v) -> {

                    if (exist(k, des)) {

                        try {
                            Field field = des.getClass().getDeclaredField(k);
                            field.setAccessible(true);
                            field.set(des, v);
                        } catch (Exception e) {
                            logger.error("field set fail :{}", e);
                            throw new BusinessException(ErrorCode.SERVER_ERROR, ErrorCode.SERVER_ERROR.getMessage());
                        }
                    }
                });
            });
        } catch (Exception e) {
            logger.error("convertMapToObj fail :{}", e);
            throw new BusinessException(ErrorCode.SERVER_ERROR, "convertMapToObj fail");
        }

        return des;
    }


    /**
     * Object -> Object
     *
     * @param des
     * @param ori
     * @param 
     * @return
     */
    public static <T> T convertObjToObj(T des, T ori) {

        if (null == des || null == ori) {
            throw new BusinessException(ErrorCode.SERVER_ERROR, "param is null");
        }

        Optional.ofNullable(ori.getClass().getDeclaredFields()).ifPresent(fields -> {
            try {

                for (Field f : fields) {
                    f.setAccessible(true);

                    String fieldName = f.getName();
                    if (!fieldName.equals("serialVersionUID")) {
                        Optional<Object> value = Optional.ofNullable(f.get(ori));
                        if (value.isPresent()) {
                            if (exist(fieldName, des)){
                                Field field = des.getClass().getDeclaredField(fieldName);
                                field.setAccessible(true);
                                field.set(des, value.get());
                            }

                        }
                    }
                }
            } catch (Exception e) {
                logger.error("convertObjToObj fail :{}", e);
                throw new BusinessException(ErrorCode.SERVER_ERROR, "convertObjToObj fail");
            }

        });

        return des;
    }


    /**
     * Object -> Map
     *
     * @param des
     * @param ori
     * @param 
     * @return
     */
    public static <T> Map<String, Object> convertObjToMap(Map<String, Object> des, T ori) {

        if (MapUtils.isEmpty(des) || null == ori) {
            throw new BusinessException(ErrorCode.SERVER_ERROR, "param is null");
        }

        Optional.ofNullable(ori.getClass().getDeclaredFields()).ifPresent(fields -> {
            try {

                for (Field f : fields) {
                    f.setAccessible(true);
                    String fieldName = f.getName();
                    if (!fieldName.equals("serialVersionUID")) {
                        Optional<Object> value = Optional.ofNullable(f.get(ori));
                        if (value.isPresent()) {
                            des.put(fieldName, value.get());
                        }
                    }
                }
            } catch (Exception e) {
                logger.error("convertObjToMap fail :{}", e);
                throw new BusinessException(ErrorCode.SERVER_ERROR, "convertObjToMap fail");
            }

        });

        return des;
    }


    /**
     * R ori -> T des
     *
     * @param des
     * @param ori
     * @param 
     * @param 
     * @return
     */
    public static <T, R> T convertObjectToObject(T des, R ori) {

        if (null == des || null == ori) {
            throw new BusinessException(ErrorCode.SERVER_ERROR, "param is null");
        }

        Optional.ofNullable(ori.getClass().getDeclaredFields()).ifPresent(fields -> {
            try {

                for (Field f : fields) {
                    f.setAccessible(true);
                    String fieldName = f.getName();
                    if (!fieldName.equals("serialVersionUID")) {
                        Optional<Object> value = Optional.ofNullable(f.get(ori));
                        if (value.isPresent()) {
                            if (exist(fieldName, des)) {
                                Field field = des.getClass().getDeclaredField(fieldName);
                                field.setAccessible(true);
                                field.set(des, value.get());
                            }
                        }
                    }
                }
            } catch (Exception e) {
                logger.error("convertParamToObject fail :{}", e);
                throw new BusinessException(ErrorCode.SERVER_ERROR, "convertParamToObject fail");
            }

        });

        return des;
    }


    /**
     * check fieldName exist
     *
     * @param fieldName
     * @param obj
     * @return
     * @throws NoSuchFieldException
     */
    public static Boolean exist(String fieldName, Object obj) {

        if (obj == null || StringUtils.isEmpty(fieldName)) {
            return null;
        }

        AtomicBoolean flag = new AtomicBoolean(false);
        Optional.ofNullable(obj.getClass().getDeclaredFields()).ifPresent(
                fields -> {
                    for (Field f : fields) {
                        f.setAccessible(true);
                        if (f.getName().equals(fieldName)) {
                            flag.set(true);
                            break;
                        }
                    }
                }
        );

        return flag.get();
    }

}

你可能感兴趣的:(反射,BeanUtils)