• 通过反射,给对象之间赋值


    /**
      * 通过反射,给对象赋值
      * 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); 
            } 
        }

  • 相关阅读:
    什么是电信BOSS系统?
    得到windows系统图标的解决方案
    FusionChart实现金字塔分布图
    OCP-1Z0-051-V9.02-91题
    FusionChart用XML和JSON两种格式提供数据源
    OCP-1Z0-051-V9.02-156题
    OCP-1Z0-051-V9.02-155题
    OCP-1Z0-051-V9.02-154题
    OCP-1Z0-051-V9.02-153题
    OCP-1Z0-051-V9.02-151题
  • 原文地址:https://www.cnblogs.com/holdon521/p/4118587.html
Copyright © 2020-2023  润新知