xiaoyong931011
2021-07-21 f79dd632f83167b3cd5ad01e2f7c494b92c834d3
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
package com.xzx.gc.util;
 
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.experimental.UtilityClass;
import org.springframework.beans.BeanWrapper;
import org.springframework.beans.BeanWrapperImpl;
 
import java.util.HashSet;
import java.util.Set;
 
@UtilityClass
public class BeanUtils {
 
    /**
     * @Description <p>获取到对象中属性不为空的属性名  </P>
     * @param object 对象
     * @return
     */
    public  String[] getNotNullPropertyNames(Object object) {
        final BeanWrapper src = new BeanWrapperImpl(object);
        java.beans.PropertyDescriptor[] pds = src.getPropertyDescriptors();
 
        Set<String> emptyNames = new HashSet<>();
        for (java.beans.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);
    }
 
    /**
     * copy属性
     * @param o
     * @param toValueType
     * @param <T>
     * @return
     */
    public <T> T copy(Object o,Class<T> toValueType){
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        return objectMapper.convertValue(o, toValueType);
    }
}