做框架的时候, 这个工具类非常有用, 深拷贝, 浅拷贝, 对象属性转换为map
包含:
- 浅层拷贝,将src对象的元数据拷贝至dest对象的同名属性中(注意:不包含继承的父类域)
- 深层拷贝 包含父类的域, object类的属性不会进行拷贝
- 对象属性转换到map中
点击(此处)折叠或打开
- import com.google.common.collect.Lists;
- import java.lang.reflect.Field;
- import java.lang.reflect.Method;
- import java.util.*;
- /**
- * 对象拷贝工具
- */
- public class BeanUtil {
- /**
- * 浅层拷贝 将src对象的元数据拷贝至dest对象的同名属性中
- * !注意!该方法拷贝的域不包含继承的父类域
- *
- * @param src 源对象
- * @param dest 目标对象
- * @param fieldBySrc true 域按照源对象拷贝 false 按目标域拷贝
- */
- public static <T> T shallowCopy(Object src, T dest, Boolean fieldBySrc) {
- Class<? extends Object> c = src.getClass();
- Class<? extends Object> c2 = dest.getClass();
- Field fields[] = fieldBySrc ? c.getDeclaredFields() : c2.getDeclaredFields();
- for (int i = 0; i < fields.length; i++) {
- Field field = fields[i];
- String fieldName = field.getName();
- String firstLetter = fieldName.substring(0, 1).toUpperCase();
- String getMethodName = "get" + firstLetter + fieldName.substring(1);
- String setMethodName = "set" + firstLetter + fieldName.substring(1);
- try {
- Method getMethod = c.getMethod(getMethodName, new Class[]{});
- Method setMethod = c2.getMethod(setMethodName,
- new Class[]{field.getType()});
- if (getMethod != null && setMethod != null) {
- setMethod.invoke(dest, new Object[]{getMethod.invoke(src, new Object[]{})});
- }
- } catch (Exception e) {
- }
- }
- return dest;
- }
- /**
- * 浅层拷贝 | 域按照源对象拷贝
- *
- * @param src 源对象
- * @param dest 目标对象
- */
- public static <T> T shallowCopy(Object src, T dest) {
- return shallowCopy(src, dest, true);
- }
- /**
- * 浅拷贝
- *
- * @param src 源对象
- * @param dest 目标类
- * @param fieldBySrc true 域按照源对象拷贝 false 按目标域拷贝
- * @return
- */
- public static <T> T shallowCopy(Object src, Class<T> dest, Boolean fieldBySrc) {
- T ret = null;
- try {
- //必须有无参 #C
- ret = shallowCopy(src, dest.newInstance(), fieldBySrc);
- } catch (InstantiationException e) {
- } catch (IllegalAccessException e) {
- }
- return ret;
- }
- /**
- * 浅拷贝 | 域按照源对象拷贝
- *
- * @param src 源对象
- * @param dest 目标类
- * @return
- */
- public static <T> T shallowCopy(Object src, Class<T> dest) {
- return shallowCopy(src, dest, true);
- }
- /**
- * 浅层拷贝 将src对象的元数据拷贝至dest对象的同名属性中
- * !注意!该方法拷贝的域包含继承的父类域
- *
- * @param src 源对象
- * @param dest 目标对象
- * @param fieldBySrc true 域按照源对象拷贝 false 按目标域拷贝
- */
- public static <T> T shallowCopyFields(Object src, T dest, Boolean fieldBySrc) {
- Class<? extends Object> c = src.getClass();
- Class<? extends Object> c2 = dest.getClass();
- List<Field> fields = fieldBySrc ? getAllField(c) : getAllField(c2);
- for (int i = 0; i < fields.size(); i++) {
- Field field = fields.get(i);
- String fieldName = field.getName();
- String firstLetter = fieldName.substring(0, 1).toUpperCase();
- String getMethodName = "get" + firstLetter + fieldName.substring(1);
- String setMethodName = "set" + firstLetter + fieldName.substring(1);
- try {
- Method getMethod = c.getMethod(getMethodName, new Class[]{});
- Method setMethod = c2.getMethod(setMethodName,
- new Class[]{field.getType()});
- if (getMethod != null && setMethod != null) {
- setMethod.invoke(dest, new Object[]{getMethod.invoke(src, new Object[]{})});
- }
- } catch (Exception e) {
- }
- }
- return dest;
- }
- public static <T> T shallowCopyFields(Object src, Class<T> dest, Boolean fieldBySrc) {
- T ret = null;
- try {
- //必须有无参 #C
- ret = shallowCopyFields(src, dest.newInstance(), fieldBySrc);
- } catch (InstantiationException e) {
- } catch (IllegalAccessException e) {
- }
- return ret;
- }
- /**
- * 递归获取所有域
- *
- * @param clazz Class对象
- * @return
- */
- public static List<Field> getAllField(Class clazz) {
- List<Field> fieldList = new ArrayList<>();
- fieldList.addAll(Arrays.asList(clazz.getDeclaredFields()));
- Class superClazz = clazz.getSuperclass();
- if (clazz.getSuperclass() == null) {
- return fieldList;
- } else {
- fieldList.addAll(getAllField(superClazz));
- return fieldList;
- }
- }
- /**
- * 浅层拷贝 | 域按照源对象拷贝
- *
- * @param src 源对象
- * @param dest 目标对象
- */
- public static <T> T shallowCopyFields(Object src, Class<T> dest) {
- return shallowCopyFields(src, dest, true);
- }
- /**
- * 浅层拷贝 | 域按照源对象拷贝
- *
- * @param src 源对象
- * @param dest 目标对象
- */
- public static <T> T shallowCopyFields(Object src, T dest) {
- return shallowCopyFields(src, dest, true);
- }
- public static <T> T deepCopyFields(Object src, T dest, Boolean fieldBySrc) {
- Class<? extends Object> c = src.getClass();
- Class<? extends Object> c2 = dest.getClass();
- List<Field> fields = fieldBySrc ? Lists.newArrayList(getFieldWithParaent(c)) : Lists.newArrayList(getFieldWithParaent(c2));
- for (int i = 0; i < fields.size(); i++) {
- Field field = fields.get(i);
- String fieldName = field.getName();
- String firstLetter = fieldName.substring(0, 1).toUpperCase();
- String getMethodName = "get" + firstLetter + fieldName.substring(1);
- String setMethodName = "set" + firstLetter + fieldName.substring(1);
- try {
- Method getMethod = c.getMethod(getMethodName, new Class[]{});
- Method setMethod = c2.getMethod(setMethodName,
- new Class[]{field.getType()});
- if (getMethod != null && setMethod != null) {
- setMethod.invoke(dest, new Object[]{getMethod.invoke(src, new Object[]{})});
- }
- } catch (Exception e) {
- }
- }
- return dest;
- }
- public static <T> T deepCopyFields(Object src, Class<T> dest, Boolean fieldBySrc) {
- T ret = null;
- try {
- //必须有无参 #C
- ret = deepCopyFields(src, dest.newInstance(), fieldBySrc);
- } catch (InstantiationException e) {
- } catch (IllegalAccessException e) {
- }
- return ret;
- }
- public static <T> T deepCopyFields(Object src, Class<T> dest) {
- return deepCopyFields(src, dest, true);
- }
- public static <T> T deepCopyFields(Object src, T dest) {
- return shallowCopyFields(src, dest, true);
- }
- /**
- * 对象字段转为map
- * 过滤掉值为null "" 0 0l
- */
- public static Map<String, Object> toMap(Object obj) {
- Map<String, Object> map = new HashMap<String, Object>();
- if (obj == null) {
- return map;
- }
- Field fields[] = getFieldWithParaent(obj);
- for (int i = 0; i < fields.length; i++) {
- Field field = fields[i];
- String fieldName = field.getName();
- String firstLetter = fieldName.substring(0, 1).toUpperCase();
- String getMethodName = "get" + firstLetter + fieldName.substring(1);
- try {
- Method getMethod = obj.getClass().getMethod(getMethodName, new Class[]{});
- if (getMethod != null) {
- Object fieldValue = getMethod.invoke(obj, new Object[]{});
- if ((fieldValue != null && !fieldValue.equals("") && !fieldValue.equals(0) && !fieldValue.equals(0l))
- || fieldName.equals("pageIndex")) {
- map.put(fieldName, fieldValue);
- }
- }
- } catch (Exception e) {
- }
- }
- return map;
- }
- private static Field[] getFieldWithParaent(Object obj){
- if(obj == null) {
- return new Field[0];
- }
- List<Field> fieldList = new ArrayList<>() ;
- Class tempClass = obj.getClass();
- while (tempClass != null && !tempClass.equals(Object.class)) {//当父类为null的时候说明到达了最上层的父类(Object类).
- fieldList.addAll(Arrays.asList(tempClass.getDeclaredFields()));
- tempClass = tempClass.getSuperclass(); //得到父类,然后赋给自己
- }
- return fieldList.toArray(new Field[fieldList.size()]);
- }
- }