package com.ynxbd.common.helper.common; import org.apache.commons.beanutils.BeanUtils; import org.apache.commons.beanutils.BeanUtilsBean; import org.apache.commons.beanutils.ConvertUtils; import org.apache.commons.beanutils.converters.DateConverter; import java.lang.reflect.Field; public class CopyHelper { static { // 解决BigDecimal类型转化报错 BeanUtilsBean.getInstance().getConvertUtils().register(false, false, 0); // 解决Date类型转化报错 ConvertUtils.register(new DateConverter(null), java.util.Date.class); } /* * 将父类所有的属性COPY到子类中。 * 类定义中child一定要extends father; * 而且child和father一定为严格javabean写法,属性为deleteDate,方法为getDeleteDate */ public static T fatherToChild(Object father, Class childClazz) { if (father == null) { return null; } Class fClazz = father.getClass(); if (!(childClazz.getSuperclass() == fClazz)) { return null; } T bean = null; try { bean = childClazz.newInstance(); for (Field f : fClazz.getDeclaredFields()) { f.setAccessible(true); BeanUtils.setProperty(bean, f.getName(), f.get(father)); } } catch (Exception e) { e.printStackTrace(); } return bean; } }