whyclxw
2024-07-30 ed04733e15a5304c68ac16c121fd2989c851a57b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package com.whyc.util;
 
import java.beans.PropertyDescriptor;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;
 
import com.fasterxml.jackson.databind.ObjectMapper;
import com.whyc.dto.BattDataDTO;
import com.whyc.pojo.BattRealdata;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.commons.beanutils.PropertyUtilsBean;
 
/**
 * @className:ReflectUtil
 * @description:动态生成类的属性、并且赋值
 * @date:2018年4月3日 下午2:33:10
 */
public class ReflectUtil {
 
    static Logger logger = LogManager.getLogger(ReflectUtil.class);
 
    @SuppressWarnings("rawtypes")
    public static Object getTarget(Object dest, Map<String, Object> addProperties) {
        PropertyUtilsBean propertyUtilsBean = new PropertyUtilsBean();
        PropertyDescriptor[] descriptors = propertyUtilsBean.getPropertyDescriptors(dest);
        Map<String, Class> propertyMap = new HashMap<>();
        for (PropertyDescriptor d : descriptors) {
            if (!"class".equalsIgnoreCase(d.getName())) {
                propertyMap.put(d.getName(), d.getPropertyType());
            }
        }
        // add extra properties
        addProperties.forEach((k, v) -> propertyMap.put(k, v.getClass()));
        // new dynamic bean
        DynamicBean dynamicBean = new DynamicBean(dest.getClass(), propertyMap);
        // add old value
        propertyMap.forEach((k, v) -> {
            try {
                // filter extra properties
                if (!addProperties.containsKey(k)) {
                    dynamicBean.setValue(k, propertyUtilsBean.getNestedProperty(dest, k));
                }
            } catch (Exception e) {
                logger.error(e.getMessage(), e);
            }
        });
        // add extra value
        addProperties.forEach((k, v) -> {
            try {
                dynamicBean.setValue(k, v);
            } catch (Exception e) {
                logger.error(e.getMessage(), e);
            }
        });
        Object target = dynamicBean.getTarget();
        return target;
    }
 
    /*public static void setProperty(Object obj, String propertyName, Object value)
            throws NoSuchFieldException, SecurityException,
            IllegalArgumentException, IllegalAccessException {
        // 根绝对象获取字节码文件
        Class c = obj.getClass();
        // 获取该对象的propertyName成员变量
        Field field = c.getDeclaredField(propertyName);  //注意你是通过参数来获取,不许加双引号“”
        // 取消访问检查
        field.setAccessible(true);
        // 给对象的成员变量赋值为指定的值--->value
        field.set(obj, value);
    }
    public static void main(String[] args) throws NoSuchFieldException, IllegalAccessException {
        BattDataDTO entity = new BattDataDTO();
        ReflectUtil.setProperty(entity,"time","hehe");
        Map<String, Object> addProperties = new HashMap<>();
        addProperties.put("day31", "你好");
        Object obj = getTarget(entity, addProperties);
        String s = ActionUtil.getGson().toJson(obj);
        System.out.println(s);
    }*/
}