复制

     public static boolean copy(Object source, Object to) {  
         try {  
             BeanInfo sourceBean = Introspector.getBeanInfo(source.getClass(),java.lang.Object.class);  
             PropertyDescriptor[] sourceProperty = sourceBean.getPropertyDescriptors();  
             
             BeanInfo destBean = Introspector.getBeanInfo(to.getClass(),java.lang.Object.class);  
             PropertyDescriptor[] destProperty = destBean.getPropertyDescriptors();  
             
             for (int i = 0; i < sourceProperty.length; i++) {  
                 for (int j = 0; j < destProperty.length; j++) {  
                     if (sourceProperty[i].getName().equals(destProperty[j].getName())) {  
                         destProperty[j].getWriteMethod().invoke(to,sourceProperty[i].getReadMethod().invoke(source));  
                         break;  
                     }  
                 }  
             }
             return true;
         } catch (Exception e) {  
             e.printStackTrace();
             return false;
         }  
     }

 

posted @ 2019-09-25 15:22  Goatherd  阅读(160)  评论(0编辑  收藏  举报