package org.springblade.common.utils;

import org.springframework.cglib.beans.BeanMap;

import java.util.HashMap;
import java.util.Map;

/**
 * Created By lidexi in 2021/3/24
 **/
public class BeanMapUtil {

	/**
	 * 将对象属性转化为map结合
	 */
	public static <T> Map<String, Object> beanToMap(T bean) {
		Map<String, Object> map = new HashMap<>();
		if (bean != null) {
			BeanMap beanMap = BeanMap.create(bean);
			for (Object key : beanMap.keySet()) {
				map.put(key+"", beanMap.get(key));
			}
		}
		return map;
	}

	/**
	 * 将map集合中的数据转化为指定对象的同名属性中
	 */
	public static <T> T mapToBean(Map<String, Object> map,Class<T> clazz) throws Exception {
		T bean = clazz.newInstance();
		BeanMap beanMap = BeanMap.create(bean);
		beanMap.putAll(map);
		return bean;
	}
}
