zoukankan      html  css  js  c++  java
  • JavaBean属性拷贝

    package org.leno.demo;<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

     

    import java.lang.reflect.Field;

    import java.lang.reflect.InvocationTargetException;

    import java.lang.reflect.Method;

     

    /**

     * @author leno

     *    做一个方法,可以将一个JavaBean风格对象的属性值拷贝到另一个对象的同名属性中

     *    (如果不存在同名属性的就不拷贝)

     */

    public class BeanUtils {

     

           @SuppressWarnings("unchecked")

           public static void copyProperties(Object target,Object source) throws Exception{

                  /*分别获得源对象和目标对象的Class类型对象,Class对象是整个反射机制的源头和灵魂!

                    Class对象是在类加载的时候产生,保存着类的相关属性,构造器,方法等信息

                  */

                  Class sourceClz = source.getClass();

                  Class targetClz = target.getClass();

                  //得到Class对象所表征的类的所有属性(包括私有属性)

                  Field[] fields = sourceClz.getDeclaredFields();

                  for (int i = 0; i < fields.length; i++) {

                         String fieldName = fields[i].getName();

                         Field targetField  = null;

                         try {

                                //得到targetClz对象所表征的类的名为fieldName的属性,不存在就进入下次循环

                                 targetField = targetClz.getDeclaredField(fieldName);

                         } catch (SecurityException e) {

                                e.printStackTrace();

                                break;

                         } catch (NoSuchFieldException e) {

                                continue;

                         }

                         //判断sourceClz字段类型和targetClz同名字段类型是否相同

                         if(fields[i].getType()==targetField.getType()){

                                //由属性名字得到对应getset方法的名字

                                String getMethodName = "get"+fieldName.substring(0,1).toUpperCase()+fieldName.substring(1);

                                String setMethodName = "set"+fieldName.substring(0,1).toUpperCase()+fieldName.substring(1);

                                //由方法的名字得到getset方法的Method对象

                                Method getMethod;

                                try {

                                       getMethod = sourceClz.getDeclaredMethod(getMethodName, new Class[]{});

                                       Method setMethod = targetClz.getDeclaredMethod(setMethodName, fields[i].getType());

                                       //调用source对象的getMethod方法

                                       Object result = getMethod.invoke(source, new Object[]{});

                                       //调用target对象的setMethod方法

                                       setMethod.invoke(target, result);

                                } catch (SecurityException e) {

                                       // TODO Auto-generated catch block

                                       e.printStackTrace();

                                } catch (NoSuchMethodException e) {

                                       // TODO Auto-generated catch block

                                       e.printStackTrace();

                                } catch (IllegalArgumentException e) {

                                       // TODO Auto-generated catch block

                                       e.printStackTrace();

                                } catch (IllegalAccessException e) {

                                       // TODO Auto-generated catch block

                                       e.printStackTrace();

                                } catch (InvocationTargetException e) {

                                       // TODO Auto-generated catch block

                                       e.printStackTrace();

                                }

                         }else{

                                throw new Exception("同名属性类型不匹配!");

                         }

                  }

           }

          

    }

     

  • 相关阅读:
    20155328 《网络攻防》 实验一:PC平台逆向破解(5)M
    20155328 《信息安全系统设计基础》 课程总结
    构建之法
    20155327 2017-2018-2《Java程序设计》课程总结
    20155327 实验五 网络编程与安全
    20155327 网络对抗 实验
    20155327 Exp9 Web安全基础
    20155327 EXP8 Web基础
    20155327 实验四 Android程序设计
    20155327 李百乾 Exp7 网络欺诈防范
  • 原文地址:https://www.cnblogs.com/CharmingDang/p/9663719.html
Copyright © 2011-2022 走看看