CGLib DynamicBeanUtil动态类工具类

CGLib DynamicBeanUtil动态类工具类

package cn.com.weijuhe;



import org.springframework.cglib.beans.BeanGenerator;
import org.springframework.cglib.beans.BeanMap;

import java.lang.reflect.Field;
import java.util.*;

public class DynamicBeanUtil {
    private Object object = null;   //动态生成的类
    private BeanMap beanMap = null; //存放属性名称以及属性的类型
    private Map propertyMap = null;  //属性名,属性类型

    public DynamicBeanUtil() {
        super();
    }

    public DynamicBeanUtil(Map propertyMap) {
        this.propertyMap = propertyMap;
        this.object = generateBean(propertyMap);
        this.beanMap = BeanMap.create(this.object);
    }

    /**
     * @param propertyMap
     * @return
     */
    private Object generateBean(Map propertyMap) {
        BeanGenerator generator = new BeanGenerator();
        Set keySet = propertyMap.keySet();
        for (Iterator i = keySet.iterator(); i.hasNext(); ) {
            String key = (String) i.next();
            generator.addProperty(key, (Class) propertyMap.get(key));
        }
        return generator.create();
    }

    /**
     * 给bean属性赋值
     *
     * @param property 属性名
     * @param value    值
     */
    public void setValue(Object property, Object value) {
        beanMap.put(property, value);
    }

    /**
     * 通过属性名得到属性值
     *
     * @param property 属性名
     * @return 值
     */
    public Object getValue(String property) {
        return beanMap.get(property);
    }

    /**
     * 得到该实体bean对象
     *
     * @return
     */
    public Object getObject() {
        return this.object;
    }

   @Override
    public String toString() {
        return "DynamicBean{" +
                "\n  propertyMap=" + propertyMap +
                "\n  beanMap=" + beanMap +
                "\n" + object +
                "\n}";
    }

    /**
     * @author liu xian song
     * @desc 引用对象,获取需要属性
     * @param data
     * @param refer
     * @return
     */
    public static Map<String, Object> referObjectType(Object data, String[] refer) {
        Map<String, Object> typeMap = new HashMap<>();
        Field[] fieldArray = data.getClass().getDeclaredFields();
        if (refer.length > 0) {
            for (int i = 0; i < fieldArray.length; i++) {
                boolean contains = Arrays.asList(refer).contains(fieldArray[i].getName());
                if (contains == true) {
                    fieldArray[i].setAccessible(true);
                    typeMap.put(fieldArray[i].getName(), fieldArray[i].getGenericType());
                }
            }
        } else {
            for (int i = 0; i < fieldArray.length; i++) {
                fieldArray[i].setAccessible(true);
                typeMap.put(fieldArray[i].getName(), fieldArray[i].getGenericType());
            }
        }
        return typeMap;
    }

} 

使用例子:

Map<String,Object> map = new HashMap<String,Object>(){{
    put("name","测试");
    put("age",27);
}};
DynamicBeanUtil bean = new DynamicBeanUtil(map);
System.out.println("name=="+bean.getValue("name"));
System.out.println("age =="+bean.getValue("age"));
评论将由博主筛选后显示,对所有人可见 | 还能输入1000个字符
©️2020 CSDN 皮肤主题: 深蓝海洋 设计师: CSDN官方博客 返回首页
实付0元
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值