package com.diagbot.util; import org.springframework.beans.BeanUtils; import java.util.ArrayList; import java.util.Collections; import java.util.List; /** * @Description: 对象转换工具类 * @author: gaodm * @time: 2018/12/14 14:21 */ public class BeanUtil { /** * 把一个对象的属性值复制给另外一个对象的属性值 * * @param source 源对象,被转换的对象 * @param target 目标对象,即转换后对象 */ public static void copyProperties(Object source, Object target) { BeanUtils.copyProperties(source, target); } /** * 复制集合 * * @param * @param source 转换前的列表 * @param destinationClass 转换后列表类 * @return 转换后列表 */ public static List listCopyTo(List source, Class destinationClass) { try { if (source.size() == 0) { return Collections.emptyList(); } List res = new ArrayList(source.size()); for (Object o : source) { E e = destinationClass.newInstance(); BeanUtils.copyProperties(o, e); res.add(e); } return res; } catch (IllegalAccessException ex) { throw new RuntimeException(ex); } catch (InstantiationException ex) { throw new RuntimeException(ex); } } }