org.objectweb.easybeans.enhancer.interceptors
Class EasyBeansInvocationContextGenerator

java.lang.Object
  extended by org.objectweb.easybeans.enhancer.CommonClassGenerator
      extended by org.objectweb.easybeans.enhancer.interceptors.EasyBeansInvocationContextGenerator
All Implemented Interfaces:
org.objectweb.asm.Opcodes

public class EasyBeansInvocationContextGenerator
extends CommonClassGenerator

Generates the implementation of EasyBeansInvocationContext interface for a given business method.

Author:
Florent Benoit

Field Summary
private  java.util.List<JClassInterceptor> allInterceptors
          List of interceptors.
static java.lang.String ARG
          Name of the attributes will start with this name with an index as suffix, ie : arg0, arg1, arg2,...
private  java.lang.String beanClassDesc
          Bean class descriptor.
private  java.lang.String beanClassName
          Bean class name.
private  org.objectweb.asm.Type beanClassType
          Bean class Type (ASM).
private  ClassAnnotationMetadata classAnnotationMetadata
          Metadata available for a class (extracted from method metadat object.
private  java.lang.String constructorDesc
          ASM descriptor of the generated constructor.
static java.lang.String EASYBEANS_INVOCATION_CONTEXT
          EasyBeansInvocationContext interface.
private  java.lang.String generatedClassName
          Full class name of the generated class (prefixed by packageName).
static java.lang.String INTERCEPTOR
          Name of the interceptor attributes will start with this name with an index as suffix, ie : interceptor0, interceptor1, interceptor2,...
private  java.lang.String interceptorManagerClassName
          Name of the interceptor manager class.
private  InterceptorType interceptorType
          Type of the interceptor (AroundInvoke, PostConstruct, etc).
static java.lang.String[] INTERFACES
          Interface of this invocation context.
private  JMethod jMethod
          JMethod object which correspond to the current method metadata which is used.
private static JLog logger
          Logger.
private  MethodAnnotationMetadata methodAnnotationMetadata
          Metadata available for a method (given as constructor arg).
private  org.objectweb.asm.Type[] methodArgsType
          ASM Type arguments of the method.
static java.lang.String PACKAGE_NAME_PREFIX
          Prefix used as package name for generated classes (EasyBeansInvocationContext* impl).
private  java.lang.String packageName
          Package name which is used for generating class.
static java.lang.String[] PROCEED_EXCEPTIONS
          Exceptions of the proceed method.
static java.lang.String SUFFIX_CLASS
          Suffix for generated classes EasyBeansInvocationContextImpl.
static java.lang.String SUFFIX_INTERCEPTOR_MANAGER
          Suffix for InterceptorManager.
 
Fields inherited from class org.objectweb.easybeans.enhancer.CommonClassGenerator
ARRAY_OBJECTS, EASYBEANS_FACTORY, GENERATED_CLASS_VERSION, JAVA_LANG_EXCEPTION, JAVA_LANG_OBJECT, JAVA_LANG_REFLECT_METHOD, VOID_METHOD_JAVA_LANG_OBJECT
 
Fields inherited from interface org.objectweb.asm.Opcodes
AALOAD, AASTORE, ACC_ABSTRACT, ACC_ANNOTATION, ACC_BRIDGE, ACC_DEPRECATED, ACC_ENUM, ACC_FINAL, ACC_INTERFACE, ACC_NATIVE, ACC_PRIVATE, ACC_PROTECTED, ACC_PUBLIC, ACC_STATIC, ACC_STRICT, ACC_SUPER, ACC_SYNCHRONIZED, ACC_SYNTHETIC, ACC_TRANSIENT, ACC_VARARGS, ACC_VOLATILE, ACONST_NULL, ALOAD, ANEWARRAY, ARETURN, ARRAYLENGTH, ASTORE, ATHROW, BALOAD, BASTORE, BIPUSH, CALOAD, CASTORE, CHECKCAST, D2F, D2I, D2L, DADD, DALOAD, DASTORE, DCMPG, DCMPL, DCONST_0, DCONST_1, DDIV, DLOAD, DMUL, DNEG, DREM, DRETURN, DSTORE, DSUB, DUP, DUP_X1, DUP_X2, DUP2, DUP2_X1, DUP2_X2, F2D, F2I, F2L, FADD, FALOAD, FASTORE, FCMPG, FCMPL, FCONST_0, FCONST_1, FCONST_2, FDIV, FLOAD, FMUL, FNEG, FREM, FRETURN, FSTORE, FSUB, GETFIELD, GETSTATIC, GOTO, I2B, I2C, I2D, I2F, I2L, I2S, IADD, IALOAD, IAND, IASTORE, ICONST_0, ICONST_1, ICONST_2, ICONST_3, ICONST_4, ICONST_5, ICONST_M1, IDIV, IF_ACMPEQ, IF_ACMPNE, IF_ICMPEQ, IF_ICMPGE, IF_ICMPGT, IF_ICMPLE, IF_ICMPLT, IF_ICMPNE, IFEQ, IFGE, IFGT, IFLE, IFLT, IFNE, IFNONNULL, IFNULL, IINC, ILOAD, IMUL, INEG, INSTANCEOF, INVOKEINTERFACE, INVOKESPECIAL, INVOKESTATIC, INVOKEVIRTUAL, IOR, IREM, IRETURN, ISHL, ISHR, ISTORE, ISUB, IUSHR, IXOR, JSR, L2D, L2F, L2I, LADD, LALOAD, LAND, LASTORE, LCMP, LCONST_0, LCONST_1, LDC, LDIV, LLOAD, LMUL, LNEG, LOOKUPSWITCH, LOR, LREM, LRETURN, LSHL, LSHR, LSTORE, LSUB, LUSHR, LXOR, MONITORENTER, MONITOREXIT, MULTIANEWARRAY, NEW, NEWARRAY, NOP, POP, POP2, PUTFIELD, PUTSTATIC, RET, RETURN, SALOAD, SASTORE, SIPUSH, SWAP, T_BOOLEAN, T_BYTE, T_CHAR, T_DOUBLE, T_FLOAT, T_INT, T_LONG, T_SHORT, TABLESWITCH, V1_1, V1_2, V1_3, V1_4, V1_5, V1_6
 
Constructor Summary
EasyBeansInvocationContextGenerator(MethodAnnotationMetadata methodAnnotationMetadata, InterceptorType interceptorType)
          Constructor It will generate a class for the given method metadata.
 
Method Summary
private  void addAttributes()
          Add attributes of the class in two steps.
private  void addClassDeclaration()
          Creates the declaration of the class with the given interfaces.
private  void addConstructor()
          Create the constructor which should look like : First arg = bean instance Last args are arguments of the method (if any)
private  void addEasyBeansInvocationContextAttributes()
          Adds attributes of EasyBeansInvocationContext interface.
private  void addEasyBeansInvocationContextGetFactory()
          Adds the getFactory method of EasyBeansInvocationContext interface.
It adds :
private  void addEasyBeansInvocationContextMethods()
          Add methods for EasyBeansInvocationContext interface.
private  void addInvocationContextAttributes()
          Adds attributes of InvocationContext interface.
private  void addInvocationContextGetBean()
          Adds the getBean method of InvocationContext interface.
It adds :
 void addInvocationContextGetContextData()
          Adds the getContextData() method.
private  void addInvocationContextGetMethod()
          Adds the getMethod() method of InvocationContext interface.
It adds :
private  void addInvocationContextGetParameters()
          Adds the getParameters method of InvocationContext interface.
It adds :
private  void addInvocationContextMethods()
          Add methods for InvocationContext interface.
private  void addInvocationContextProceed()
          Adds the proceed method.
It adds :
private  void addInvocationContextSetParameters()
          Adds the setParameters method of InvocationContext interface.
It adds :
private  void addMethods()
          Add methods of the class in two steps.
private  void addStaticClassInitialization()
          Adds the initialization of static attributes.
private  void addToString()
          Generated toString() method.
private  void endClass()
          Called when the generated class is done.
 void generate()
          Generates the class.
 java.util.List<JClassInterceptor> getAllInterceptors()
           
 byte[] getBytes()
           
 java.lang.String getConstructorDesc()
           
 java.lang.String getGeneratedClassName()
           
 MethodAnnotationMetadata getMethodAnnotationMetadata()
           
 
Methods inherited from class org.objectweb.easybeans.enhancer.CommonClassGenerator
addAttribute, addAttribute, addFieldGettersSetters, addFieldGettersSetters, addNullGetter, addReturnType, encodeArrayClassDesc, encodeClassDesc, getCW, putFieldLoadOpCode, returnsObject, transformObjectIntoPrimitive, transformPrimitiveIntoObject, visitClassType
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

PACKAGE_NAME_PREFIX

public static final java.lang.String PACKAGE_NAME_PREFIX
Prefix used as package name for generated classes (EasyBeansInvocationContext* impl).

See Also:
Constant Field Values

ARG

public static final java.lang.String ARG
Name of the attributes will start with this name with an index as suffix, ie : arg0, arg1, arg2,...

See Also:
Constant Field Values

INTERCEPTOR

public static final java.lang.String INTERCEPTOR
Name of the interceptor attributes will start with this name with an index as suffix, ie : interceptor0, interceptor1, interceptor2,...

See Also:
Constant Field Values

SUFFIX_CLASS

public static final java.lang.String SUFFIX_CLASS
Suffix for generated classes EasyBeansInvocationContextImpl.

See Also:
Constant Field Values

INTERFACES

public static final java.lang.String[] INTERFACES
Interface of this invocation context.


PROCEED_EXCEPTIONS

public static final java.lang.String[] PROCEED_EXCEPTIONS
Exceptions of the proceed method.


EASYBEANS_INVOCATION_CONTEXT

public static final java.lang.String EASYBEANS_INVOCATION_CONTEXT
EasyBeansInvocationContext interface.


logger

private static JLog logger
Logger.


classAnnotationMetadata

private ClassAnnotationMetadata classAnnotationMetadata
Metadata available for a class (extracted from method metadat object. (parent))


packageName

private java.lang.String packageName
Package name which is used for generating class.


generatedClassName

private java.lang.String generatedClassName
Full class name of the generated class (prefixed by packageName).


jMethod

private JMethod jMethod
JMethod object which correspond to the current method metadata which is used.


methodAnnotationMetadata

private MethodAnnotationMetadata methodAnnotationMetadata
Metadata available for a method (given as constructor arg).


beanClassDesc

private java.lang.String beanClassDesc
Bean class descriptor.


beanClassName

private java.lang.String beanClassName
Bean class name.


beanClassType

private org.objectweb.asm.Type beanClassType
Bean class Type (ASM).


constructorDesc

private java.lang.String constructorDesc
ASM descriptor of the generated constructor.


methodArgsType

private org.objectweb.asm.Type[] methodArgsType
ASM Type arguments of the method.


allInterceptors

private java.util.List<JClassInterceptor> allInterceptors
List of interceptors.


interceptorType

private InterceptorType interceptorType
Type of the interceptor (AroundInvoke, PostConstruct, etc).


interceptorManagerClassName

private java.lang.String interceptorManagerClassName
Name of the interceptor manager class.


SUFFIX_INTERCEPTOR_MANAGER

public static final java.lang.String SUFFIX_INTERCEPTOR_MANAGER
Suffix for InterceptorManager.

See Also:
Constant Field Values
Constructor Detail

EasyBeansInvocationContextGenerator

public EasyBeansInvocationContextGenerator(MethodAnnotationMetadata methodAnnotationMetadata,
                                           InterceptorType interceptorType)
Constructor It will generate a class for the given method metadata.

Parameters:
methodAnnotationMetadata - method meta data
interceptorType - the type of invocationContext to generate (AroundInvoke, PostConstruct, etc)
Method Detail

generate

public void generate()
Generates the class. It call sub methods for being more clear for read the code


getBytes

public byte[] getBytes()
Returns:
the bytecode of the generated class.

addClassDeclaration

private void addClassDeclaration()
Creates the declaration of the class with the given interfaces.


addConstructor

private void addConstructor()
Create the constructor which should look like :
  public CtxImpl(Bean bean, int i, Long k, ...) {
      this.bean = bean;
      this.factory = bean.getEasyBeansFactory();
      this.interceptorManager = bean.getEasyBeansInterceptorManager();
      this.i = i;
      this.k = k;
      this... = ...
      this.interceptor0 = interceptorManager.getXXXInterceptor();
      this.interceptor1 = interceptorManager....();
  }
 


endClass

private void endClass()
Called when the generated class is done.


addAttributes

private void addAttributes()
Add attributes of the class in two steps.


addMethods

private void addMethods()
Add methods of the class in two steps.


addInvocationContextMethods

private void addInvocationContextMethods()
Add methods for InvocationContext interface.


addEasyBeansInvocationContextMethods

private void addEasyBeansInvocationContextMethods()
Add methods for EasyBeansInvocationContext interface.


addInvocationContextGetBean

private void addInvocationContextGetBean()
Adds the getBean method of InvocationContext interface.
It adds :
 public Object getBean() {
   return bean;
 }
 


addEasyBeansInvocationContextGetFactory

private void addEasyBeansInvocationContextGetFactory()
Adds the getFactory method of EasyBeansInvocationContext interface.
It adds :
 public Factory getFactory() {
     return this.factory;
 }
 


addInvocationContextGetMethod

private void addInvocationContextGetMethod()
Adds the getMethod() method of InvocationContext interface.
It adds :
 public Method getMethod() {
   if (method == null) {
     try {
       method = MyEjb.class.getMethod("methodName", new Class[] {xxx, yyy, ...});
     } catch (SecurityException e) {
       throw new RuntimeException("Cannot...", e);
     } catch (NoSuchMethodException e) {
       throw new RuntimeException("Cannot...", e);
     }
   }
   return method;
 }
 


addInvocationContextAttributes

private void addInvocationContextAttributes()
Adds attributes of InvocationContext interface.
      private StatelessBean bean;
      private Object[] parameters;
      private static Method method;
      private int interceptor;
      private Map contextData;

      // args of the method
      private TYPE_ARG_METHOD arg0 = xxx;
      private TYPE_ARG_METHOD arg1 = xxx;
      private TYPE_ARG_METHOD arg2 = xxx;
      private TYPE_ARG_METHOD.......;
 


addStaticClassInitialization

private void addStaticClassInitialization()
Adds the initialization of static attributes. ie : private static Method method = null private static InterceptorClass interceptor0 = new MyInterceptor(); private static InterceptorClass2 interceptor1 = ....


addEasyBeansInvocationContextAttributes

private void addEasyBeansInvocationContextAttributes()
Adds attributes of EasyBeansInvocationContext interface.
 private Factory factory;
 


addInvocationContextProceed

private void addInvocationContextProceed()
Adds the proceed method.
It adds :
  public Object proceed() throws Exception {
    interceptor++;
    switch (interceptor) {
      case 1 :
        return myInterceptor.intercept(this);
      case 2 :
        return otherInterceptor.intercept(this);
      case 3 :
           return bean.originalmethod(...);
       default:
           throw new IllegalStateException("Problem in interceptors");
    }
  }
 


addInvocationContextGetContextData

public void addInvocationContextGetContextData()
Adds the getContextData() method.
 public Map getContextData() {
    if (contextData == null) {
       contextData = new HashMap();
    }
    return contextData;
 }
 


addInvocationContextGetParameters

private void addInvocationContextGetParameters()
Adds the getParameters method of InvocationContext interface.
It adds :
 public Object[] getParameters() {
     if (parameters == null) {
         parameters = new Object[] {arg0, arg1, argxxx};
     }
     return parameters;
 }
 


addInvocationContextSetParameters

private void addInvocationContextSetParameters()
Adds the setParameters method of InvocationContext interface.
It adds :
 public void setParameters(Object aobj[]) {
   if (aobj == null) {
     throw new IllegalStateException("Cannot set a null array.");
   }
   if (aobj.length != ...) {
     throw new IllegalStateException("Invalid size of the given array. The length should be '" + ... + "'.");
   }
   parameters = aobj;

   arg0 = (Integer) aobj[0];
   arg1 = ((Integer) aobj[1]).intValue();
   arg2 = ((Double) aobj[2]).doubleValue();
   arg3 = ((Float) aobj[3]).floatValue();
   arg4 = (String) aobj[4];
   ...
 }

 


addToString

private void addToString()
Generated toString() method. Generated code is in the comments of the method body.


getMethodAnnotationMetadata

public MethodAnnotationMetadata getMethodAnnotationMetadata()
Returns:
method metadata used by this generator

getGeneratedClassName

public java.lang.String getGeneratedClassName()
Returns:
the name of the generated class name (with package name)

getConstructorDesc

public java.lang.String getConstructorDesc()
Returns:
the ASM descriptor of the generated constructor.

getAllInterceptors

public java.util.List<JClassInterceptor> getAllInterceptors()
Returns:
the interceptors used by this InvocationContext implementation object.