package com.cslg.ppa.common.utils;


import com.cslg.ppa.common.core.log.LogHelper;
import com.cslg.ppa.common.exception.BusinessException;
import com.cslg.ppa.common.exception.ExceptionEnum;

public  class BeanUtil {


	/**
	* @Title: copy
	* @Description: 类属性复制
	* @param @param source
	* @param @param target    参数
	* @return void    返回类型
	* @throws
	*/
	public static void copy(Object source,Object target){
		org.springframework.beans.BeanUtils.copyProperties(source, target);
	}

	/**
	 * @name: 克隆source为新对象
	 * @author Peach
	 * @date:2018/9/4
	 */
	public static <T> T cloneNewObject(Object source,Class<T> clazz){
		T t = null;
		try {
			t = clazz.newInstance();
		} catch (Exception e) {
			LogHelper.log(BeanUtil.class, "泛型实例化异常：" + clazz.getName(), e);
			throw new BusinessException(ExceptionEnum.INIT_GENERICITY_BEAN_ERROR);
		}
		BeanUtil.copy(source,t);
		return t;
	}


	/**
	 * @Title: newTclass
	 * @Description: 实例化泛型类
	 * @param @param
	 *            clazz
	 * @param @return
	 * @param @throws
	 *            InstantiationException
	 * @param @throws
	 *            IllegalAccessException 参数
	 * @return T 返回类型
	 * @throws @author
	 *             Orange
	 * @date 2018年1月16日
	 */
	public static <T> T newTclass(Class<T> clazz) {
		T a = null;
		try {
			a = clazz.newInstance();
		} catch (Exception e) {
			LogHelper.log(BeanUtil.class, "泛型实例化异常：" + clazz.getName(), e);
			throw new BusinessException(ExceptionEnum.INIT_GENERICITY_BEAN_ERROR);
		}
		return a;
	}

	public static Object[] getEntities(Object target, Class<?>[] objs) {
		Object[] entities = new Object[objs.length];
		int count = 0;
		for (Class<?> o : objs) {
			Object entity = null;
			try {
				entity = o.newInstance();
			} catch (Exception e) {
				continue;
			}
			if (target != null)
				copy(target, entity);
			entities[count ++] = entity;
		}
		return entities;
	}
}
