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

org.javasimon.proxy.DelegatingMethodInvocation Maven / Gradle / Ivy

There is a newer version: 4.2.0
Show newest version
package org.javasimon.proxy;

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

/**
 * Proxy method invocation.
 *
 * @author gquintana
 */
public class DelegatingMethodInvocation implements Delegating, Runnable, Callable {

	/** Target (real) object. */
	private final T delegate;
	/** Proxy. */
	private final Object proxy;
	/** Method. */
	private final Method method;
	/** Invocation arguments. */
	private final Object[] args;

	public DelegatingMethodInvocation(T target, Object proxy, Method method, Object... args) {
		this.delegate = target;
		this.proxy = proxy;
		this.method = method;
		this.args = args;
	}

	public Object[] getArgs() {
		return args;
	}

	public Method getMethod() {
		return method;
	}

	public Object getProxy() {
		return proxy;
	}

	public T getDelegate() {
		return delegate;
	}

	public Method getTargetMethod() throws NoSuchMethodException {
		return delegate.getClass().getMethod(method.getName(), method.getParameterTypes());
	}

	public Object proceed() throws Throwable {
		return method.invoke(delegate, args);
	}

	public void run() {
		try {
			proceed();
		} catch (Throwable throwable) {
			// Forget exception
		}
	}

	public Object call() throws Exception {
		try {
			return proceed();
		} catch (Exception exception) {
			throw exception;
		} catch (Throwable throwable) {
			throw new IllegalStateException(throwable);
		}
	}
}