You can not select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
			
				
					49 lines
				
				1.5 KiB
			
		
		
			
		
	
	
					49 lines
				
				1.5 KiB
			| 
								 
											3 years ago
										 
									 | 
							
								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> T fatherToChild(Object father, Class<T> 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;
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    
							 | 
						||
| 
								 | 
							
								}
							 |