zoukankan      html  css  js  c++  java
  • 通过反射,给对象之间赋值

    /**
      * 通过反射,给对象赋值
      * add by wangHao 2014-01-08
      * @param source
      * @param dest
      * @throws Exception
      */
        public  void CopyObject(Object source,Object dest)throws Exception { 
            BeanInfo sourceBean = Introspector.getBeanInfo(source.getClass(), java.lang.Object.class); 
            PropertyDescriptor[] sourceProperty = sourceBean.getPropertyDescriptors(); 
            BeanInfo destBean = Introspector.getBeanInfo(dest.getClass(), java.lang.Object.class); 
            PropertyDescriptor[] destProperty = destBean.getPropertyDescriptors(); 
            try{ 
                for(int i=0;i<sourceProperty.length;i++){ 
                     if( "id".equals(sourceProperty[i].getName())){
                        continue;
                     }
                    for(int j=0;j<destProperty.length;j++){ 
                        if(sourceProperty[i].getName().equals(destProperty[j].getName())){ 
                            destProperty[j].getWriteMethod().invoke(dest, sourceProperty[i].getReadMethod().invoke(source)); 
                            break;                   
                        } 
                    } 
                } 
            }catch(Exception e){ 
                throw new Exception("属性复制失败:",e); 
            } 
        }

  • 相关阅读:
    12.python笔记之mysqldb模块
    13.python笔记之pyyaml模块
    11.python之线程,协程,进程,
    2.saltstack笔记之目标,模块,返回写入数据库
    6.django笔记之orm
    5.django笔记之form保存表单信息,动态select
    4.django笔记之admin
    docker批量删除none镜像
    docker 给none镜像打镜像
    jenkins卡在等待界面解决方法
  • 原文地址:https://www.cnblogs.com/holdon521/p/4118587.html
Copyright © 2011-2022 走看看