All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.wichell.framework.proxy.mybatis.MybatisProxyFactory Maven / Gradle / Ivy

package com.wichell.framework.proxy.mybatis;

import java.util.Collection;
import java.util.List;

import org.apache.ibatis.reflection.ReflectionException;
import org.apache.ibatis.reflection.factory.DefaultObjectFactory;

import com.wichell.framework.proxy.ProxyFactory;

@SuppressWarnings("serial")
public abstract class MybatisProxyFactory extends DefaultObjectFactory implements IMybatisProxyFactory {
	private static ProxyFactory proxyFactory;

	@Override
	public  T create(Class type) {
		return create(type, null, null);
	}

	@SuppressWarnings("static-access")
	public MybatisProxyFactory() {
		this.proxyFactory = new ProxyFactory(init());
	}

	@Override
	public  T create(Class type, List> constructorArgTypes, List constructorArgs) {
		Class classToCreate = resolveInterface(type);
		@SuppressWarnings("unchecked")
		// we know types are assignable
		T created = (T) instantiateClassNew(classToCreate, constructorArgTypes, constructorArgs);
		return created;
	}

	@SuppressWarnings("static-access")
	private  T instantiateClassNew(Class type, List> constructorArgTypes, List constructorArgs) {
		if (Collection.class.isAssignableFrom(type)) {
			return super.create(type, constructorArgTypes, constructorArgs);
		}
		try {
			if (constructorArgTypes == null || constructorArgs == null) {
				return proxyFactory.newInstance(type);
			}
			return proxyFactory.newInstance(type, constructorArgTypes.toArray(new Class[constructorArgTypes.size()]),
					constructorArgs.toArray(new Object[constructorArgs.size()]));
		} catch (Exception e) {
			StringBuilder argTypes = new StringBuilder();
			if (constructorArgTypes != null) {
				for (Class argType : constructorArgTypes) {
					argTypes.append(argType.getSimpleName());
					argTypes.append(",");
				}
			}
			StringBuilder argValues = new StringBuilder();
			if (constructorArgs != null) {
				for (Object argValue : constructorArgs) {
					argValues.append(String.valueOf(argValue));
					argValues.append(",");
				}
			}
			throw new ReflectionException("Error instantiating " + type + " with invalid types (" + argTypes
					+ ") or values (" + argValues + "). Cause: " + e, e);
		}
	}
}