org.objectweb.easybeans.enhancer.interceptors
Class InterceptorClassAdapter

java.lang.Object
  extended by org.objectweb.asm.ClassAdapter
      extended by org.objectweb.easybeans.enhancer.interceptors.InterceptorClassAdapter
All Implemented Interfaces:
org.objectweb.asm.ClassVisitor, org.objectweb.asm.Opcodes

public class InterceptorClassAdapter
extends org.objectweb.asm.ClassAdapter
implements org.objectweb.asm.Opcodes

This class delegates the creation of an implementation of a EasyBeansInvocationContext interface and intercepts all business methods() of a Bean.

Author:
Florent Benoit

Field Summary
private  boolean addInterface
          If it is true, interfaces of interceptor lifecycle will be added.
private  java.util.List<java.lang.String> beanInterceptors
          List of interceptors classes used by the bean.
private  ClassAnnotationMetadata classAnnotationMetadata
          Metadata available by this adapter for a class.
private  java.util.List<DefinedClass> definedClasses
          Mappping between className and the bytecode.
private  java.util.List<InterceptorType> generatedTypes
          List of generated classes for each interceptor type.
private  java.util.List<JMethod> renamedMethods
          List of methods which have been renamed.
 
Fields inherited from class org.objectweb.asm.ClassAdapter
cv
 
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
InterceptorClassAdapter(ClassAnnotationMetadata classAnnotationMetadata, org.objectweb.asm.ClassVisitor cv)
          Constructor.
InterceptorClassAdapter(ClassAnnotationMetadata classAnnotationMetadata, org.objectweb.asm.ClassVisitor cv, boolean addInterface)
          Constructor.
 
Method Summary
private  void generateCallSuperEncodedMethod(MethodAnnotationMetadata method)
          Generates a call to the method defined in the super class.
private  void generateCallToInvocationContext(MethodAnnotationMetadata method, EasyBeansInvocationContextGenerator genInvCtx, InterceptorType interceptorType)
          Generates the call to InvocationContext impl proceed method after building a new object.
private  void generateClass(MethodAnnotationMetadata method, InterceptorType interceptorType)
          Generate an invocation context object.
private  MethodAnnotationMetadata generateDummyMethod(ClassAnnotationMetadata classMetaData, InterceptorType interceptorType)
          Generates a dummy method for missing lifecycle method events.
 java.util.List<DefinedClass> getDefinedClasses()
           
private  boolean isDependencyInjectionMethod(JMethod jMethod)
          Check if this method is the injected method used for dependency injection.
private  boolean isInjectedMethod(JMethod jMethod)
          Check if this method is injected or not by injection class adapter : No need to add interceptors on these methods.
private  boolean isInterceptedMethod(JMethod jMethod)
           
private  boolean isInterceptorMethod(JMethod jMethod)
           
 void visit(int version, int access, java.lang.String name, java.lang.String signature, java.lang.String superName, java.lang.String[] interfaces)
          Visits the header of the class.
 void visitEnd()
          Visits the end of the class.
 void visitInnerClass(java.lang.String name, java.lang.String outerName, java.lang.String innerName, int access)
          Visits information about an inner class.
 org.objectweb.asm.MethodVisitor visitMethod(int access, java.lang.String name, java.lang.String desc, java.lang.String signature, java.lang.String[] exceptions)
          Visits a method of the class.
 
Methods inherited from class org.objectweb.asm.ClassAdapter
visitAnnotation, visitAttribute, visitField, visitOuterClass, visitSource
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

classAnnotationMetadata

private ClassAnnotationMetadata classAnnotationMetadata
Metadata available by this adapter for a class.


renamedMethods

private java.util.List<JMethod> renamedMethods
List of methods which have been renamed.


definedClasses

private java.util.List<DefinedClass> definedClasses
Mappping between className and the bytecode.


generatedTypes

private java.util.List<InterceptorType> generatedTypes
List of generated classes for each interceptor type.


beanInterceptors

private java.util.List<java.lang.String> beanInterceptors
List of interceptors classes used by the bean.


addInterface

private boolean addInterface
If it is true, interfaces of interceptor lifecycle will be added.

Constructor Detail

InterceptorClassAdapter

public InterceptorClassAdapter(ClassAnnotationMetadata classAnnotationMetadata,
                               org.objectweb.asm.ClassVisitor cv)
Constructor.

Parameters:
classAnnotationMetadata - object containing all attributes of the class
cv - the class visitor to which this adapter must delegate calls.

InterceptorClassAdapter

public InterceptorClassAdapter(ClassAnnotationMetadata classAnnotationMetadata,
                               org.objectweb.asm.ClassVisitor cv,
                               boolean addInterface)
Constructor.

Parameters:
classAnnotationMetadata - object containing all attributes of the class
cv - the class visitor to which this adapter must delegate calls.
addInterface - adds lifecycle interface for a given bean.
Method Detail

visit

public void visit(int version,
                  int access,
                  java.lang.String name,
                  java.lang.String signature,
                  java.lang.String superName,
                  java.lang.String[] interfaces)
Visits the header of the class.

