Java 对象复制工具类
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@SuppressWarnings(value = "unused")
@Slf4j
public class BeanUtils {
public static <T, R> List<R> beanCopyPropertiesForList(List<T> sourceList, Class<R> targetCls) {
if (sourceList == null) {
return null;
}
Assert.notNull(targetCls, "target class can not null");
List<R> targetList = new ArrayList<>();
if (!sourceList.isEmpty()) {
for (T source : sourceList) {
targetList.add(beanCopyProperties(source, targetCls));
}
}
return targetList;
}
public static <R> R beanCopyProperties(Object source, Class<R> targetCls) {
try {
if (source == null) {
return null;
}
Assert.notNull(targetCls, "target class can not null");
R target = targetCls.getDeclaredConstructor().newInstance();
org.springframework.beans.BeanUtils.copyProperties(source, target);
return target;
} catch (Exception e) {
log.error(e.getMessage(), e);
return null;
}
}
public static <R> R copyProperties(Object source, Class<R> targetCls) {
return beanCopyProperties(source, targetCls);
}
public static <R> R copyProperties(Object source, R target) {
try {
if (source == null) {
return null;
}
Assert.notNull(target, "target class can not null");
org.springframework.beans.BeanUtils.copyProperties(source, target);
return target;
} catch (Exception e) {
log.error(e.getMessage(), e);
return null;
}
}
public static <R> R beanCopyProperties(Object source, Class<R> targetCls,
String ignoreProperties) {
try {
if (source == null) {
return null;
}
Assert.notNull(targetCls, "target class can not null");
R target = targetCls.getDeclaredConstructor().newInstance();
if (StringUtils.isEmpty(ignoreProperties)) {
org.springframework.beans.BeanUtils.copyProperties(source, target);
} else {
org.springframework.beans.BeanUtils.copyProperties(source, target, ignoreProperties.split(","));
}
return target;
} catch (Exception e) {
log.error(e.getMessage(), e);
return null;
}
}
public static <R> R beanCopyProperties(Object source, Class<R> target, R defVal) {
return beanCopyProperties(source, target, defVal, null);
}
public static <R> R beanCopyProperties(Object source, Class<R> target, R defVal,
String ignoreProperties) {
if (source == null) {
return defVal;
}
return beanCopyProperties(source, target, ignoreProperties);
}
public static void beanCopyProperties(Object source, Object target, String ignoreProperties) {
try {
Assert.notNull(target, "target can not null");
if (source == null) {
return;
}
if (StringUtils.isEmpty(ignoreProperties)) {
org.springframework.beans.BeanUtils.copyProperties(source, target);
} else {
org.springframework.beans.BeanUtils.copyProperties(source, target, ignoreProperties.split(","));
}
} catch (Exception e) {
log.error(e.getMessage(), e);
}
}
public static <T> T valueOrDefault(T value, T defaultValue) {
return value == null ? defaultValue : value;
}
private static Method getMethod(Object bean, String propertyName) {
StringBuilder sb = new StringBuilder("get")
.append(Character.toUpperCase(propertyName.charAt(0)));
if (propertyName.length() > 1) {
sb.append(propertyName.substring(1));
}
String getterName = sb.toString();
for (Method m : bean.getClass().getMethods()) {
if (getterName.equals(m.getName()) && m.getParameterTypes().length == 0) {
return m;
}
}
return null;
}
private static Field getField(Object bean, String propertyName) {
for (Field f : bean.getClass().getDeclaredFields()) {
if (propertyName.equals(f.getName())) {
return f;
}
}
return null;
}
private static void validateArgs(Object bean, String propertyName) {
if (bean == null) {
throw new IllegalArgumentException("bean is null");
}
if (propertyName == null) {
throw new IllegalArgumentException("propertyName is null");
}
if (propertyName.trim().length() == 0) {
throw new IllegalArgumentException("propertyName is empty");
}
}
public static Object getBeanProperty(Object bean, String propertyName) {
validateArgs(bean, propertyName);
Method getter = getMethod(bean, propertyName);
if (getter != null) {
try {
return getter.invoke(bean);
} catch (Exception ignored) {
}
}
Field field = getField(bean, propertyName);
if (field != null) {
try {
field.setAccessible(true);
return field.get(bean);
} catch (Exception ignored) {
}
}
return null;
}
public static long getLongBeanProperty(final Object bean, final String propertyName)
throws NoSuchFieldException {
validateArgs(bean, propertyName);
Object o = getBeanProperty(bean, propertyName);
if (o == null) {
throw new NoSuchFieldException(propertyName);
} else if (!(o instanceof Number)) {
throw new IllegalArgumentException(propertyName + " not an Number");
}
return ((Number) o).longValue();
}
public static <R> R mapToClass(Map<String, ?> params, Class<R> targetCls) {
Assert.notEmpty(params, "params can not empty");
try {
return JSON.parseObject(JSON.toJSONString(params), targetCls);
} catch (Exception e) {
log.error(e.getMessage(), e);
return null;
}
}
public static Map<String, Object> beanToMap(Object obj) {
Assert.notNull(obj, "params can not empty");
Map<String, Object> map = new HashMap<>();
Class<?> clazz = obj.getClass();
try {
for (Field field : clazz.getDeclaredFields()) {
field.setAccessible(true);
String fieldName = field.getName();
Object value = field.get(obj);
map.put(fieldName, value);
}
} catch (Exception e) {
log.error(e.getMessage(), e);
return null;
}
return map;
}
}