本篇文章主要介绍Java如何给已有实体类动态的添加字段并返回新的实体对象且不影响原来的实体对象结构。
参考代码如下:
引入依赖包
<dependency><groupId>cglib</groupId><artifactId>cglib</artifactId><version>2.2.2</version></dependency><dependency><groupId>commons-beanutils</groupId><artifactId>commons-beanutils</artifactId><version>1.9.4</version></dependency>
代码实现
DynamicBean.java
import net.sf.cglib.beans.BeanGenerator;
import net.sf.cglib.beans.BeanMap;import java.util.Map;public class DynamicBean {private Object target;private BeanMap beanMap;public DynamicBean(Class superclass, Map<String, Class> propertyMap) {this.target = generateBean(superclass, propertyMap);this.beanMap = BeanMap.create(this.target);}public void setValue(String property, Object value) {beanMap.put(property, value);}public Object getValue(String property) {return beanMap.get(property);}public Object getTarget() {return this.target;}/*** 根据属性生成对象*/private Object generateBean(Class superclass, Map<String, Class> propertyMap) {BeanGenerator generator = new BeanGenerator();if (null != superclass) {generator.setSuperclass(superclass);}BeanGenerator.addProperties(generator, propertyMap);return generator.create();}
}
ReflectUtil.java
import com.google.common.collect.Maps;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.beanutils.PropertyUtilsBean;import java.beans.PropertyDescriptor;
import java.util.Date;
import java.util.Map;@Slf4j
public class ReflectUtil {public static Object getTarget(Object dest, Map<String, Object> addProperties) {PropertyUtilsBean propertyUtilsBean = new PropertyUtilsBean();PropertyDescriptor[] descriptors = propertyUtilsBean.getPropertyDescriptors(dest);Map<String, Class> propertyMap = Maps.newHashMap();for (PropertyDescriptor d : descriptors) {if (!"class".equalsIgnoreCase(d.getName())) {propertyMap.put(d.getName(), d.getPropertyType());}}addProperties.forEach((k, v) -> {String sclass = v.getClass().toString();if (sclass.equals("class java.util.Date")) {//对日期进行处理propertyMap.put(k, Long.class);} else {propertyMap.put(k, v.getClass());}});DynamicBean dynamicBean = new DynamicBean(dest.getClass(), propertyMap);propertyMap.forEach((k, v) -> {try {if (!addProperties.containsKey(k)) {dynamicBean.setValue(k, propertyUtilsBean.getNestedProperty(dest, k));}} catch (Exception e) {log.error("动态添加字段出错", e);}});addProperties.forEach((k, v) -> {try {String sclass = v.getClass().toString();if (sclass.equals("class java.util.Date")) {//动态添加的字段为date类型需要进行处理Date date = (Date) v;dynamicBean.setValue(k, date.getTime());} else {dynamicBean.setValue(k, v);}} catch (Exception e) {log.error("动态添加字段值出错", e);}});Object obj = dynamicBean.getTarget();return obj;}
}
使用示例
创建Student
实体类:
@Setter
@Getter
@ToString
@NoArgsConstructor
@AllArgsConstructor
public class Student implements Serializable {private String name;
}
测试
@Slf4j
public class MyTest {public static void main(String[] args) {Student student = new Student();student.setName("公众号:小小开发者");log.info("student = {}",JSON.toJSONString(student));Map<String, Object> propertiesMap = new HashMap<>();propertiesMap.put("age", 25);Object obj = ReflectUtil.getTarget(student, propertiesMap);log.info("动态添加属性: = {}",JSON.toJSONString(obj));}
}
测试结果
student = {“name”:“公众号:小小开发者”}
动态添加属性: = {“age”:25,“name”:“公众号:小小开发者”}
可以看到,我们动态为对象添加的属性和属性值已经成功实现!