Specified by:
visit in interface org.objectweb.asm.ClassVisitor
Overrides:
visit in class org.objectweb.asm.ClassAdapter
Parameters:
version - the class version.
access - the class's access flags (see Opcodes). This parameter also indicates if the class is deprecated.
name - the internal name of the class (see getInternalName).
signature - the signature of this class. May be null if the class is not a generic one, and does not extend or implement generic classes or interfaces.
superName - the internal of name of the super class (see getInternalName). For interfaces, the super class is Object. May be null, but only for the Object class.
interfaces - the internal names of the class's interfaces (see getInternalName). May be null.

visitInnerClass

public void visitInnerClass(java.lang.String name,
                            java.lang.String outerName,
                            java.lang.String innerName,
                            int access)
Visits information about an inner class. This inner class is not necessarily a member of the class being visited.

Specified by:
visitInnerClass in interface org.objectweb.asm.ClassVisitor
Overrides:
visitInnerClass in class org.objectweb.asm.ClassAdapter
Parameters:
name - the internal name of an inner class (see getInternalName).
outerName - the internal name of the class to which the inner class belongs (see getInternalName). May be null.
innerName - the (simple) name of the inner class inside its enclosing class. May be null for anonymous inner classes.
access - the access flags of the inner class as originally declared in the enclosing class.

visitMethod

public org.objectweb.asm.MethodVisitor visitMethod(int access,
                                                   java.lang.String name,
                                                   java.lang.String desc,
                                                   java.lang.String signature,
                                                   java.lang.String[] exceptions)
Visits a method of the class. T

Specified by:
visitMethod in interface org.objectweb.asm.ClassVisitor
Overrides:
visitMethod in class org.objectweb.asm.ClassAdapter
Parameters:
access - the method's access flags (see Opcodes). This parameter also indicates if the method is synthetic and/or deprecated.
name - the method's name.
desc - the method's descriptor (see Type).
signature - the method's signature. May be null if the method parameters, return type and exceptions do not use generic types.
exceptions - the internal names of the method's exception classes (see getInternalName). May be null.
Returns:
an object to visit the byte code of the method, or null if this class visitor is not interested in visiting the code of this method.

visitEnd

public void visitEnd()
Visits the end of the class. This method, which is the last one to be called, is used to inform the visitor that all the fields and methods of the class have been visited.

Specified by:
visitEnd in interface org.objectweb.asm.ClassVisitor
Overrides:
visitEnd in class org.objectweb.asm.ClassAdapter

generateCallToInvocationContext

private void generateCallToInvocationContext(MethodAnnotationMetadata method,
                                             EasyBeansInvocationContextGenerator genInvCtx,
                                             InterceptorType interceptorType)
Generates the call to InvocationContext impl proceed method after building a new object. ie :
 public int generatedMethodName(int a, int b) throws MyException {
     try {
         return ((Integer) new MethodAddInvocationContextImpl(this, a, b).proceed()).intValue();
     } catch (MyException e) {
         throw e;
     } catch (Exception e) {
         throw new RuntimeException(e);
     }
 }
 

Parameters:
method - the annotation metadata of the method
genInvCtx - the generator of the EasyBeansInvocationContext impl class.
interceptorType - the type of method which is intercepted

generateClass

private void generateClass(MethodAnnotationMetadata method,
                           InterceptorType interceptorType)
Generate an invocation context object.

Parameters:
method - intercepted method
interceptorType - the type of method which is intercepted

generateCallSuperEncodedMethod

private void generateCallSuperEncodedMethod(MethodAnnotationMetadata method)
Generates a call to the method defined in the super class. public int original$add(int i, int j) { return super.add(i, j); }

Parameters:
method - the annotation metadata of the method

generateDummyMethod

private MethodAnnotationMetadata generateDummyMethod(ClassAnnotationMetadata classMetaData,
                                                     InterceptorType interceptorType)
Generates a dummy method for missing lifecycle method events.

Parameters:
classMetaData - the metadata used to generate method metadata
interceptorType - the type of intercepted method
Returns:
the generated method metadata

isDependencyInjectionMethod

private boolean isDependencyInjectionMethod(JMethod jMethod)
Check if this method is the injected method used for dependency injection.

Parameters:
jMethod - object to check
Returns:
true if the given method is the injected method used for dependency injection

isInjectedMethod

private boolean isInjectedMethod(JMethod jMethod)
Check if this method is injected or not by injection class adapter : No need to add interceptors on these methods.

Parameters:
jMethod - object to check
Returns:
true if the given method is injected by injection class adapter.

isInterceptedMethod

private boolean isInterceptedMethod(JMethod jMethod)
Parameters:
jMethod - object to check
Returns:
true if the given method is an intercepted method (and business method) because lifecycle methods are not renamed

isInterceptorMethod

private boolean isInterceptorMethod(JMethod jMethod)
Parameters:
jMethod - object to check
Returns:
true if the given method is an interceptor method (ie AroundInvoke, PostConstruct, etc).

getDefinedClasses

public java.util.List<DefinedClass> getDefinedClasses()
Returns:
list of classes generated and that need to be defined in a classloader