copy工具类--实现对象,对象内子对象,对象内集合等的复制
这里使用net.sf.dozer下的
DozerBeanMapper
实现对象,对象内子对象,以及对象内集合等的copy
需要引入net.sf.dozer的依赖 仓库链接net.sf.dozer:dozer:5.5.1
<dependency> <groupId>net.sf.dozer</groupId> <artifactId>dozer</artifactId> <version>5.5.1</version> </dependency>
工具类实现主要有两个方法:一个是from方法,将原对象加载;一个是to方法,要copy到的对象;如果要对集合的copy,则用toList方法。
package xxx; import org.dozer.DozerBeanMapper; import org.dozer.Mapper; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Locale; import java.util.Map; import java.util.Set; public class BeansCopy { private static Logger log = LoggerFactory.getLogger(BeansCopy.class); private static Mapper mapper = new DozerBeanMapper(); private Object fromObj; private List<?> fromList; private BeansCopy() { } public static BeansCopy getInstance() { return new BeansCopy(); } private static Mapper getMapperInstance() { return mapper; } public BeansCopy from(Object fromObj) { this.fromObj = fromObj; return this; } public BeansCopy from(List<?> fromList) { this.fromList = fromList; return this; } public <T> T to(Class<T> toClass) { return this.fromObj == null ? null : getMapperInstance().map(this.fromObj, toClass); } public <T> List<T> toList(Class<T> toClass) { if (this.fromList == null) { return null; } else { List<T> listTo = new ArrayList(); Iterator var3 = this.fromList.iterator(); while(var3.hasNext()) { Object myFromObj = var3.next(); T toObj = getMapperInstance().map(myFromObj, toClass); listTo.add(toObj); } return listTo; } } public static void copy(Object from, Object to) { Method[] methods = from.getClass().getMethods(); Map<String, Object> fromObj = new HashMap(); Method[] var4 = methods; int var5 = methods.length; for(int var6 = 0; var6 < var5; ++var6) { Method method = var4[var6]; String m = method.getName(); if (m.startsWith("get")) { try { Object result = method.invoke(from); if (result != null) { String field = m.substring(3).toLowerCase(Locale.getDefault()); fromObj.put(field, result); } } catch (IllegalArgumentException | InvocationTargetException | IllegalAccessException var11) { log.warn("{}", var11); } } } Set<String> fields = fromObj.keySet(); Iterator var13 = fields.iterator(); while(var13.hasNext()) { String str = (String)var13.next(); invoke(to, str, fromObj.get(str)); } } public static Object invoke(Object o, String field, Object value) { Method[] methods = o.getClass().getMethods(); Method[] var4 = methods; int var5 = methods.length; for(int var6 = 0; var6 < var5; ++var6) { Method method = var4[var6]; String m = method.getName(); if (m.startsWith("set")) { String s = m.substring(3); if (s.equalsIgnoreCase(field)) { try { method.invoke(o, value); } catch (IllegalArgumentException | InvocationTargetException | IllegalAccessException var11) { log.warn("{}", var11); } break; } } } return o; } }
调用方式:A是PO,AVo是VO,两个类属性一样。
//单个对象的复制
A a = new A();
AVo aVo = BeansCopy.getInstance().from(a).to(AVo.class);
log.info(JSON.toJSONString(aVo));
//集合的复制
List<A> As = Collections.singletonList(a);
List<AVo> AVoList = BeansCopy.getInstance().from(As).toList(AVo.class);
log.info(JSON.toJSONString(AVoList));
注意:日志是sl4j
很多东西更多的是写给自己的,下一次想用的时候能方便直接用。