001package org.javasimon.proxy;
002
003import java.lang.reflect.Method;
004import java.util.concurrent.Callable;
005
006/**
007 * Proxy method invocation.
008 *
009 * @author gquintana
010 */
011public class DelegatingMethodInvocation<T> implements Delegating<T>, Runnable, Callable<Object> {
012
013        /** Target (real) object. */
014        private final T delegate;
015
016        /** Proxy. */
017        private final Object proxy;
018
019        /** Method. */
020        private final Method method;
021
022        /** Invocation arguments. */
023        private final Object[] args;
024
025        public DelegatingMethodInvocation(T target, Object proxy, Method method, Object... args) {
026                this.delegate = target;
027                this.proxy = proxy;
028                this.method = method;
029                this.args = args;
030        }
031
032        public Object[] getArgs() {
033                return args;
034        }
035
036        public Method getMethod() {
037                return method;
038        }
039
040        public Object getProxy() {
041                return proxy;
042        }
043
044        public T getDelegate() {
045                return delegate;
046        }
047
048        public Method getTargetMethod() throws NoSuchMethodException {
049                return delegate.getClass().getMethod(method.getName(), method.getParameterTypes());
050        }
051
052        public Object proceed() throws Throwable {
053                return method.invoke(delegate, args);
054        }
055
056        public void run() {
057                try {
058                        proceed();
059                } catch (Throwable throwable) {
060                        // Forget exception
061                }
062        }
063
064        public Object call() throws Exception {
065                try {
066                        return proceed();
067                } catch (Exception exception) {
068                        throw exception;
069                } catch (Throwable throwable) {
070                        throw new IllegalStateException(throwable);
071                }
072        }
073}