io.github.nichetoolkit.rest.util.BeanUtils Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of rest-toolkit-utils Show documentation
Show all versions of rest-toolkit-utils Show documentation
Rest toolkit utils project for Spring Boot
package io.github.nichetoolkit.rest.util;
import org.springframework.beans.BeanWrapper;
import org.springframework.beans.BeanWrapperImpl;
import java.beans.PropertyDescriptor;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
/**
* BeanUtils
* @author Cyan ([email protected])
* @version v1.0.0
*/
public class BeanUtils {
public static T copyNullProperties(S source, T target) {
org.springframework.beans.BeanUtils.copyProperties(source, target);
return target;
}
public static String[] ignoreProperties(Object source, String... ignoreProperties) {
BeanWrapper src = new BeanWrapperImpl(source);
PropertyDescriptor[] pds = src.getPropertyDescriptors();
Set emptyNames;
if (GeneralUtils.isNotEmpty(ignoreProperties)) {
emptyNames = new HashSet<>(Arrays.asList(ignoreProperties));
} else {
emptyNames = new HashSet<>();
}
for (PropertyDescriptor pd : pds) {
Object srcValue = src.getPropertyValue(pd.getName());
if (srcValue == null) {
emptyNames.add(pd.getName());
}
}
String[] ignorePropertiesNames = new String[emptyNames.size()];
emptyNames.toArray(ignorePropertiesNames);
return ignorePropertiesNames;
}
public static T copyNonullProperties(S source, T target) {
org.springframework.beans.BeanUtils.copyProperties(source, target,ignoreProperties(source));
return target;
}
public static T copyNonullProperties(S source, T target, String... ignoreProperties) {
org.springframework.beans.BeanUtils.copyProperties(source, target,ignoreProperties(source, ignoreProperties));
return target;
}
public static T copyProperties(S source, T target, Class editable) {
org.springframework.beans.BeanUtils.copyProperties(source, target, editable);
return target;
}
public static T copyProperties(S source, T target, String... ignoreProperties) {
org.springframework.beans.BeanUtils.copyProperties(source, target, ignoreProperties);
return target;
}
}