org.codehaus.dimple
Class Implementor<ImplClass>

java.lang.Object
  extended by org.codehaus.dimple.Implementor<ImplClass>
All Implemented Interfaces:
java.io.Serializable

public class Implementor<ImplClass>
extends java.lang.Object
implements java.io.Serializable

This class is used to create implementation of interface(s) dynamically.

This class is ideal for creating stub or interceptor for interfaces where only a few methods are of interest while most other methods are either ignored or delegated.

For example:

 
 Connection realConn = ...;
 Connection nonCloseableConnection = Implementor.proxy(Connection.class, new Object(){
   public void close() {
     //we intercept close() call and do nothing.
   }
 }, realConn);
 nonCloseableConnection.close();// no-op. realConn is not closed.
 

Author:
Ben Yu Dec 9, 2006 11:27:44 PM
See Also:
Serialized Form

Constructor Summary
Implementor(java.lang.Class<ImplClass> implClass)
          To create an Implementor class.
 
Method Summary
 void checkImplementingMethods(java.lang.Class<?>... asTypes)
          Makes sure that methods defined by ImplClass implement some method in any Class object in asTypes.
 void checkImplementingMethods(java.lang.Class<?> asType)
          Makes sure that methods defined by ImplClass implement some method in asType.
 java.lang.reflect.InvocationHandler createInvocationHandler(ImplClass instance)
          create an InvocationHandler object by calling instance if a method is implemented by the impl class.
 java.lang.reflect.InvocationHandler createInvocationHandler(ImplClass instance, java.lang.Object defaultDelegate)
          create an InvocationHandler object by calling instance if a method is implemented by the impl class.
 java.lang.reflect.InvocationHandler createInvocationHandlerWithDefaultHandler(ImplClass instance, java.lang.reflect.InvocationHandler defaultHandler)
          create an InvocationHandler object by calling instance if a method is implemented by the impl class.
 boolean equals(java.lang.Object obj)
           
<T> Interceptor<T,ImplClass>
generateInterceptor(java.lang.Class<T> interceptedType)
          Generate byte code to create an interceptor that will intercept objects of interceptedType with objects of implClass.
static
<T,Impl> Interceptor<T,Impl>
generateInterceptor(java.lang.Class<T> interceptedType, java.lang.Class<Impl> implClass)
          Generate byte code to create an interceptor that will intercept objects of interceptedType with objects of Impl.
 java.lang.Class<ImplClass> getImplClass()
          Get the impl class, which is the class whose public methods are used to implement target interface.
 int hashCode()
           
<T> T
implement(java.lang.Class<T> asType, ImplClass with)
          create a dynamic proxy that implements asType by calling with if a method is implemented by the impl class.
<T> T
implement(java.lang.Class<T> asType, ImplClass with, T defaultDelegate)
          create a dynamic proxy that implements asType by calling with if a method is implemented by the impl class.
static
<T> java.lang.Class<T>
implementedBy(java.lang.Class<T> asType, java.lang.Class<?> implClass)
          To assert that all methods in implClass will properly implement some method in asType
<T> T
implementWithDefaultHandler(java.lang.Class<T> asType, ImplClass with, java.lang.reflect.InvocationHandler defaultHandler)
          create a dynamic proxy that implements asType by calling with if a method is implemented by the impl class.
static
<ImplClass>
Implementor<ImplClass>
instance(java.lang.Class<ImplClass> implClass)
          Convenience method to create an Implementor object.
static
<T> T
intercept(java.lang.Class<T> interceptedType, T intercepted, java.lang.Object with)
          Convenience method to intercept an instance of an interface.
 java.lang.reflect.Method lookupImplementingMethod(java.lang.reflect.Method implemented)
          To find a method in the impl class that can be used in place of the implemented method.
static
<T> T
newProxyInstance(java.lang.ClassLoader loader, java.lang.Class<T> asType, java.lang.reflect.InvocationHandler handler)
          To create a proxy instance for a given interface or superclass.
 java.lang.Object override(java.lang.Object obj, ImplClass overrider)
          Overrides an object using methods defined in impl class and the overrider object bound to "this".
static java.lang.Object overrideObject(java.lang.Object obj, java.lang.Object overrider)
          Overrides an object using the overrider object.
static
<T,ImplClass>
T
proxy(java.lang.Class<T> asType, ImplClass with)
          Equivalent as new Implementor(with.getClass()).implement(asType)
static
<T,ImplClass>
T
proxy(java.lang.Class<T> asType, ImplClass with, T defaultDelegate)
          Equivalent as new Implementor(with.getClass()).implement(asType, with, defaultDelegate)
static
<T,ImplClass>
T
proxyWithDefaultHandler(java.lang.Class<T> asType, ImplClass with, java.lang.reflect.InvocationHandler defaultHandler)
          Equivalent as new Implementor(with.getClass()).implementWithDefaultHandler(itf, with, defaultHandler)
static
<T> T
stub(java.lang.Class<T> stubbedType, java.lang.Object with)
          Convenience method to stub an interface.
 java.lang.String toString()
           
static
<ImplClass>
java.lang.Class<ImplClass>
willImplement(java.lang.Class<ImplClass> implClass, java.lang.Class<?>... asTypes)
          To assert that all methods in implClass will properly implement some method in any one of asTypes
static
<ImplClass>
java.lang.Class<ImplClass>
willImplement(java.lang.Class<ImplClass> implClass, java.lang.Class<?> asType)
          To assert that all methods in implClass will properly implement some method in asType
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Constructor Detail

Implementor

public Implementor(java.lang.Class<ImplClass> implClass)
To create an Implementor class.

Parameters:
implClass - the class used to implement.
Method Detail

proxyWithDefaultHandler

public static <T,ImplClass> T proxyWithDefaultHandler(java.lang.Class<T> asType,
                                                      ImplClass with,
                                                      java.lang.reflect.InvocationHandler defaultHandler)
Equivalent as new Implementor(with.getClass()).implementWithDefaultHandler(itf, with, defaultHandler)

This method is a convenience shortcut. As the constructor of Implementor may be expensive, it is recommended to create an Implementor object once and then use it repeatedly.


proxy

public static <T,ImplClass> T proxy(java.lang.Class<T> asType,
                                    ImplClass with,
                                    T defaultDelegate)
Equivalent as new Implementor(with.getClass()).implement(asType, with, defaultDelegate)

This method is a convenience shortcut. As the constructor of Implementor may be expensive, it is recommended to create an Implementor object once and then use it repeatedly.


proxy

public static <T,ImplClass> T proxy(java.lang.Class<T> asType,
                                    ImplClass with)
Equivalent as new Implementor(with.getClass()).implement(asType)

This method is a convenience shortcut. As the constructor of Implementor may be expensive, it is recommended to create an Implementor object once and then use it repeatedly.


implement

public <T> T implement(java.lang.Class<T> asType,
                       ImplClass with,
                       T defaultDelegate)
create a dynamic proxy that implements asType by calling with if a method is implemented by the impl class. Otherwise delegate call to defaultDelegate or throw UnsupportedOperationException if defaultDelegate is null.

Parameters:
asType - the interface to implement or super class to override (cglib is required in this case).
with - the instance of the impl class.
defaultDelegate - the default delegate.
Returns:
the dynamic proxy that implements asType.

implementWithDefaultHandler

public <T> T implementWithDefaultHandler(java.lang.Class<T> asType,
                                         ImplClass with,
                                         java.lang.reflect.InvocationHandler defaultHandler)
create a dynamic proxy that implements asType by calling with if a method is implemented by the impl class. Otherwise call the invoke() method of defaultHandler, or throw UnsupportedOperationException if defaultHandler is null.

Parameters:
asType - the interface to implement or super class to override (cglib is required in this case).
with - the instance of the impl class.
defaultHandler - the default InvocationHandler.
Returns:
the dynamic proxy that implements asType.

implement

public <T> T implement(java.lang.Class<T> asType,
                       ImplClass with)
create a dynamic proxy that implements asType by calling with if a method is implemented by the impl class. Otherwise call the invoke() method if with implements InvocationHandler, or throw UnsupportedOperationException otherwise.

Parameters:
asType - the interface to implement or super class to override (cglib is required in this case).
with - the instance of the impl class.
Returns:
the dynamic proxy that implements asType.

createInvocationHandler

public java.lang.reflect.InvocationHandler createInvocationHandler(ImplClass instance)
create an InvocationHandler object by calling instance if a method is implemented by the impl class. Otherwise call the invoke() method if instance implements InvocationHandler, or throw UnsupportedOperationException otherwise.

Parameters:
instance - the instance of the impl class.
Returns:
the InvocationHandler object.

createInvocationHandlerWithDefaultHandler

public java.lang.reflect.InvocationHandler createInvocationHandlerWithDefaultHandler(ImplClass instance,
                                                                                     java.lang.reflect.InvocationHandler defaultHandler)
create an InvocationHandler object by calling instance if a method is implemented by the impl class. Otherwise call the provided default InvocationHandler object.

Parameters:
instance - the instance of the impl class.
defaultHandler - the InvocationHandler object to provide default behavior. If null, UnsupportedOperationException is thrown.
Returns:
the InvocationHandler object.

createInvocationHandler

public java.lang.reflect.InvocationHandler createInvocationHandler(ImplClass instance,
                                                                   java.lang.Object defaultDelegate)
create an InvocationHandler object by calling instance if a method is implemented by the impl class. Otherwise forward the call to defaultDelegate.

Parameters:
instance - the instance of the impl class.
defaultDelegate - the default delegate. If null, UnsupportedOperationException is thrown.
Returns:
the InvocationHandler object.

instance

public static <ImplClass> Implementor<ImplClass> instance(java.lang.Class<ImplClass> implClass)
Convenience method to create an Implementor object.

Type Parameters:
ImplClass - the impl class.
Parameters:
implClass - the iml class object.
Returns:
the Implementor object.

equals

public boolean equals(java.lang.Object obj)
Overrides:
equals in class java.lang.Object

hashCode

public int hashCode()
Overrides:
hashCode in class java.lang.Object

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object

getImplClass

public java.lang.Class<ImplClass> getImplClass()
Get the impl class, which is the class whose public methods are used to implement target interface.


generateInterceptor

public <T> Interceptor<T,ImplClass> generateInterceptor(java.lang.Class<T> interceptedType)
Generate byte code to create an interceptor that will intercept objects of interceptedType with objects of implClass.

asm and cglib jar files have to be in classpath to use this method.

Type Parameters:
T - the type of object to be intercepted.
Parameters:
interceptedType - the intercepted type.
Returns:
the Interceptor instance.

generateInterceptor

public static <T,Impl> Interceptor<T,Impl> generateInterceptor(java.lang.Class<T> interceptedType,
                                                               java.lang.Class<Impl> implClass)
Generate byte code to create an interceptor that will intercept objects of interceptedType with objects of Impl.

asm and cglib jar files have to be in classpath to use this method.

Type Parameters:
T - the type of objects to be intercepted.
Impl - the type of objects used to intercept.
Parameters:
interceptedType - the intercepted type.
implClass - the type used to intercept.
Returns:
the interceptor.

intercept

public static <T> T intercept(java.lang.Class<T> interceptedType,
                              T intercepted,
                              java.lang.Object with)
Convenience method to intercept an instance of an interface. Equivalent to
 generateInterceptor(interceptedType, with.getClass()).intercept(intercepted, with);
 

asm and cglib jar files have to be in classpath to use this method.

Type Parameters:
T - the interface type to be intercepted.
Parameters:
interceptedType - the intercepted type.
intercepted - the object to be intercepted.
with - the object used to intercept.
Returns:
the intercepted instance.

stub

public static <T> T stub(java.lang.Class<T> stubbedType,
                         java.lang.Object with)
Convenience method to stub an interface. Equivalent to
 generateInterceptor(interceptedType, with.getClass()).stub(with);
 

asm and cglib jar files have to be in classpath to use this method.

Type Parameters:
T - the interface type to be stubbed.
Parameters:
stubbedType - the type to be stubbed.
with - the object used to stub.
Returns:
the stubbed instance.

lookupImplementingMethod

public java.lang.reflect.Method lookupImplementingMethod(java.lang.reflect.Method implemented)
To find a method in the impl class that can be used in place of the implemented method.

Parameters:
implemented - the method to be implemented.
Returns:
the method that can be used to implement, or null if not found.

newProxyInstance

public static <T> T newProxyInstance(java.lang.ClassLoader loader,
                                     java.lang.Class<T> asType,
                                     java.lang.reflect.InvocationHandler handler)
To create a proxy instance for a given interface or superclass.

Parameters:
loader - the class loader.
asType - the interface or super class (cglib is required in this case).
handler - the InvocationHandler to handle calls.
Returns:
the proxy instance.

checkImplementingMethods

public void checkImplementingMethods(java.lang.Class<?> asType)
                              throws InvalidReturnTypeException,
                                     UnusedMethodException
Makes sure that methods defined by ImplClass implement some method in asType. This checking is only performed on methods annotated by Implement.

Parameters:
asType - the interface to implement.
Throws:
InvalidReturnTypeException
UnusedMethodException

checkImplementingMethods

public void checkImplementingMethods(java.lang.Class<?>... asTypes)
                              throws InvalidReturnTypeException,
                                     UnusedMethodException
Makes sure that methods defined by ImplClass implement some method in any Class object in asTypes. This checking is only performed on methods annotated by Implement.

Parameters:
asTypes - the interfaces to implement.
Throws:
InvalidReturnTypeException
UnusedMethodException

implementedBy

public static <T> java.lang.Class<T> implementedBy(java.lang.Class<T> asType,
                                                   java.lang.Class<?> implClass)
                                        throws InvalidReturnTypeException,
                                               UnusedMethodException
To assert that all methods in implClass will properly implement some method in asType

Returns:
the asType
Throws:
InvalidReturnTypeException
UnusedMethodException

willImplement

public static <ImplClass> java.lang.Class<ImplClass> willImplement(java.lang.Class<ImplClass> implClass,
                                                                   java.lang.Class<?> asType)
                                                throws InvalidReturnTypeException,
                                                       UnusedMethodException
To assert that all methods in implClass will properly implement some method in asType

Returns:
the implClass
Throws:
InvalidReturnTypeException
UnusedMethodException

willImplement

public static <ImplClass> java.lang.Class<ImplClass> willImplement(java.lang.Class<ImplClass> implClass,
                                                                   java.lang.Class<?>... asTypes)
                                                throws InvalidReturnTypeException,
                                                       UnusedMethodException
To assert that all methods in implClass will properly implement some method in any one of asTypes

Returns:
the implClass
Throws:
InvalidReturnTypeException
UnusedMethodException

override

public final java.lang.Object override(java.lang.Object obj,
                                       ImplClass overrider)
Overrides an object using methods defined in impl class and the overrider object bound to "this". All interfaces of obj are implemented by the proxy.

Parameters:
obj - the object to be overriden.
overrider - the overrider.
Returns:
the proxy object.

overrideObject

public static java.lang.Object overrideObject(java.lang.Object obj,
                                              java.lang.Object overrider)
Overrides an object using the overrider object. All interfaces of obj are implemented by the proxy.

Parameters:
obj - the object to be overriden.
overrider - the overrider.
Returns:
the proxy object.