克隆一个对象,通过反射
作者:互联网
//工具类,把from对象赋予T这个对象
pubic static <T> T copyTo(Class<T> toClass,Object from){
try{
T to =null;
to = toClass.newInstance();
Field[] ff = getFields(from.getClass());
for(Field f:ff){
if(Modifier.ifFinal(f.getModefies())){
continue;
}
try{
Field toField = getField(toClass,f.getName());
f.setAccessible(true);
Object fv = f.get(from);
toField.setAccessible(true);
toField.set(to,fv);
}catch(Exception e){
}
}
return to;
}catch(Exception e){
}
}
//调用
RenewQuery cloneQuery = BeanUtil.copyTo(RenewQuery.class,query);
标签:反射,Exception,克隆,toClass,对象,Object,Field,copyTo,toField 来源: https://blog.csdn.net/weixin_41126842/article/details/100553007