package com.hammurapi.common.concurrent;

import java.lang.reflect.Method;
import java.util.concurrent.ExecutorService;

/**
 * Adapts java.lang.reflect.Method to Invocable.
 * @author Pavel Vlasov
 *
 * @param <C>
 */
public class MethodInvocable<C> implements Invocable<C, Object, Object, InvocationException> {
	
	Method method;
	
	public MethodInvocable(Method method) {
		super();
		this.method = method;
	}
	
	@Override
	public Object invoke(C context, Invocation<Object> invocation, ExecutorService executorService) throws InvocationException {
		try {
			return method.invoke(context, invocation.getArguments());
		} catch (Exception e) {
			throw new InvocationException(e);
		}		
	};

	@Override
	public Class<Object> getReturnType() {
		return (Class<Object>) method.getReturnType();
	}

	@Override
	public Class<?>[] getParameterTypes() {
		return method.getParameterTypes();
	}

}
