    001package com.hammurapi.common.concurrent;
    002
    003import java.lang.reflect.Method;
    004import java.util.concurrent.ExecutorService;
    005
    006/**
    007 * Adapts java.lang.reflect.Method to Invocable.
    008 * @author Pavel Vlasov
    009 *
    010 * @param <C>
    011 */
    012public class MethodInvocable<C> implements Invocable<C, Object, Object, InvocationException> {
    013
    014        Method method;
    015
    016        public MethodInvocable(Method method) {
    017                super();
    018                this.method = method;
    019        }
    020
    021        @Override
    022        public Object invoke(C context, Invocation<Object> invocation, ExecutorService executorService) throws InvocationException {
    023                try {
    024                        return method.invoke(context, invocation.getArguments());
    025                } catch (Exception e) {
    026                        throw new InvocationException(e);
    027                }
    028        };
    029
    030        @Override
    031        public Class<Object> getReturnType() {
    032                return (Class<Object>) method.getReturnType();
    033        }
    034
    035        @Override
    036        public Class<?>[] getParameterTypes() {
    037                return method.getParameterTypes();
    038        }
    039
    040}