Java中把一个对象的值复制给另外一个对象引发的思考

  Spring生态在Java项目中被广泛应用,从架构到技术应用再到常用的基本功能,Spring给我们的开发带来了很大的便利。今天翻到项目中导出报表功能的时候,发现经常复制对象的方法:

  BeanUtils.copyProperties;

  把源对象的属性值赋值给目标对象,Spring和Apache和其他的一些框架都给我们提供了对象属性的拷贝方法:

  org.springframework.beans.BeanUtils.copyProperties(Object source, Object target)

  org.apache.commons.beanutils.BeanUtils.copyProperties(Object dest, Object orig)

  使用的时候注意这两个方法的参数顺序,源和目标是相反的。这两个方法底层都用到了反射,心血来潮之下我自己造了一个对象拷贝的轮子,代码如下:

import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.HashMap; public class ObjConvertor { private static HashMap<String,HashMap<String,Method>> mapSetMethods=new HashMap<String,HashMap<String,Method>>(); private static HashMap<String,HashMap<String,Method>> mapGetMethods=new HashMap<String,HashMap<String,Method>>(); static { } public static boolean Convert(Object src,Object dst) { HashMap<String,Method> mapSrc=new HashMap<String,Method>(); HashMap<String,Method> mapDst=new HashMap<String,Method>(); if(mapGetMethods.containsKey(src.getClass().getTypeName())) mapSrc=mapGetMethods.get(src.getClass().getTypeName()); else { Method srcMethods[]=src.getClass().getMethods(); mapGetMethods.put(src.getClass().getTypeName(),mapSrc); for(Method method:srcMethods) { String name=method.getName(); if(name.startsWith("get")) { String prop=name.substring(3).toLowerCase(); mapSrc.put(prop, method); } } } if(mapSetMethods.containsKey(dst.getClass().getTypeName())) mapDst=mapSetMethods.get(dst.getClass().getTypeName()); else { Method dstMethods[]=dst.getClass().getMethods(); mapSetMethods.put(dst.getClass().getTypeName(),mapDst); for(Method method:dstMethods) { String name=method.getName(); if(name.startsWith("set")) { String prop=name.substring(3).toLowerCase(); mapDst.put(prop, method); } } } for(String prop:mapSrc.keySet()) { if(mapDst.containsKey(prop)){ Method setMethod=mapDst.get(prop); Method getMethod=mapSrc.get(prop); try { Object data=getMethod.invoke(src); if(data==null) continue; if(data instanceof java.sql.Time) { if(setMethod.getParameterTypes()[0]==String.class) data=data.toString(); } else if(data instanceof java.sql.Date) { if(setMethod.getParameterTypes()[0]==String.class) data=new SimpleDateFormat("yyyy-MM-dd").format(data); } else if(data instanceof java.util.Date) { if(setMethod.getParameterTypes()[0]==String.class) data=new SimpleDateFormat("yyyy-MM-dd").format((java.util.Date)data); } else if(data instanceof java.sql.Timestamp) { if(setMethod.getParameterTypes()[0]==String.class) data=data.toString(); } if(setMethod.getParameterTypes()[0]==java.sql.Time.class) { if(data instanceof String) data=java.sql.Time.valueOf((String)data); } else if(setMethod.getParameterTypes()[0]==java.sql.Date.class) { if(data instanceof String) data=java.sql.Date.valueOf((String)data); } else if(setMethod.getParameterTypes()[0]==java.util.Date.class) { if(data instanceof String) { data=(new SimpleDateFormat("yyyy-MM-dd")).parse((String)data); } } else if(setMethod.getParameterTypes()[0]==java.sql.Timestamp.class) { if(data instanceof String) data=java.sql.Timestamp.valueOf((String)data); } if(data==null) continue; setMethod.invoke(dst, data); } catch (IllegalAccessException e) { // TODO Auto-generated catch block e.printStackTrace(); return false; } catch (IllegalArgumentException e) { // TODO Auto-generated catch block e.printStackTrace(); return false; } catch (InvocationTargetException e) { // TODO Auto-generated catch block e.printStackTrace(); return false; } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); return false; } } } return true; } }

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/zyyszp.html