All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.efeichong.proxy.DynamicBean Maven / Gradle / Ivy

package com.efeichong.proxy;

import com.efeichong.cache.EntityCache;
import lombok.NonNull;
import org.springframework.cglib.beans.BeanGenerator;
import org.springframework.cglib.beans.BeanMap;

import java.beans.PropertyDescriptor;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author lxk
 * @date 2021/1/18
 * @description 生成一个动态的类
 */
public class DynamicBean {

    /**
     * 动态生成的类
     */
    private Object object;
    /**
     * 存放属性名称以及属性的类型
     */
    private BeanMap beanMap;
    /**
     * K:属性名 V:属性值
     */
    private Map propMap = new HashMap();


    public DynamicBean(@NonNull List propDescList, @NonNull Object obj) {
        this.object = generateBean(propDescList, obj);
        this.beanMap = BeanMap.create(this.object);
        this.beanMap.putAll(propMap);
    }

    /**
     * 生成代理类
     *
     * @param propDescList
     * @param proxied
     * @return
     */
    private Object generateBean(List propDescList, Object proxied) {
        BeanGenerator generator = new BeanGenerator();
        EntityCache entityCache = EntityCache.forClass(proxied.getClass());
        for (PropDesc propDesc : propDescList) {
            generator.addProperty(propDesc.getFieldName(), propDesc.getClazz());
            propMap.put(propDesc.getFieldName(), propDesc.getValue());
        }

        PropertyDescriptor[] propDescriptors = entityCache.getPropDescriptors();
        for (PropertyDescriptor propertyDescriptor : propDescriptors) {
            String descriptorName = propertyDescriptor.getName();
            Object value = entityCache.getValue(proxied, descriptorName);
            if (!"class".equals(descriptorName)) {
                generator.addProperty(descriptorName, propertyDescriptor.getPropertyType());
                propMap.put(descriptorName, value);
            }
        }
        generator.setSuperclass(proxied.getClass());
        return generator.create();
    }

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




© 2015 - 2025 Weber Informatics LLC | Privacy Policy