mrcode.duckprxy.impl
Class JavassistDuckPrxy

java.lang.Object
  extended by mrcode.duckprxy.impl.JavassistDuckPrxy
All Implemented Interfaces:
DuckPrxy

public class JavassistDuckPrxy
extends java.lang.Object
implements DuckPrxy

Javassist implementation of DuckPrxy. A new class is generated for a proxy which will delegate calls to the provided delegate object. No runtime reflection is used, making it very efficient. Also, it can implement classes, not just interfaces. However, there are some down-sides:

There are some rough edges: The proxy creator assumes that the delegate methods have a appropriate parameter and return types. This is not checked. You will probably get strange javassist or class loading exceptions when this assumption is not met.


Constructor Summary
JavassistDuckPrxy()
           
JavassistDuckPrxy(java.lang.String classNamePrefix)
          Constructor to set own class name prefix.
 
Method Summary
protected  void addInterface(javassist.ClassPool classPool, javassist.CtClass ctClass, java.lang.Class<?> interfce, java.lang.Class<?> delegateClass, java.util.List<MethodUtils.MethodRetrieveStrategy> strategies, java.lang.reflect.Method subDelegateGetter)
           
protected  void addMethod(javassist.ClassPool classPool, javassist.CtClass ctClass, java.lang.reflect.Method method, java.lang.Class<?> delegateClass, java.util.List<MethodUtils.MethodRetrieveStrategy> strategies, java.lang.reflect.Method subDelegateGetter)
           
protected  java.lang.String createClassName(java.lang.Class<?> delegateClass, java.util.Collection<java.lang.Class<?>> interfaces)
           
protected  javassist.CtClass getCtClass(javassist.ClassPool classPool, java.lang.Class<?> clazz)
           
protected  javassist.CtClass[] getCtClass(javassist.ClassPool classPool, java.lang.Class<?>[] classes)
           
protected  java.lang.String getDelegateCall(java.lang.String name, java.lang.reflect.Method delegateMethod, int argsLen)
           
protected  AbstractPrxyInvocationHandler.DelegateClassInformation getDelegateClassInformation(java.lang.Class<?> delegateClass)
           
 DuckArg getDuckArg(java.lang.annotation.Annotation[] annotations)
           
protected  java.lang.reflect.Method getMethodDelegate(java.lang.reflect.Method method, java.lang.Class<?> delegateClass, java.util.List<MethodUtils.MethodRetrieveStrategy> strategies, java.lang.reflect.Method subDelegateGetter, java.lang.StringBuilder delegatePath)
           
 java.lang.Class<?> getProxyClass(java.lang.String className, java.lang.Object delegate, java.lang.Class<?> delegateClass, java.util.List<java.lang.Class<?>> allInterfaces, javassist.ClassPool classPool)
           
<T> T
makeProxy(java.lang.Class<T> mainInterface, java.lang.Object delegate, java.lang.Class<?>... interfaces)
          Creates an object that implements the defined interfaces delegating to a given object, mainly using duck typing.
protected  java.util.List<MethodUtils.MethodRetrieveStrategy> makeStrategies(java.lang.Class<?> delegateClass, AbstractPrxyInvocationHandler.DelegateClassInformation info)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

JavassistDuckPrxy

public JavassistDuckPrxy()

JavassistDuckPrxy

public JavassistDuckPrxy(java.lang.String classNamePrefix)
Constructor to set own class name prefix. Normally, you do not want to override the class name prefix.

Parameters:
classNamePrefix - Override value for class name prefix.
Method Detail

makeProxy

public <T> T makeProxy(java.lang.Class<T> mainInterface,
                       java.lang.Object delegate,
                       java.lang.Class<?>... interfaces)
Description copied from interface: DuckPrxy
Creates an object that implements the defined interfaces delegating to a given object, mainly using duck typing.

Specified by:
makeProxy in interface DuckPrxy
Type Parameters:
T - The class of the main interface.
Parameters:
mainInterface - The main interface that the resulting object shall implement.
delegate - The object to delegate the calls to.
interfaces - Secondary interfaces to implement.
Returns:
An object implementing the main interface and secondary interfaces, delegating to the delegate object.

getProxyClass

public java.lang.Class<?> getProxyClass(java.lang.String className,
                                        java.lang.Object delegate,
                                        java.lang.Class<?> delegateClass,
                                        java.util.List<java.lang.Class<?>> allInterfaces,
                                        javassist.ClassPool classPool)
                                 throws javassist.CannotCompileException,
                                        javassist.NotFoundException
Throws:
javassist.CannotCompileException
javassist.NotFoundException

createClassName

protected java.lang.String createClassName(java.lang.Class<?> delegateClass,
                                           java.util.Collection<java.lang.Class<?>> interfaces)

addInterface

protected void addInterface(javassist.ClassPool classPool,
                            javassist.CtClass ctClass,
                            java.lang.Class<?> interfce,
                            java.lang.Class<?> delegateClass,
                            java.util.List<MethodUtils.MethodRetrieveStrategy> strategies,
                            java.lang.reflect.Method subDelegateGetter)
                     throws javassist.NotFoundException,
                            javassist.CannotCompileException
Throws:
javassist.NotFoundException
javassist.CannotCompileException

addMethod

protected void addMethod(javassist.ClassPool classPool,
                         javassist.CtClass ctClass,
                         java.lang.reflect.Method method,
                         java.lang.Class<?> delegateClass,
                         java.util.List<MethodUtils.MethodRetrieveStrategy> strategies,
                         java.lang.reflect.Method subDelegateGetter)
                  throws javassist.NotFoundException,
                         javassist.CannotCompileException
Throws:
javassist.NotFoundException
javassist.CannotCompileException

getMethodDelegate

protected java.lang.reflect.Method getMethodDelegate(java.lang.reflect.Method method,
                                                     java.lang.Class<?> delegateClass,
                                                     java.util.List<MethodUtils.MethodRetrieveStrategy> strategies,
                                                     java.lang.reflect.Method subDelegateGetter,
                                                     java.lang.StringBuilder delegatePath)

getDelegateCall

protected java.lang.String getDelegateCall(java.lang.String name,
                                           java.lang.reflect.Method delegateMethod,
                                           int argsLen)

getDuckArg

public DuckArg getDuckArg(java.lang.annotation.Annotation[] annotations)

getCtClass

protected javassist.CtClass getCtClass(javassist.ClassPool classPool,
                                       java.lang.Class<?> clazz)
                                throws javassist.NotFoundException
Throws:
javassist.NotFoundException

getCtClass

protected javassist.CtClass[] getCtClass(javassist.ClassPool classPool,
                                         java.lang.Class<?>[] classes)
                                  throws javassist.NotFoundException
Throws:
javassist.NotFoundException

makeStrategies

protected java.util.List<MethodUtils.MethodRetrieveStrategy> makeStrategies(java.lang.Class<?> delegateClass,
                                                                            AbstractPrxyInvocationHandler.DelegateClassInformation info)

getDelegateClassInformation

protected AbstractPrxyInvocationHandler.DelegateClassInformation getDelegateClassInformation(java.lang.Class<?> delegateClass)


Copyright © 2008. All Rights Reserved.