博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Java:通过反射复制父类字段到子类。
阅读量:4615 次
发布时间:2019-06-09

本文共 4367 字,大约阅读时间需要 14 分钟。

有些时候需要建立子类继承于父类,尤其是java里面很多类是用mybatis generator生成的。通过父类构造子类,好像很麻烦,要逐个字段进行赋值(反正我没有找到其他好办法)。

想到用反射复制的方式来实现。通过研究,做到了。主要是用了fastjson里面的东西。估计已经有其他类库实现了这个功能,可惜我不知道,只能自己搞。

  

 

1 import java.beans.Statement; 2 import java.lang.reflect.Field; 3 import java.lang.reflect.Method; 4 import java.lang.reflect.Type; 5 import java.util.List; 6  7 import com.alibaba.fastjson.JSON; 8 import com.alibaba.fastjson.util.DeserializeBeanInfo; 9 import com.alibaba.fastjson.util.FieldInfo;10 import com.alibaba.fastjson.util.TypeUtils;11 12 /**13  * Created by fenggqc on 2016/6/29.14  */15 public class Copyer {16 17 18     public static 
void Copy(B bo, S so) throws IllegalAccessException {19 20 try {21 Class bc = bo.getClass();22 if (bo == null || so == null) {23 return;24 }25 26 27 DeserializeBeanInfo deserializeBeanInfo = DeserializeBeanInfo.computeSetters(so.getClass(), (Type) so.getClass());28 List
getters = TypeUtils.computeGetters(bo.getClass(), null);29 30 List
setters = deserializeBeanInfo.getFieldList();31 Object v;32 FieldInfo getterfield;33 FieldInfo setterfidld;34 35 for (int j = 0; j < getters.size(); j++) {36 37 getterfield=getters.get(j);38 for (int i = 0; i < setters.size(); i++) {39 setterfidld=setters.get(i);40 // System.out.println(setterfidld.getName());41 // System.out.println(getterfield.getName());42 43 if (setterfidld.getName().compareTo(getterfield.getName()) == 0) {44 v = getterfield.getMethod().invoke(bo);45 setterfidld.getMethod().invoke(so,v);46 break;47 }48 49 }50 }51 } catch (Exception ex) {52 System.out.println(ex.toString());53 }54 55 56 }57 58 59 }
View Code

 

1 public class SubClass extends  BaseClass { 2  3     private Date birthday; 4  5  6     public Date getBirthday() { 7         return birthday; 8     } 9 10     public void setBirthday(Date birthday) {11         this.birthday = birthday;12     }13 }14 15 16 public class BaseClass {17 18     private  Integer i;19     private Boolean b;20 21     private  Boolean is;22     private  Boolean isa;23 24     private  String whatname;25 26 27     public Boolean getB() {28         return b;29     }30 31     public void setB(Boolean b) {32         this.b = b;33     }34 35     public Integer getI() {36         return i;37     }38 39     public void setI(Integer i) {40         this.i = i;41     }42 43     public Boolean getIs() {44         return is;45     }46 47     public void setIs(Boolean is) {48         this.is = is;49     }50 51     public Boolean getIsa() {52         return isa;53     }54 55     public void setIsa(Boolean isa) {56         this.isa = isa;57     }58 59     public String getWhatname() {60         return whatname;61     }62 63     public void setWhatname(String whatname) {64         this.whatname = whatname;65     }66 }
View Code

 

 

 

测试了一下性能:

1    @Test 2     public void Test() throws InvocationTargetException, IllegalAccessException { 3  4  5         BaseClass baseClass = new BaseClass(); 6         SubClass subClass=new SubClass(); 7  8         baseClass.setB(true); 9         baseClass.setI(1010);10         baseClass.setIs(false);11         baseClass.setWhatname("fgq");12 13 14         Integer i=0;15         List
ii=new ArrayList
();16 ii.add(1);17 ii.add(10);18 ii.add(100);19 ii.add(1000);20 ii.add(10000);21 22 StopWatch stopWatch=new StopWatch();23 24 stopWatch.reset();;25 stopWatch.start();26 Copyer.Copy(baseClass, subClass);27 stopWatch.stop();28 System.out.println("cache the reflection:"+String.valueOf(stopWatch.getTime()));29 30 31 32 33 for (int j = 0; j < ii.size(); j++) {34 i=0;35 stopWatch.reset();;36 stopWatch.start();37 while ( i
View Code

 

性能结果为:

cache the reflection:120

1 1
10 7
100 53
1000 139
10000 634

感觉还行。呵呵

转载于:https://www.cnblogs.com/fgq841103/p/5849243.html

你可能感兴趣的文章
几种数据库获取系统的时间
查看>>
SIMD数据并行(四)——三种结构的比较
查看>>
R语言之读取本地文件注意事项
查看>>
关于Ubuntu10.04磁盘空间不足的问题
查看>>
AMD、CMD、CommonJs和 ES6对比
查看>>
Windows Phone 7 Belling‘s课堂(九) LINQ to SQL语句
查看>>
Python 学习笔记5
查看>>
python协程函数、递归、匿名函数与内置函数使用、模块与包
查看>>
[ConcurrencyCheck]并发检查
查看>>
NOIP2016普及总结
查看>>
springMVC学习总结(一) --springMVC搭建
查看>>
Flutter中通过https post Json接收Json
查看>>
负载均衡
查看>>
Linux环境下连接Mssql 2008
查看>>
Compiling wxWidgets
查看>>
c语言日历系统的设计与部分实现
查看>>
BZOJ 1770: [Usaco2009 Nov]lights 燈( 高斯消元 )
查看>>
NYOJ 478
查看>>
Mac 配置几个环境变量
查看>>
10.10
查看>>