java对象拷贝(java反射)

  1. package cn.com.reflection;   
  2.   
  3. import java.lang.reflect.Field;   
  4. import java.lang.reflect.InvocationTargetException;   
  5. import java.lang.reflect.Method;   
  6.   
  7. public class ReflectTester {   
  8.   
  9.     /**  
  10.      * 在这个类里面存在有copy()方法,根据指定的方法的参数去 构造一个新的对象的拷贝  
  11.      * 并将他返回  
  12.      * @throws NoSuchMethodException   
  13.      * @throws InvocationTargetException   
  14.      * @throws IllegalAccessException   
  15.      * @throws InstantiationException   
  16.      * @throws SecurityException   
  17.      * @throws IllegalArgumentException   
  18.      */  
  19.     public Object copy(Object obj) throws IllegalArgumentException, SecurityException, InstantiationException, IllegalAccessException, InvocationTargetException, NoSuchMethodException{   
  20.            
  21.         //获得对象的类型   
  22.         Class classType=obj.getClass();   
  23.         System.out.println("该对象的类型是:"+classType.toString());   
  24.            
  25.         //通过默认构造方法去创建一个新的对象,getConstructor的视其参数决定调用哪个构造方法   
  26.         Object objectCopy=classType.getConstructor(new Class[]{}).newInstance(new Object[]{});   
  27.            
  28.         //获得对象的所有属性   
  29.         Field[] fields=classType.getDeclaredFields();   
  30.            
  31.         for(int i=0;i
  32.             //获取数组中对应的属性   
  33.             Field field=fields[i];   
  34.                
  35.             String fieldName=field.getName();   
  36.             String stringLetter=fieldName.substring(01).toUpperCase();   
  37.                
  38.             //获得相应属性的getXXX和setXXX方法名称   
  39.             String getName="get"+stringLetter+fieldName.substring(1);   
  40.             String setName="set"+stringLetter+fieldName.substring(1);   
  41.                
  42.             //获取相应的方法   
  43.             Method getMethod=classType.getMethod(getName, new Class[]{});   
  44.             Method setMethod=classType.getMethod(setName, new Class[]{field.getType()});   
  45.                
  46.             //调用源对象的getXXX()方法   
  47.             Object value=getMethod.invoke(obj, new Object[]{});   
  48.             System.out.println(fieldName+" :"+value);   
  49.                
  50.             //调用拷贝对象的setXXX()方法   
  51.             setMethod.invoke(objectCopy,new Object[]{value});   
  52.                
  53.                
  54.         }   
  55.            
  56.         return objectCopy;   
  57.            
  58.     }   
  59.        
  60.        
  61.     public static void main(String[] args) throws IllegalArgumentException, SecurityException, InstantiationException, IllegalAccessException, InvocationTargetException, NoSuchMethodException {   
  62.         Customer customer=new Customer();   
  63.         customer.setName("hejianjie");   
  64.         customer.setId(new Long(1234));   
  65.         customer.setAge(19);   
  66.            
  67.         Customer customer2=null;   
  68.         customer2=(Customer)new ReflectTester().copy(customer);   
  69.         System.out.println(customer.getName()+" "+customer2.getAge()+" "+customer2.getId());   
  70.            
  71.         System.out.println(customer);   
  72.         System.out.println(customer2);   
  73.            
  74.   
  75.     }   
  76.   
  77. }   
  78.   
  79.   
  80. class Customer{   
  81.        
  82.     private Long id;   
  83.        
  84.     private String name;   
  85.        
  86.     private int age;   
  87.        
  88.        
  89.     public Customer(){   
  90.            
  91.     }   
  92.   
  93.     public int getAge() {   
  94.         return age;   
  95.     }   
  96.   
  97.   
  98.     public void setAge(int age) {   
  99.         this.age = age;   
  100.     }   
  101.   
  102.   
  103.     public Long getId() {   
  104.         return id;   
  105.     }   
  106.   
  107.   
  108.     public void setId(Long id) {   
  109.         this.id = id;   
  110.     }   
  111.   
  112.   
  113.     public String getName() {   
  114.         return name;   
  115.     }   
  116.   
  117.   
  118.     public void setName(String name) {   
  119.         this.name = name;   
  120.     }   
  121.        
  122. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值