package *************import com.alibaba.fastjson.JSON;import com.alibaba.fastjson.JSONObject;import com.alibaba.fastjson.serializer.SerializerFeature;import com.github.pagehelper.PageInfo;import com.google.common.collect.Lists;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.beans.BeanUtils;import org.springframework.util.CollectionUtils;import java.io.ByteArrayInputStream;import java.io.ByteArrayOutputStream;import java.io.ObjectInputStream;import java.io.ObjectOutputStream;import java.util.Collection;import java.util.List;import java.util.Objects;import java.util.function.Function;import java.util.stream.Collectors;/** * 对象之间转换工具类 */public class BeanUtil { private static Logger log = LoggerFactory.getLogger(BeanUtil.class); public static T deepCopy(T obj) { if (Objects.isNull(obj)) return null; try { ByteArrayOutputStream bos = new ByteArrayOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(bos); oos.writeObject(obj); ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(bos.toByteArray())); return (T) ois.readObject(); } catch (Exception e) { log.error("对象深拷贝异常", e); throw new ServiceException("对象深拷贝异常"); } } public static R deepCopyWithJSON(T obj, Class rClass) { if (Objects.isNull(obj)) return null; //禁用 fastJSON 循环引用检测特性 String jsonObject = JSONObject.toJSONString(obj, SerializerFeature.DisableCircularReferenceDetect); return JSON.parseObject(jsonObject, rClass); } /** * 对象复制 * 支持 深度复制 * 不支持静态内部类的复制 * * @param source * @param target * @param * @param * @return */ @Deprecated public static R copy(F source, Class target) { try { R r = target.newInstance(); BeanUtils.copyProperties(r, source); return r; } catch (Exception e) { throw new ServiceException(e); } } /** * 对象复制,采用spring的方法,速度更快 * * @param source * @param targetClass * @param * @param * @return */ public static R copySpring(F source, Class targetClass) { try { if (source == null) return null; R r = targetClass.newInstance(); BeanUtils.copyProperties(source, r); return r; } catch (Exception e) { throw new ServiceException(e); } } /** * 列表复制 * * @param col 原始列表 * @param c 转化对象 * @param * @param * @return 转化对象列表 */ public static List copyList(Collection col, Class c) { return CollectionUtils.isEmpty(col) ? Lists.newArrayList() : col.stream().map(s -> copySpring(s, c)).collect(Collectors.toList()); } /** * 忽略部分属性的拷贝 * * @param source * @param target * @param ignoreProperties * @param * @param * @return */ public static R copyIgnore(F source, Class target, String... ignoreProperties) { try { R r = target.newInstance(); BeanUtils.copyProperties(source, r, ignoreProperties); return r; } catch (Exception e) { throw new ServiceException(e); } } /** * 消除空判断的三段表达式 * * @param obj 箱要取的值 * @param defaultReturn 值为空时的默认值 * @param * @return */ public static T getOrDefault(T obj, T defaultReturn) { return getDefaultWithEqual(obj, null, defaultReturn); } /** * 对象获取默认属性方法 * * 代替类型:bean == null || bean.getId() == null ? 0 : bean.getId(); * * @param obj 对象 * @param getFun 属性的get方法 * @param defaultReturn 默认值 * @param * @param * @return */ public static R getOrDefault(T obj, Function getFun, R defaultReturn) { if (obj == null) { return defaultReturn; } if (getFun.apply(obj) == null) { return defaultReturn; } return getFun.apply(obj); } /** * 如果需求值和期望值相等时,返回默认值 * * @param obj 需求值 * @param equalVal 期望值 * @param defaultReturn 默认值 * @param * @return */ public static T getDefaultWithEqual(T obj, T equalVal, T defaultReturn) { if (equalVal == null && obj == null) { return defaultReturn; } if (Objects.equals(equalVal, obj)) { return defaultReturn; } return obj; } public static PageInfo copyPage(PageInfo source, Class targetClass) { try { if (source == null) { return null; } else { List rList = BeanUtil.copyList(source.getList(), targetClass); PageInfo r = new PageInfo<>(rList); r.setPageNum(source.getPageNum()); r.setPageSize(source.getPageSize()); r.setTotal(source.getTotal()); return r; } } catch (Exception var3) { throw new ServiceException(var3); } } public static PageResult copyPage(PageResult source, Class targetClass) { try { if (source == null) { return null; } else { List rList = BeanUtil.copyList(source.getResult(), targetClass); return PageResult.create(rList, source.getTotal()); } } catch (Exception e) { throw new ServiceException(e); } } }
使用
public static void main(String[] args) { DftTask task = new DftTask(); task.setMonthBuy(BigDecimal.ONE); DftTask task2 = copySpring(task,DftTask.class); System.out.println(JSON.toJSON(task)); System.out.println(JSON.toJSON(task2)); }
留言与评论(共有 0 条评论) “” |