JavaBean和Map间转换

JavaBean和Map间转换

Android小彩虹2021-08-23 3:28:08100A+A-

BeanUtils

public class BeanUtils {

    /** * 成员变量是否忽略判断 */
    public static boolean isIgnore(Field field) {
        if (field.isAnnotationPresent(Ignore.class)) {
            Ignore ignore = field.getAnnotation(Ignore.class);
            return ignore.ignore();
        }
        return false;
    }

    /** * 对象转集合 */
    public static Map<String, String> beanToMapString(Object object) {
        HashMap<String, String> map = new HashMap<>();
        Class<?> aClass = object.getClass();
        Field[] fields = aClass.getFields();
        for (Field field : fields) {
            field.setAccessible(true);
            try {
                String key = field.getName();
                Object value = field.get(object);
                if (value != null) {
                    map.put(key, value.toString());
                } else {
                    map.put(key, null);
                }
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }
        return map;
    }

    /** * 对象转集合 */
    public static Map<String, Object> beanToMap(Object object) {
        HashMap<String, Object> map = new HashMap<>();
        Class<?> aClass = object.getClass();
        Field[] fields = aClass.getFields();
        for (Field field : fields) {
            field.setAccessible(true);
            try {
                String key = field.getName();
                Object value = field.get(object);
                map.put(key, value);
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }
        return map;
    }

    /** * 对象转集合 */
    public static Object mapToBean(Map<String, Object> map, Class clazz) {
        try {
            Object object = clazz.newInstance();
            for (Map.Entry<String, Object> entry : map.entrySet()) {
                String key = entry.getKey();
                Object value = entry.getKey();
                for (Field field : clazz.getFields()) {
                    field.setAccessible(true);
                    if (field.getName().equals(key)) {
                        field.set(object, value);
                    }
                }
            }
            return object;
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return null;
    }

    /** * 集合的深度克隆,集合中对象必须实现序列化接口 * * @param src 源数据 * @return 克隆对象 */
    public static Object clone(Object src) {
        Object dest = null;
        try {
            ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
            ObjectOutputStream out = new ObjectOutputStream(byteOut);
            out.writeObject(src);
            ByteArrayInputStream byteIn = new ByteArrayInputStream(byteOut.toByteArray());
            ObjectInputStream in = new ObjectInputStream(byteIn);
            //noinspection unchecked
            dest = in.readObject();
        } catch (Exception e) {
            Logger.e("BeanUtils", e.getMessage());
        }
        return dest;
    }

    public static void formatValue(Object object) {
        formatNumber(object);
        formatString(object);
    }

    /** * 对象中类型为Number值为NULL的格式化为0 */
    public static void formatNumber(Object object) {
        if (object != null) {
            Class<?> aClass = object.getClass();
            Field[] fields = aClass.getFields();
            for (Field field : fields) {
                field.setAccessible(true);
                if (isIgnore(field)) continue;
                Class<?> type = field.getType();
                try {
                    Object value = field.get(object);
                    if (value == null) {
                        if (type == Byte.class) {
                            field.set(object, (byte) 0);
                        }
                        if (type == Short.class) {
                            field.set(object, (short) 0);
                        }
                        if (type == Integer.class) {
                            field.set(object, (int) 0);
                        }
                        if (type == Long.class) {
                            field.set(object, (long) 0);
                        }
                        if (type == Float.class) {
                            field.set(object, (float) 0);
                        }
                        if (type == Double.class) {
                            field.set(object, (double) 0);
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }

    /** * 对象中类型为String值为NULL的格式化为"" */
    public static void formatString(Object object) {
        if (object != null) {
            Class<?> aClass = object.getClass();
            Field[] fields = aClass.getFields();
            for (Field field : fields) {
                field.setAccessible(true);
                if (isIgnore(field)) continue;
                Class<?> type = field.getType();
                if (type == String.class) {
                    try {
                        Object value = field.get(object);
                        if (value == null) {
                            field.set(object, "");
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }
}

点击这里复制本文地址 以上内容由权冠洲的博客整理呈现,请务必在转载分享时注明本文地址!如对内容有疑问,请联系我们,谢谢!

支持Ctrl+Enter提交

联系我们| 本站介绍| 留言建议 | 交换友链 | 域名展示
本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除

权冠洲的博客 © All Rights Reserved.  Copyright quanguanzhou.top All Rights Reserved
苏公网安备 32030302000848号   苏ICP备20033101号-1
本网站由 提供CDN/云存储服务

联系我们