package com.xisc.pm.infra.util;

import lombok.extern.slf4j.Slf4j;
import org.springframework.cglib.beans.BeanCopier;

/**
 * 组板工具类
 * @author jun.zeng
 *
 */
@Slf4j
public class BeanUtils {
    
    /**
     * @param source
     * @param target
     * @return
     */
    public static <T,V> T copy(V source, T target) {
        if (source == null) {
            return null;
        }
        BeanCopier.create(source.getClass(), target.getClass(), false).copy(source, target, null);
        return target;
    }
    
    /**
     * 
     * @param source
     * @param targetCalss
     * @return
     */
    public static <T,V> T copy(V source,Class<T> targetCalss) {
        if (source == null) {
            return null;
        }
        T target = null;
        try {
            target = targetCalss.newInstance();
        } catch (InstantiationException | IllegalAccessException e) {
            log.error(e.getMessage(), e);
        }
        if(target == null) {
            return null;
        }
        BeanCopier.create(source.getClass(), target.getClass(), false).copy(source, target, null);
        return target;
    }
    
}
