yanghb
2024-12-24 fe6e43d5e1144156d0ca4e9d6080c9821c25d97c
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package com.zzg.common.utils;
 
 
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.BeanWrapper;
import org.springframework.beans.BeanWrapperImpl;
 
import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
 
/**
 * @author: xxxg
 * @date: 2022/2/7 17:18
 */
public class BeanUtil {
    /**
     * 使用org.apache.commons.beanutils进行转换
     * <p>
     * 注意:org.springframework.beans.BeanUtils与org.apache.commons.beanutils.BeanUtils都有copyProperties方法,但前者支持忽略个别属性的方法,即copyProperties(source, target, ignoreProperties)
     */
    public static Object mapToObject1(Map<String, Object> map, Class<?> beanClass) throws Exception {
        if (map == null) {
            return null;
        }
        Object obj = beanClass.newInstance();
        BeanUtils.populate(obj, map);
        return obj;
    }
 
    public static Map<?, ?> objectToMap1(Object obj) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
        if (obj == null) {
            return null;
        }
        return BeanUtils.describe(obj);
    }
 
 
    /**
     * 使用Introspector进行转换
     */
    public static Object mapToObject2(Map<String, Object> map, Class<?> beanClass) throws Exception {
        if (map == null) {
            return null;
        }
 
        Object obj = beanClass.newInstance();
 
        BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass());
        PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
        for (PropertyDescriptor property : propertyDescriptors) {
            Method setter = property.getWriteMethod();
            if (setter != null) {
                setter.invoke(obj, map.get(property.getName()));
            }
        }
 
        return obj;
    }
 
    public static Map<String, Object> objectToMap2(Object obj) throws Exception {
        if (obj == null) {
            return null;
        }
 
        Map<String, Object> map = new HashMap<String, Object>();
 
        BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass());
        PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
        for (PropertyDescriptor property : propertyDescriptors) {
            String key = property.getName();
            if (key.compareToIgnoreCase("class") == 0) {
                continue;
            }
            Method getter = property.getReadMethod();
            Object value = getter != null ? getter.invoke(obj) : null;
            map.put(key, value);
        }
 
        return map;
    }
 
    /**
     * 使用reflect进行转换
     */
    public static Object mapToObject3(Map<String, Object> map, Class<?> beanClass) throws Exception {
        if (map == null) {
            return null;
        }
 
        Object obj = beanClass.newInstance();
 
        Field[] fields = obj.getClass().getDeclaredFields();
        for (Field field : fields) {
            int mod = field.getModifiers();
            if (Modifier.isStatic(mod) || Modifier.isFinal(mod)) {
                continue;
            }
 
            field.setAccessible(true);
            field.set(obj, map.get(field.getName()));
        }
 
        return obj;
    }
 
    public static Map<String, Object> objectToMap3(Object obj) throws Exception {
        if (obj == null) {
            return null;
        }
 
        Map<String, Object> map = new HashMap<String, Object>();
 
        Field[] declaredFields = obj.getClass().getDeclaredFields();
        for (Field field : declaredFields) {
            field.setAccessible(true);
            map.put(field.getName(), field.get(obj));
        }
 
        return map;
    }
 
    /**
     * 判断实体中是否包含某个属性
     *
     * @param clazz
     * @param propertyName
     * @return
     */
    public static boolean containField(Class<?> clazz, String propertyName) {
        /**
         * 循环遍历所有的元素,检测有没有这个名字
         */
        Field[] fields = clazz.getDeclaredFields();
        boolean b = false;
        for (int i = 0; i < fields.length; i++) {
            if (fields[i].getName().equals(propertyName)) {
                b = true;
                break;
            }
        }
        return b;
    }
 
    public static String[] getNullPropertyNames(Object source) {
        final BeanWrapper src = new BeanWrapperImpl(source);
        PropertyDescriptor[] pds = src.getPropertyDescriptors();
 
        Set<String> emptyNames = new HashSet<>();
        for (PropertyDescriptor pd : pds) {
            Object srcValue = src.getPropertyValue(pd.getName());
            if (srcValue == null) {
                emptyNames.add(pd.getName());
            }
        }
        String[] result = new String[emptyNames.size()];
        return emptyNames.toArray(result);
    }
 
    /**
     * 非空拷贝
     *
     * @param source
     * @param target
     */
    public static void copyNotNullBean(Object source, Object target) {
        org.springframework.beans.BeanUtils.copyProperties(source, target, getNullPropertyNames(source));
    }
}