Chinaunix首页 | 论坛 | 博客
  • 博客访问: 166097
  • 博文数量: 56
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 593
  • 用 户 组: 普通用户
  • 注册时间: 2014-02-18 09:59
文章分类

全部博文(56)

文章存档

2019年(1)

2018年(26)

2016年(1)

2015年(6)

2014年(22)

我的朋友

分类: Java

2018-06-29 16:07:09



做框架的时候, 这个工具类非常有用, 深拷贝, 浅拷贝, 对象属性转换为map

包含:
  • 浅层拷贝,将src对象的元数据拷贝至dest对象的同名属性中(注意:不包含继承的父类域)
  • 深层拷贝  包含父类的域, object类的属性不会进行拷贝
  • 对象属性转换到map中


点击(此处)折叠或打开

  1. import com.google.common.collect.Lists;

  2. import java.lang.reflect.Field;
  3. import java.lang.reflect.Method;
  4. import java.util.*;

  5. /**
  6.  * 对象拷贝工具
  7.  */
  8. public class BeanUtil {

  9.     /**
  10.      * 浅层拷贝 将src对象的元数据拷贝至dest对象的同名属性中
  11.      * !注意!该方法拷贝的域不包含继承的父类域
  12.      *
  13.      * @param src 源对象
  14.      * @param dest 目标对象
  15.      * @param fieldBySrc true 域按照源对象拷贝 false 按目标域拷贝
  16.      */
  17.     public static <T> T shallowCopy(Object src, T dest, Boolean fieldBySrc) {
  18.         Class<? extends Object> c = src.getClass();
  19.         Class<? extends Object> c2 = dest.getClass();
  20.         Field fields[] = fieldBySrc ? c.getDeclaredFields() : c2.getDeclaredFields();
  21.         for (int i = 0; i < fields.length; i++) {
  22.             Field field = fields[i];
  23.             String fieldName = field.getName();
  24.             String firstLetter = fieldName.substring(0, 1).toUpperCase();
  25.             String getMethodName = "get" + firstLetter + fieldName.substring(1);
  26.             String setMethodName = "set" + firstLetter + fieldName.substring(1);
  27.             try {
  28.                 Method getMethod = c.getMethod(getMethodName, new Class[]{});
  29.                 Method setMethod = c2.getMethod(setMethodName,
  30.                         new Class[]{field.getType()});
  31.                 if (getMethod != null && setMethod != null) {
  32.                     setMethod.invoke(dest, new Object[]{getMethod.invoke(src, new Object[]{})});
  33.                 }
  34.             } catch (Exception e) {

  35.             }
  36.         }
  37.         return dest;
  38.     }

  39.     /**
  40.      * 浅层拷贝 | 域按照源对象拷贝
  41.      *
  42.      * @param src 源对象
  43.      * @param dest 目标对象
  44.      */
  45.     public static <T> T shallowCopy(Object src, T dest) {
  46.         return shallowCopy(src, dest, true);
  47.     }

  48.     /**
  49.      * 浅拷贝
  50.      *
  51.      * @param src 源对象
  52.      * @param dest 目标类
  53.      * @param fieldBySrc true 域按照源对象拷贝 false 按目标域拷贝
  54.      * @return
  55.      */
  56.     public static <T> T shallowCopy(Object src, Class<T> dest, Boolean fieldBySrc) {
  57.         T ret = null;
  58.         try {
  59.             //必须有无参 #C
  60.             ret = shallowCopy(src, dest.newInstance(), fieldBySrc);
  61.         } catch (InstantiationException e) {
  62.         } catch (IllegalAccessException e) {
  63.         }
  64.         return ret;
  65.     }

  66.     /**
  67.      * 浅拷贝 | 域按照源对象拷贝
  68.      *
  69.      * @param src 源对象
  70.      * @param dest 目标类
  71.      * @return
  72.      */
  73.     public static <T> T shallowCopy(Object src, Class<T> dest) {
  74.         return shallowCopy(src, dest, true);
  75.     }

  76.     /**
  77.      * 浅层拷贝 将src对象的元数据拷贝至dest对象的同名属性中
  78.      * !注意!该方法拷贝的域包含继承的父类域
  79.      *
  80.      * @param src 源对象
  81.      * @param dest 目标对象
  82.      * @param fieldBySrc true 域按照源对象拷贝 false 按目标域拷贝
  83.      */
  84.     public static <T> T shallowCopyFields(Object src, T dest, Boolean fieldBySrc) {
  85.         Class<? extends Object> c = src.getClass();
  86.         Class<? extends Object> c2 = dest.getClass();
  87.         List<Field> fields = fieldBySrc ? getAllField(c) : getAllField(c2);
  88.         for (int i = 0; i < fields.size(); i++) {
  89.             Field field = fields.get(i);
  90.             String fieldName = field.getName();
  91.             String firstLetter = fieldName.substring(0, 1).toUpperCase();
  92.             String getMethodName = "get" + firstLetter + fieldName.substring(1);
  93.             String setMethodName = "set" + firstLetter + fieldName.substring(1);
  94.             try {
  95.                 Method getMethod = c.getMethod(getMethodName, new Class[]{});
  96.                 Method setMethod = c2.getMethod(setMethodName,
  97.                         new Class[]{field.getType()});
  98.                 if (getMethod != null && setMethod != null) {
  99.                     setMethod.invoke(dest, new Object[]{getMethod.invoke(src, new Object[]{})});
  100.                 }
  101.             } catch (Exception e) {
  102.             }
  103.         }
  104.         return dest;
  105.     }

  106.     public static <T> T shallowCopyFields(Object src, Class<T> dest, Boolean fieldBySrc) {
  107.         T ret = null;
  108.         try {
  109.             //必须有无参 #C
  110.             ret = shallowCopyFields(src, dest.newInstance(), fieldBySrc);
  111.         } catch (InstantiationException e) {
  112.         } catch (IllegalAccessException e) {
  113.         }
  114.         return ret;
  115.     }

  116.     /**
  117.      * 递归获取所有域
  118.      *
  119.      * @param clazz Class对象
  120.      * @return
  121.      */
  122.     public static List<Field> getAllField(Class clazz) {
  123.         List<Field> fieldList = new ArrayList<>();
  124.         fieldList.addAll(Arrays.asList(clazz.getDeclaredFields()));
  125.         Class superClazz = clazz.getSuperclass();
  126.         if (clazz.getSuperclass() == null) {
  127.             return fieldList;
  128.         } else {
  129.             fieldList.addAll(getAllField(superClazz));
  130.             return fieldList;
  131.         }
  132.     }

  133.     /**
  134.      * 浅层拷贝 | 域按照源对象拷贝
  135.      *
  136.      * @param src 源对象
  137.      * @param dest 目标对象
  138.      */
  139.     public static <T> T shallowCopyFields(Object src, Class<T> dest) {
  140.         return shallowCopyFields(src, dest, true);
  141.     }

  142.     /**
  143.      * 浅层拷贝 | 域按照源对象拷贝
  144.      *
  145.      * @param src 源对象
  146.      * @param dest 目标对象
  147.      */
  148.     public static <T> T shallowCopyFields(Object src, T dest) {
  149.         return shallowCopyFields(src, dest, true);
  150.     }

  151.     public static <T> T deepCopyFields(Object src, T dest, Boolean fieldBySrc) {
  152.         Class<? extends Object> c = src.getClass();
  153.         Class<? extends Object> c2 = dest.getClass();
  154.         List<Field> fields = fieldBySrc ? Lists.newArrayList(getFieldWithParaent(c)) : Lists.newArrayList(getFieldWithParaent(c2));
  155.         for (int i = 0; i < fields.size(); i++) {
  156.             Field field = fields.get(i);
  157.             String fieldName = field.getName();
  158.             String firstLetter = fieldName.substring(0, 1).toUpperCase();
  159.             String getMethodName = "get" + firstLetter + fieldName.substring(1);
  160.             String setMethodName = "set" + firstLetter + fieldName.substring(1);
  161.             try {
  162.                 Method getMethod = c.getMethod(getMethodName, new Class[]{});
  163.                 Method setMethod = c2.getMethod(setMethodName,
  164.                         new Class[]{field.getType()});
  165.                 if (getMethod != null && setMethod != null) {
  166.                     setMethod.invoke(dest, new Object[]{getMethod.invoke(src, new Object[]{})});
  167.                 }
  168.             } catch (Exception e) {
  169.             }
  170.         }
  171.         return dest;
  172.     }

  173.     public static <T> T deepCopyFields(Object src, Class<T> dest, Boolean fieldBySrc) {
  174.         T ret = null;
  175.         try {
  176.             //必须有无参 #C
  177.             ret = deepCopyFields(src, dest.newInstance(), fieldBySrc);
  178.         } catch (InstantiationException e) {
  179.         } catch (IllegalAccessException e) {
  180.         }
  181.         return ret;
  182.     }

  183.     public static <T> T deepCopyFields(Object src, Class<T> dest) {
  184.         return deepCopyFields(src, dest, true);
  185.     }

  186.     public static <T> T deepCopyFields(Object src, T dest) {
  187.         return shallowCopyFields(src, dest, true);
  188.     }




  189.     /**
  190.      * 对象字段转为map
  191.      * 过滤掉值为null "" 0 0l
  192.      */
  193.     public static Map<String, Object> toMap(Object obj) {
  194.         Map<String, Object> map = new HashMap<String, Object>();
  195.         if (obj == null) {
  196.             return map;
  197.         }
  198.         Field fields[] = getFieldWithParaent(obj);
  199.         for (int i = 0; i < fields.length; i++) {
  200.             Field field = fields[i];
  201.             String fieldName = field.getName();
  202.             String firstLetter = fieldName.substring(0, 1).toUpperCase();
  203.             String getMethodName = "get" + firstLetter + fieldName.substring(1);
  204.             try {
  205.                 Method getMethod = obj.getClass().getMethod(getMethodName, new Class[]{});
  206.                 if (getMethod != null) {
  207.                     Object fieldValue = getMethod.invoke(obj, new Object[]{});
  208.                     if ((fieldValue != null && !fieldValue.equals("") && !fieldValue.equals(0) && !fieldValue.equals(0l))
  209.                             || fieldName.equals("pageIndex")) {
  210.                         map.put(fieldName, fieldValue);
  211.                     }
  212.                 }
  213.             } catch (Exception e) {
  214.             }
  215.         }
  216.         return map;
  217.     }

  218.     private static Field[] getFieldWithParaent(Object obj){
  219.         if(obj == null) {
  220.             return new Field[0];
  221.         }
  222.         List<Field> fieldList = new ArrayList<>() ;
  223.         Class tempClass = obj.getClass();
  224.         while (tempClass != null && !tempClass.equals(Object.class)) {//当父类为null的时候说明到达了最上层的父类(Object类).
  225.             fieldList.addAll(Arrays.asList(tempClass.getDeclaredFields()));
  226.             tempClass = tempClass.getSuperclass(); //得到父类,然后赋给自己
  227.         }
  228.         return fieldList.toArray(new Field[fieldList.size()]);

  229.     }
  230. }

阅读(1712) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~