Java学习者论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

手机号码,快捷登录

恭喜Java学习者论坛(https://www.javaxxz.com)已经为数万Java学习者服务超过8年了!积累会员资料超过10000G+
成为本站VIP会员,下载本站10000G+会员资源,购买链接:点击进入购买VIP会员
JAVA高级面试进阶视频教程Java架构师系统进阶VIP课程

分布式高可用全栈开发微服务教程

Go语言视频零基础入门到精通

Java架构师3期(课件+源码)

Java开发全终端实战租房项目视频教程

SpringBoot2.X入门到高级使用教程

大数据培训第六期全套视频教程

深度学习(CNN RNN GAN)算法原理

Java亿级流量电商系统视频教程

互联网架构师视频教程

年薪50万Spark2.0从入门到精通

年薪50万!人工智能学习路线教程

年薪50万!大数据从入门到精通学习路线年薪50万!机器学习入门到精通视频教程
仿小米商城类app和小程序视频教程深度学习数据分析基础到实战最新黑马javaEE2.1就业课程从 0到JVM实战高手教程 MySQL入门到精通教程
查看: 286|回复: 0

[Java反射学习]反射学习:JavaBean的复制

[复制链接]
  • TA的每日心情
    开心
    2021-3-12 23:18
  • 签到天数: 2 天

    [LV.1]初来乍到

    发表于 2014-10-28 23:56:32 | 显示全部楼层 |阅读模式
    应用的场景是:
    页面上一个javaBean已经有数据了,再创建一个JavaBean,想将这些数据复制到这个新的Bean中
    有了这个方法,就不用自己手动的写这些代码了:
    bean2.setXxx(bean1.getXxx())
    ...
    ...
    ...

    代码如下:
    1. import java.lang.reflect.Method;  
    2.    
    3. import java.util.ArrayList;  
    4. import java.util.List;  
    5. import java.util.ListIterator;  
    6.    
    7. /**
    8.   * 复制相似Row之间的数据,比如讲一个有数据的Row的所有数据选择性的复制到一个空的Row中,通过属性的set、get方法

    9.   * desRow和srcRow的类型是一样的

    10.   * 只能复制RowImpl类型的对象

    11.   * 注意:栏位以VO中Attribute标签里的列表为准,desRow中存在的属性则srcRow中必须存在,
    12.   * 否则在needlessColumn_List加入srcRow中不存在的属性
    13.   */  
    14. public class CloneUtil {  
    15.      private Object desRow; // 目标Row  
    16.      private Object srcRow; // 源Row  
    17.      private List
    18.    
    19.       needlessColumn_List; // 需要跳过的属性的List  
    20.    
    21.      public CloneUtil(Object desRow, Object srcRow,  
    22.              List
    23.      
    24.        needlessColumn_List) {  
    25.          this.desRow = desRow;  
    26.          this.srcRow = srcRow;  
    27.          this.needlessColumn_List = needlessColumn_List;  
    28.      }  
    29.    
    30.      /**
    31.       * 主方法
    32.       *  
    33.       * @throws Exception
    34.       */  
    35.      public void deepClone() {  
    36.          Method setMethod = null; // 源 Bean中的属性的set方法  
    37.          Method getMethod = null; // 源 Bean中的属性的get方法  
    38.    
    39.          Class desRowClass = desRow.getClass(); // 得到desRow的Class
    40.           // 所有Public范围的方法,包括从父类继承的和从接口实现的公共方法
    41.          Method[] publicMethods = desRowClass.getMethods();  
    42.           // 本类定义的所有范围的方法,不包括从父类继承的和从接口实现的方法  
    43.          Method[] exceptedSuperMethods = desRowClass.getDeclaredMethods();
    44.          List
    45.       
    46.         neededMethodsList = new ArrayList
    47.       
    48.         (); // 保存Bean有对应属性的方法的List  
    49.          fetchNeededMethods(publicMethods, exceptedSuperMethods,  
    50.                  neededMethodsList); // 只将Bean的属性的set方法放入neededMethodsList中以便处理  
    51.          try {  
    52.              for (Method neededMethod : neededMethodsList) { // 循环处理此desRow的Class对应的所有方法  
    53.                  String neededMethodName = neededMethod.getName();  
    54.                  // 跳过不需要复制的属性  
    55.                  boolean inList = false;  
    56.                  if (this.needlessColumn_List != null  
    57.                          && !needlessColumn_List.isEmpty()) {  
    58.                      inList = checkNeedlessColumn(neededMethodName); // 检查不需要复制的属性  
    59.                      if (inList == false) { // 如果当前属性名称存在于needlessColumn_List中,则跳过  
    60.                          continue;  
    61.                      }  
    62.                  }  
    63.                  // 得到源Bean中的属性的set方法  
    64.                  setMethod = srcRow.getClass().getMethod(neededMethodName,  
    65.                          neededMethod.getParameterTypes());  
    66.                  // 得到源Bean中的属性的get方法  
    67.                  getMethod = srcRow.getClass().getMethod(convertName(setMethod.getName()), null);  
    68.                  Object item = getMethod.invoke(srcRow, null); // 执行源 Bean的get方法,得到返回值  
    69.                  neededMethod.invoke(desRow, item); // 执行目标Bean的set方法  
    70.              }  
    71.          } catch (Exception e) {  
    72.              e.printStackTrace();  
    73.          }  
    74.      }  
    75.    
    76.      /**
    77.       * 只将VO的属性的set方法放入neededMethodsList中以便处理
    78.       * 因为publicMethods和exceptedSuperMethods中可能有很多不需要方法
    79.       *  
    80.       * @param publicMethods
    81.       * @param exceptedSuperMethods
    82.       * @param neededMethodsList
    83.       */  
    84.      private void fetchNeededMethods(Method[] publicMethods,  
    85.              Method[] exceptedSuperMethods, List
    86.         
    87.           neededMethodsList) {  
    88.          for (Method publicMethod : publicMethods) { // 遍历publicMethods  
    89.              String publicMethodName = publicMethod.getName(); // publicMethods中的每一个方法名  
    90.              if (publicMethodName.startsWith("set")) { // 只处理以“set”开头的方法  
    91.                  for (Method exceptedSuperMethod : exceptedSuperMethods) { // 遍历methodsExceptSuper  
    92.                      String exceptedSuperMethodName = exceptedSuperMethod.getName();  
    93.                      // 如果publicMethodName与exceptedSuperMethodName相等  
    94.                      if (publicMethodName.equals(exceptedSuperMethodName))
    95.                          neededMethodsList.add(publicMethod); // 将符合条件的方法放入此List中  
    96.                  }  
    97.              }  
    98.          }  
    99.      }  
    100.    
    101.      /**
    102.       * 过滤needlessColumn_List中的条目
    103.       *  
    104.       * @param methodName
    105.       *            desRow的Class中以“set”开头的方法
    106.       * @return 如果methodName存在于此List中 则返回false
    107.       */  
    108.      private boolean checkNeedlessColumn(String methodName) {  
    109.          ListIterator
    110.          
    111.            it = needlessColumn_List.listIterator();  
    112.          while (it.hasNext()) {  
    113.              String needlessColumn = it.next();  
    114.              String uuidCName = "set" + needlessColumn;  
    115.              if (uuidCName.equals(methodName)) {  
    116.                  needlessColumn_List.remove(needlessColumn);  
    117.                  return false;  
    118.              }  
    119.          }  
    120.          return true;  
    121.      }  
    122.    
    123.      /**
    124.       * 将setXxx方法名称转换成getXxx方法名称
    125.       *  
    126.       * @param methodName
    127.       *            set开头的方法名称
    128.       * @return 对应的get方法
    129.       */  
    130.      private String convertName(String methodName) {  
    131.          if (methodName == null) {  
    132.              return null;  
    133.          }  
    134.          String s = methodName.substring(3, methodName.length());  
    135.          return "get" + s;  
    136.      }  
    137. }
    138.    测试代码:   public class Test {  
    139.    
    140.      public static void main(String[] args) {  
    141.          Bean src = new Bean();  
    142.          src.setName("DOTA");  
    143.    
    144.          Bean des = new Bean();  
    145.          System.out.println("复制前的Name = " + des.getName());  
    146.          CloneUtil cu = new CloneUtil(des, src, null);  
    147.          cu.deepClone();  
    148.          System.out.println("复制后的Name = " + des.getName());  
    149.      }  
    150. }  
    151.    
    152. class Bean {  
    153.      private String name;  
    154.    
    155.      public void setName(String name) {  
    156.          this.name = name;  
    157.      }  
    158.    
    159.      public String getName() {  
    160.          return name;  
    161.      }  
    162. }
    163.          
    164. 运行:
    165.          
    166. C:java>java   Test
    167.          
    168. 复制前的Name = null
    169.          
    170. 复制后的Name = DOTA
    171.          
    172.         
    173.       
    174.       
    175.      
    176.    
    复制代码


       
         
         
          
          

            
          

            
          
         
       

      


    源码下载:http://file.javaxxz.com/2014/10/28/235631953.zip
    回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    QQ|手机版|Java学习者论坛 ( 声明:本站资料整理自互联网,用于Java学习者交流学习使用,对资料版权不负任何法律责任,若有侵权请及时联系客服屏蔽删除 )

    GMT+8, 2025-2-26 02:27 , Processed in 0.380384 second(s), 34 queries .

    Powered by Discuz! X3.4

    © 2001-2017 Comsenz Inc.

    快速回复 返回顶部 返回列表