使用反射复制一个JavaBean的对象

先看代码吧:

[java]  view plain copy
  1. package com.java.study;  
  2.   
  3. import java.io.Serializable;  
  4. import java.lang.reflect.Field;  
  5. import java.lang.reflect.InvocationTargetException;  
  6. import java.lang.reflect.Method;  
  7.   
  8. public class ReflectTester {  
  9.     public Object copy(Object object) throws IllegalArgumentException, SecurityException, InstantiationException, IllegalAccessException, InvocationTargetException, NoSuchMethodException {  
  10.         Class classType = object.getClass();//获得对象的类型  
  11.         System.out.println("Class:" + classType.getName());  
  12.           
  13.         //通过默认的构造函数创建一个新的对象  
  14.         Object objectCopy = classType.getConstructor(new Class[] {}).  
  15.         newInstance(new Object[] {});  
  16.           
  17.         //获得对象的所有属性  
  18.         Field fields[] = classType.getDeclaredFields();  
  19.           
  20.         for(int i = 0; i < fields.length; i++) {  
  21.             Field field = fields[i];  
  22.               
  23.             String fieldName = field.getName();  
  24.             String firstLetter = fieldName.substring(0,1).toUpperCase();  
  25.             //获得和属性对应的getXXX()方法的名字  
  26.             String getMethodName = "get" + firstLetter + fieldName.substring(1);  
  27.             //获得和属性对应的setXXX()方法的名字  
  28.             String setMethodName = "set" + firstLetter + fieldName.substring(1);  
  29.               
  30.             //获得和属性对应的getXXX()方法  
  31.             Method getMethod = classType.getMethod(getMethodName, new Class[]{});  
  32.             //获得和属性对应的setXXX()方法  
  33.             Method setMethod = classType.getMethod(setMethodName, new Class[]{field.getType()});  
  34.               
  35.             //调用原对象的getXXX()方法  
  36.             Object value = getMethod.invoke(object, new Object[]{});  
  37.             System.out.println(fieldName + ":" + value);  
  38.             //调用复制对象的setXXX()方法  
  39.             setMethod.invoke(objectCopy, new Object[]{value});  
  40.         }  
  41.         return objectCopy;  
  42.           
  43.     }  
  44.       
  45.     public static void main(String[] args) throws IllegalArgumentException, SecurityException, InstantiationException, IllegalAccessException, InvocationTargetException, NoSuchMethodException {  
  46.         Customer1 customer = new Customer1("tom",21);  
  47.         customer.setId(new Long(1));  
  48.           
  49.         Customer1 customerCopy = (Customer1) new ReflectTester().copy(customer);  
  50.         System.out.println("Copy information:" + customerCopy.getName()+ ""  
  51.                 +customerCopy.getAge());  
  52.     }  
  53.   
  54. }  
  55.   
  56. class Customer1 implements Serializable {  
  57.       
  58.     private Long id;  
  59.     private String name;  
  60.     private int age;  
  61.       
  62.     public Customer1() {}  
  63.     public Customer1(String name, int age){  
  64.         this.name = name;  
  65.         this.age = age;  
  66.     }  
  67.       
  68.     public Long getId() {  
  69.         return id;  
  70.     }  
  71.     public void setId(Long id) {  
  72.         this.id = id;  
  73.     }  
  74.     public String getName() {  
  75.         return name;  
  76.     }  
  77.   
  78.     public void setName(String name) {  
  79.         this.name = name;  
  80.     }  
  81.   
  82.     public int getAge() {  
  83.         return age;  
  84.     }  
  85.   
  86.     public void setAge(int age) {  
  87.         this.age = age;  
  88.     }  
  89. }  


代码中有一个Customer1的JavaBean,通过反射复制了一个Customer1类的对象,为customerCopy,程序中的new Class[] { }是一种快捷方式,获得方法的引用。[]中用来放参数,{}里面可以放置方法的返回类型。new Class[] { }相当于new class("构造函数参数").newinstance.

//调用原对象的getXXX()方法
      Object value = getMethod.invoke(object, new Object[]{});
      System.out.println(fieldName + ":" + value);


用来获得特定对象的特定方法。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值