| 1 | package com.hammurapi.common.concurrent; |
| 2 | |
| 3 | import java.lang.reflect.Method; |
| 4 | import java.util.concurrent.ExecutorService; |
| 5 | |
| 6 | /** |
| 7 | * Adapts java.lang.reflect.Method to Invocable. |
| 8 | * @author Pavel Vlasov |
| 9 | * |
| 10 | * @param <C> |
| 11 | */ |
| 12 | public class MethodInvocable<C> implements Invocable<C, Object, Object> { |
| 13 | |
| 14 | Method method; |
| 15 | |
| 16 | public MethodInvocable(Method method) { |
| 17 | super(); |
| 18 | this.method = method; |
| 19 | } |
| 20 | |
| 21 | @Override |
| 22 | public Object invoke(C context, Invocation invocation, ExecutorService executorService) { |
| 23 | try { |
| 24 | return method.invoke(context, invocation.getArguments()); |
| 25 | } catch (Exception e) { |
| 26 | throw new InvocationException(e); |
| 27 | } |
| 28 | } |
| 29 | |
| 30 | @Override |
| 31 | public Class<Object> getReturnType() { |
| 32 | return (Class<Object>) method.getReturnType(); |
| 33 | } |
| 34 | |
| 35 | @Override |
| 36 | public Class<?>[] getParameterTypes() { |
| 37 | return method.getParameterTypes(); |
| 38 | } |
| 39 | |
| 40 | } |