/**
 *
 */
package cn.citycraft.GsonAgent.api.utils;

import java.lang.reflect.Constructor;

/**
 * Gson代理工具类(来自7老板)
 *
 * @since 2016年1月5日 下午1:33:57
 * @author 曲尘,喵♂呜
 */
@SuppressWarnings({ "unchecked", "rawtypes" })
public class Utils {
	public static <T> T deepCopyObject(final T obj) {
		try {
			return (T) obj.getClass().getDeclaredMethod("deepCopy").invoke(obj);
		} catch (final Exception e) {
			throw new RuntimeException(e);
		}
	}

	public static <T> T newInstance(final Class<T> clzz) {
		try {
			final Constructor<T> constructor = clzz.getDeclaredConstructor();
			constructor.setAccessible(true);
			return constructor.newInstance();
		} catch (final Exception e) {
			throw new RuntimeException(e);
		}
	}

	public static <T> T newInstance(final Class<T> clzz, final Class[] types, final Object[] args) {
		if (args.length == 0 || types.length == 0) {
			return newInstance(clzz);
		}
		try {
			final Constructor<T> constructor = clzz.getDeclaredConstructor(types);
			constructor.setAccessible(true);
			return constructor.newInstance(args);
		} catch (final Exception e) {
			throw new RuntimeException(e);
		}
	}

	public static <T> T newInstance(final Class<T> clzz, final Object... args) {
		final Class[] argClasses = new Class[args.length];
		for (int i = 0; i < argClasses.length; i++) {
			argClasses[i] = args[i].getClass();
		}
		return newInstance(clzz, argClasses, args);
	}
}
