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);
|
}
|
}
|