Java:通过反射复制父类字段到子类。

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

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

  

 import java.beans.Statement;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Type;
import java.util.List; import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.util.DeserializeBeanInfo;
import com.alibaba.fastjson.util.FieldInfo;
import com.alibaba.fastjson.util.TypeUtils; /**
* Created by fenggqc on 2016/6/29.
*/
public class Copyer { public static <B, S extends B> void Copy(B bo, S so) throws IllegalAccessException { try {
Class bc = bo.getClass();
if (bo == null || so == null) {
return;
} DeserializeBeanInfo deserializeBeanInfo = DeserializeBeanInfo.computeSetters(so.getClass(), (Type) so.getClass());
List<FieldInfo> getters = TypeUtils.computeGetters(bo.getClass(), null); List<FieldInfo> setters = deserializeBeanInfo.getFieldList();
Object v;
FieldInfo getterfield;
FieldInfo setterfidld; for (int j = 0; j < getters.size(); j++) { getterfield=getters.get(j);
for (int i = 0; i < setters.size(); i++) {
setterfidld=setters.get(i);
// System.out.println(setterfidld.getName());
// System.out.println(getterfield.getName()); if (setterfidld.getName().compareTo(getterfield.getName()) == 0) {
v = getterfield.getMethod().invoke(bo);
setterfidld.getMethod().invoke(so,v);
break;
} }
}
} catch (Exception ex) {
System.out.println(ex.toString());
} } }
 public class SubClass extends  BaseClass {

     private Date birthday;

     public Date getBirthday() {
return birthday;
} public void setBirthday(Date birthday) {
this.birthday = birthday;
}
} public class BaseClass { private Integer i;
private Boolean b; private Boolean is;
private Boolean isa; private String whatname; public Boolean getB() {
return b;
} public void setB(Boolean b) {
this.b = b;
} public Integer getI() {
return i;
} public void setI(Integer i) {
this.i = i;
} public Boolean getIs() {
return is;
} public void setIs(Boolean is) {
this.is = is;
} public Boolean getIsa() {
return isa;
} public void setIsa(Boolean isa) {
this.isa = isa;
} public String getWhatname() {
return whatname;
} public void setWhatname(String whatname) {
this.whatname = whatname;
}
}

测试了一下性能:

    @Test
public void Test() throws InvocationTargetException, IllegalAccessException { BaseClass baseClass = new BaseClass();
SubClass subClass=new SubClass(); baseClass.setB(true);
baseClass.setI(1010);
baseClass.setIs(false);
baseClass.setWhatname("fgq"); Integer i=0;
List<Integer> ii=new ArrayList<Integer>();
ii.add(1);
ii.add(10);
ii.add(100);
ii.add(1000);
ii.add(10000); StopWatch stopWatch=new StopWatch(); stopWatch.reset();;
stopWatch.start();
Copyer.Copy(baseClass, subClass);
stopWatch.stop();
System.out.println("cache the reflection:"+String.valueOf(stopWatch.getTime())); for (int j = 0; j < ii.size(); j++) {
i=0;
stopWatch.reset();;
stopWatch.start();
while ( i<ii.get(j)) { Copyer.Copy(baseClass, subClass);
subClass.setBirthday(new Date());
i+=1;
}
stopWatch.stop();
System.out.println(String.format("%s %s",ii.get(j),String.valueOf(stopWatch.getTime())));
} System.out.println(JSON.toJSONString(subClass,SerializerFeature.PrettyFormat,SerializerFeature.UseISO8601DateFormat)); }

性能结果为:

cache the reflection:120
1 1
10 7
100 53
1000 139
10000 634

感觉还行。呵呵

上一篇:HTK学习2:工具使用


下一篇:使用Visual Studio Code创建第一个ASP.NET Core应用程序