两个实体类 复制 copy 工具类

  1 import java.lang.reflect.Field;
  2 import java.lang.reflect.Method;
  3 import java.util.Date;
  4 
  5 public class CopyBean{
  6 
  7     /**
  8      * 复制sour里属性不为空的值到obje为空的属性
  9      * 
 10      * @param obje
 11      *            目标实体类
 12      * @param sour
 13      *            源实体类
 14      * @param isCover
 15      *            是否保留obje类里不为null的属性值(保留源值,属性为null则赋值)
 16      * @return obje
 17      */
 18     public static Object Copy(Object obje, Object sour, boolean isCover) {
 19         Field[] fields = sour.getClass().getDeclaredFields();
 20         for (int i = 0, j = fields.length; i < j; i++) {
 21             String propertyName = fields[i].getName();
 22             Object propertyValue = getProperty(sour, propertyName);
 23             if (isCover) {
 24                 if (getProperty(obje, propertyName) == null
 25                         && propertyValue != null) {
 26                     Object setProperty = setProperty(obje, propertyName,
 27                             propertyValue);
 28                 }
 29             } else {
 30                 Object setProperty = setProperty(obje, propertyName,
 31                         propertyValue);
 32             }
 33 
 34         }
 35         return obje;
 36     }
 37 
 38     /**
 39      * 得到值
 40      * 
 41      * @param bean
 42      * @param propertyName
 43      * @return
 44      */
 45     private static Object getProperty(Object bean, String propertyName) {
 46         Class clazz = bean.getClass();
 47         try {
 48             Field field = clazz.getDeclaredField(propertyName);
 49             Method method = clazz.getDeclaredMethod(
 50                     getGetterName(field.getName()), new Class[] {});
 51             return method.invoke(bean, new Object[] {});
 52         } catch (Exception e) {
 53         }
 54         return null;
 55     }
 56 
 57     /**
 58      * 给bean赋值
 59      * 
 60      * @param bean
 61      * @param propertyName
 62      * @param value
 63      * @return
 64      */
 65     private static Object setProperty(Object bean, String propertyName,
 66             Object value) {
 67         Class clazz = bean.getClass();
 68         try {
 69             Field field = clazz.getDeclaredField(propertyName);
 70             Method method = clazz.getDeclaredMethod(
 71                     getSetterName(field.getName()),
 72                     new Class[] { field.getType() });
 73             return method.invoke(bean, new Object[] { value });
 74         } catch (Exception e) {
 75         }
 76         return null;
 77     }
 78 
 79     /**
 80      * 根据变量名得到get方法
 81      * 
 82      * @param propertyName
 83      * @return
 84      */
 85     private static String getGetterName(String propertyName) {
 86         String method = "get" + propertyName.substring(0, 1).toUpperCase()
 87                 + propertyName.substring(1);
 88         return method;
 89     }
 90 
 91     /**
 92      * 得到setter方法
 93      * 
 94      * @param propertyName
 95      *            变量名
 96      * @return
 97      */
 98     private static String getSetterName(String propertyName) {
 99         String method = "set" + propertyName.substring(0, 1).toUpperCase()
100                 + propertyName.substring(1);
101         return method;
102     }
103 
104 //    public static void main(String[] args) {
105 //        User u = new User();
106 //        u.setId(1l);
107 //        u.setAge(30);
108 //        User u1 = new User();
109 //        u1.setAge(10);
110 //        u1.setBirthday(new Date());
111 //        u1.setFirtsName("aaaa");
112 //        u1.setName("adf");
113 //        u1.setSchool("aaaa");
114 //        Field[] fields = u1.getClass().getDeclaredFields();
115 //        u.setSchool("bbbbbbbbb");
116 //        System.out.println("u1--------->  " + u1);
117 //        System.out.println("u---------->  " + u);
118 //        System.out.println(Copy(u, u1, false));
119 //    }
120 
121 }
原文地址:https://www.cnblogs.com/shaoyu19900421/p/4036290.html