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

net.peachjean.commons.base.service.ServiceImplementation Maven / Gradle / Ivy

The newest version!
package net.peachjean.commons.base.service;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

import net.peachjean.commons.base.constructor.ConstructorSignatures;

/**
 * Utility used by generated {service}Factory classes.
 * @param 
 */
public class ServiceImplementation
{
	private Class serviceType;
	private Class implementationType;

	public ServiceImplementation(final Class serviceType, final Class implementationType)
	{
		this.serviceType = serviceType;
		this.implementationType = implementationType;
	}

	public T instantiate(Object ... args)
	{
		final Class[] argTypes = ConstructorSignatures.forService(serviceType);
		try
		{
			Constructor constructor = implementationType.getConstructor(argTypes);
			return constructor.newInstance(args);
		}
		catch (NoSuchMethodException e)
		{
			throw new ServiceInstantiationException(serviceType, implementationType, e);
		}
		catch (InvocationTargetException e)
		{
			throw new ServiceInstantiationException(serviceType, implementationType, e);
		}
		catch (InstantiationException e)
		{
			throw new ServiceInstantiationException(serviceType, implementationType, e);
		}
		catch (IllegalAccessException e)
		{
			throw new ServiceInstantiationException(serviceType, implementationType, e);
		}
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy