org.codehaus.dimple
Class Implementor

java.lang.Object
  |
  +--org.codehaus.dimple.Implementor
All Implemented Interfaces:
java.io.Serializable

public class Implementor
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 = (Connection)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)
          To create an Implementor class.
 
Method Summary
 java.lang.reflect.InvocationHandler createInvocationHandler(java.lang.Object instance)
          create an InvocationHandler object by calling instance if a method is implemented by the impl class.
 java.lang.reflect.InvocationHandler createInvocationHandler(java.lang.Object 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(java.lang.Object 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)
           
 java.lang.Class getImplClass()
          Get the impl class, which is the class whose public methods are used to implement target interface.
 int hashCode()
           
 java.lang.Object implement(java.lang.Class asType, java.lang.Object with)
          create a dynamic proxy that implements asType by calling with if a method is implemented by the impl class.
 java.lang.Object implement(java.lang.Class asType, java.lang.Object with, java.lang.Object defaultDelegate)
          create a dynamic proxy that implements asType by calling with if a method is implemented by the impl class.
static java.lang.Class implementedBy(java.lang.Class asType, java.lang.Class implClass)
          To assert that all methods in implClass will properly implement some method in asType
 java.lang.Object implementWithDefaultHandler(java.lang.Class asType, java.lang.Object 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.
 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 java.lang.Object newProxyInstance(java.lang.ClassLoader loader, java.lang.Class 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, java.lang.Object 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 java.lang.Object proxy(java.lang.Class asType, java.lang.Object with)
          Equivalent as new Implementor(with.getClass()).implement(asType)
static java.lang.Object proxy(java.lang.Class asType, java.lang.Object with, java.lang.Object defaultDelegate)
          Equivalent as new Implementor(with.getClass()).implement(asType, with, defaultDelegate)
static java.lang.Object proxyWithDefaultHandler(java.lang.Class asType, java.lang.Object with, java.lang.reflect.InvocationHandler defaultHandler)
          Equivalent as new Implementor(with.getClass()).implementWithDefaultHandler(itf, with, defaultHandler)
 java.lang.String toString()
           
static java.lang.Class willImplement(java.lang.Class implClass, java.lang.Class asType)
          To assert that all methods in implClass will properly implement some method in asType
static java.lang.Class willImplement(java.lang.Class implClass, java.lang.Class[] asTypes)
          To assert that all methods in implClass will properly implement some method in any one of asTypes
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Constructor Detail

Implementor

public Implementor(java.lang.Class implClass)
To create an Implementor class.
Parameters:
implClass - the class used to implement.
Method Detail

proxyWithDefaultHandler

public static java.lang.Object proxyWithDefaultHandler(java.lang.Class asType,
                                                       java.lang.Object 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 java.lang.Object proxy(java.lang.Class asType,
                                     java.lang.Object with,
                                     java.lang.Object 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 java.lang.Object proxy(java.lang.Class asType,
                                     java.lang.Object 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 java.lang.Object implement(java.lang.Class asType,
                                  java.lang.Object with,
                                  java.lang.Object 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 java.lang.Object implementWithDefaultHandler(java.lang.Class asType,
                                                    java.lang.Object 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 java.lang.Object implement(java.lang.Class asType,
                                  java.lang.Object 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(java.lang.Object 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(java.lang.Object 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(java.lang.Object 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.

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 getImplClass()
Get the impl class, which is the class whose public methods are used to implement target interface.

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 java.lang.Object newProxyInstance(java.lang.ClassLoader loader,
                                                java.lang.Class 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.

implementedBy

public static java.lang.Class implementedBy(java.lang.Class 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 java.lang.Class willImplement(java.lang.Class 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 java.lang.Class willImplement(java.lang.Class 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,
                                       java.lang.Object 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 final 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.