XMANcharles
12/1/2017 - 2:14 AM

Map2J

Map转换JavaObject

/*
 * Copyright (C), 1995-2017, 没钱有限公司
 * FileName: Map2JUtil
 * Author:   Neo Geng
 * Date:     2017/10/25 16:40
 * Description: Convert Map 2 Java Object
 * History:
 * <author>          <time>          <version>          <desc>
 * 作者姓名           修改时间           版本号              描述
 */
package com.cubwolf.serializable2j;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 〈Convert Map 2 Java Object〉
 *
 * @author Neo Geng
 *         Date 2017/10/25
 * @since 1.0.0
 */
public class Map2JUtil {

    private Map2JUtil() {

    }

    /**
     * 将Map转换为JavaObject
     * @param map 要转换的集合
     * @param clazz 类对象
     * @param <T> 泛型约束
     * @return
     */
    public static <T> T convertToObj(Map<String, Object> map, Class<T> clazz) {
        //map为null
        if (map == null || map.size() == 0) {
            return null;
        }
        try {
            T t = clazz.newInstance();
            //class集合
            List<Class> classes = new ArrayList<>();
            //clazz
            classes.add(clazz);
            //superClass
            Class superClass;
            //遍历父类,如果不为Objecrt(object没有继承字段)
            while ((superClass = clazz.getSuperclass()) != Object.class) {
                //add
                classes.add(superClass);
            }
            //遍历得到的Class并依次对字段赋值
            for (Class superClazz : classes) {
                //获取定义的字段
                Field[] declaredFields = superClazz.getDeclaredFields();
                //便利字段
                for (Field field : declaredFields) {
                    //is not static||final
                    int modifiers = field.getModifiers();
                    if (Modifier.isStatic(modifiers) || Modifier.isFinal(modifiers)) {
                        continue;
                    }
                    field.setAccessible(true);
                    field.set(t, map.get(field.getName()));
                }
            }
            return t;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 将JavaObject转换为Map
     * @param t 要转换的对象
     * @param <T> 泛型约束
     * @return
     */
    public static <T> Map<String, Object> convertToMap(T t) {
        if (t == null) {
            return null;
        }
        Map<String, Object> map = new HashMap<>(16);
        //得到关联的类(本类、父类)
        List<Class> classes = new ArrayList<>();
        classes.add(t.getClass());
        Class superClass;
        while ((superClass = t.getClass().getSuperclass()) != Object.class) {
            classes.add(superClass);
        }
        try {
            for (Class clazz : classes) {
                Field[] declaredFields = clazz.getDeclaredFields();
                for (Field field : declaredFields) {
                    int modifiers = field.getModifiers();
                    if (Modifier.isStatic(modifiers) || Modifier.isFinal(modifiers)) {
                        continue;
                    }
                    map.put(field.getName(), field.get(t));
                }
            }
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return map;
    }
}