com.thoughtworks.proxy.toys.delegate
Class DelegatingInvoker

java.lang.Object
  extended by com.thoughtworks.proxy.toys.delegate.DelegatingInvoker
All Implemented Interfaces:
Invoker, Serializable
Direct Known Subclasses:
HotSwappingInvoker, Pool.PoolingInvoker

public class DelegatingInvoker
extends Object
implements Invoker

Invoker that delegates method calls to an object.

This forms the basis of many other proxy toys. The delegation behaviour was factored out of HotSwappingInvoker.

Since:
0.1
Author:
Aslak Hellesøy, Paul Hammant, Dan North, Jörg Schaible
See Also:
HotSwappingInvoker, Serialized Form

Constructor Summary
DelegatingInvoker(Object delegate)
          Construct a DelegatingInvoker with a StandardProxyFactory and Delegating.MODE_SIGNATURE.
DelegatingInvoker(ProxyFactory proxyFactory, ObjectReference delegateReference, boolean staticTyping)
          Deprecated. since 0.2, use DelegatingInvoker(ProxyFactory, ObjectReference, int)
DelegatingInvoker(ProxyFactory proxyFactory, ObjectReference delegateReference, int delegationMode)
          Construct a DelegatingInvoker.
 
Method Summary
protected  Object delegate()
          Retrieve the delegated object in derived classes.
 boolean equals(Object obj)
          Compares a DelegatingInvoker with another one for equality.
protected  ObjectReference getDelegateReference()
          Retrieve the ObjectReference of the delegate.
protected  Method getMethodToInvoke(Method method, Object[] args)
          Lookup a matching method.
protected  ProxyFactory getProxyFactory()
          Retrieve the ProxyFactory to use.
 int hashCode()
           
 Object invoke(Object proxy, Method method, Object[] args)
          Invocation of a method of the proxied object.
protected  Object invokeOnDelegate(Method method, Object[] args)
          Invoke the given method on the delegate.
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

DelegatingInvoker

public DelegatingInvoker(ProxyFactory proxyFactory,
                         ObjectReference delegateReference,
                         int delegationMode)
Construct a DelegatingInvoker.

Parameters:
proxyFactory - the ProxyFactory to use
delegateReference - the ObjectReference of the delegate
delegationMode - one of the delegation modes
Throws:
IllegalArgumentException - if the delegationMode is not one of the predefined constants of Delegating
Since:
0.2

DelegatingInvoker

public DelegatingInvoker(ProxyFactory proxyFactory,
                         ObjectReference delegateReference,
                         boolean staticTyping)
Deprecated. since 0.2, use DelegatingInvoker(ProxyFactory, ObjectReference, int)

Construct a DelegatingInvoker.

Parameters:
proxyFactory - the ProxyFactory to use
delegateReference - the ObjectReference of the delegate
staticTyping - Delegating.STATIC_TYPING or Delegating.DYNAMIC_TYPING
Since:
0.1

DelegatingInvoker

public DelegatingInvoker(Object delegate)
Construct a DelegatingInvoker with a StandardProxyFactory and Delegating.MODE_SIGNATURE.

Parameters:
delegate - the delegated object
Since:
0.1
Method Detail

invoke

public Object invoke(Object proxy,
                     Method method,
                     Object[] args)
              throws Throwable
Description copied from interface: Invoker
Invocation of a method of the proxied object.

Specified by:
invoke in interface Invoker
Parameters:
proxy - the proxy instance.
method - the method to invoke.
args - the arguments of the mothod.
Returns:
the result of the onvoked method.
Throws:
Throwable - if the invoked method has thrown.

delegate

protected Object delegate()
Retrieve the delegated object in derived classes.

Returns:
the delegated object
Since:
0.1

getMethodToInvoke

protected Method getMethodToInvoke(Method method,
                                   Object[] args)
Lookup a matching method. The lookup will only be done once for every method called on the proxy.

Parameters:
method - the invoked method on the proxy
args - the arguments for the invocation
Returns:
the matching method
Throws:
DelegationException - if no matching method can be found
Since:
0.2

invokeOnDelegate

protected Object invokeOnDelegate(Method method,
                                  Object[] args)
                           throws InvocationTargetException
Invoke the given method on the delegate.

Parameters:
method - the method to invoke
args - the arguments for the invocation
Returns:
the method's result
Throws:
InvocationTargetException - if the invoked method throws any exception
Since:
0.1

getDelegateReference

protected ObjectReference getDelegateReference()
Retrieve the ObjectReference of the delegate.

Returns:
the reference of hte delegate
Since:
0.2

getProxyFactory

protected ProxyFactory getProxyFactory()
Retrieve the ProxyFactory to use.

Returns:
the ProxyFactory
Since:
0.2

equals

public boolean equals(Object obj)
Compares a DelegatingInvoker with another one for equality. Two DelegatingInvoker are equal, if they have both the same delegation mode and their delegees are equal.

Overrides:
equals in class Object
Since:
0.2
See Also:
Object.equals(java.lang.Object)

hashCode

public int hashCode()
Overrides:
hashCode in class Object