public class BeanUtils {
private static final String DEFAULT_HYPHEN = "-";
/**
* 对象拷贝,先使用Spring的BeanUtils进行浅拷贝,再尝试拷贝属性名称相同、但类型不同的字段
*
* @param source 源对象
* @param target 目标对象
*/
public static void copyProperties(Object source, Object target) {
// 先做属性类型、属性名称都一致的基本拷贝
org.springframework.beans.BeanUtils.copyProperties(source, target);
// 尝试拷贝属性名称相同、但类型不同的字段
try {
copyPropertiesExtension(source, target);
} catch (Exception e) {
log.error("反射调用/访问方法异常", e);
throw new RuntimeException(e);
}
}
/**
* 尝试拷贝属性名称相同、但类型不同的字段
*
* @param source 源对象
* @param target 目标对象
* @throws InvocationTargetException 调用目标方法异常
* @throws IllegalAccessException 非法访问异常
*/
public static void copyPropertiesExtension(Object source, Object target) throws InvocationTargetException, IllegalAccessException {
Class<?> sourceClazz = source.getClass();
Field[] sourceFields = sourceClazz.getDeclaredFields();
Class<?> targetClazz = target.getClass();
Field[] targetFields = targetClazz.getDeclaredFields();
HashMap<String, Field> targetFieldsMap = Maps.newHashMap();
for (Field targetField : targetFields) {
targetFieldsMap.put(targetField.getName(), targetField);
}
for (Field sourceField : sourceFields) {
// 查找属性名相同字段
String sourceFieldName = sourceField.getName();
Field targetField = targetFieldsMap.get(sourceFieldName);
if (targetField != null && "class java.lang.String".equals(targetField.getType().toString())) {
targetField.setAccessible(true);
Method sourceMethod;
try {
sourceMethod = source.getClass().getMethod("get" + getMethodName(sourceFieldName));
} catch (NoSuchMethodException e) {
log.error("找不到方法", e);
throw new RuntimeException(e);
}
// 判断属性类型
String fieldType = sourceField.getType().toString();
if ("class java.lang.Double".equals(fieldType)) {
// Double转String
Double invoke = (Double) sourceMethod.invoke(source);
if (invoke != null) {
targetField.set(target, invoke.toString());
}
} else if ("class java.lang.Integer".equals(fieldType)) {
// Integer转String
Integer invoke = (Integer) sourceMethod.invoke(source);
if (invoke != null) {
targetField.set(target, invoke.toString());
}
} else if ("class java.lang.Long".equals(fieldType)) {
// Long转String
Long invoke = (Long) sourceMethod.invoke(source);
if (invoke != null) {
targetField.set(target, invoke.toString());
}
}
}
}
}
/**
* 把一个字符串的第一个字母大写、效率是最高的
*
* @param str 字符串
* @return 第一个字母转大写后的str
*/
private static String getMethodName(String str) {
byte[] items = str.getBytes();
items[0] = (byte) ((char) items[0] - 'a' + 'A');
return new String(items);
}
/**
* 转换JavaBean空字段的Value
*
* @param obj obj
*/
public static void convertNullFieldValue(Object obj) {
convertNullFieldValue(obj, DEFAULT_HYPHEN);
}
/**
* 转换JavaBean空字段的Value
*
* @param obj obj
* @param str 空字段的值
*/
public static void convertNullFieldValue(Object obj, String str) {
Class<?> clazz = obj.getClass();
Field[] declaredFields = clazz.getDeclaredFields();
if (declaredFields.length == 0) {
return;
}
for (Field field : declaredFields) {
field.setAccessible(true);
try {
if (field.get(obj) == null && "class java.lang.String".equals(field.getType().toString())) {
field.set(obj, str);
}
} catch (IllegalAccessException e) {
throw new RuntimeException();
}
}
}
}
12-25
6282

10-18
4840
