org.objectweb.easybeans.enhancer.injection
Class InjectionClassAdapter

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

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

This class adds methods which will inject resources in the bean class.

Author:
Florent Benoit

Field Summary
private  ClassAnnotationMetadata classAnnotationMetadata
          Metadata available by this adapter for a class.
private static java.lang.String DATASOURCE_ITF
          javax.sql.DataSource interface.
private static java.lang.String ENTITYMANAGER_ITF
          Entity Manager interface.
private static java.lang.String ENTITYMANAGERFACTORY_ITF
          Entity Manager Factory interface.
private static java.lang.String EZB_SESSIONCONTEXT_DESC
          EasyBeansSessionContext type descriptor.
static JMethod INJECTED_JMETHOD
          JMethod object for injectedByEasyBeans.
static java.lang.String INJECTED_METHOD
          Injected method name.
static java.lang.String[] INJECTED_METHODS
          List of injected methods.
static java.lang.String JAVA_LANG_OBJECT
          Defines java.lang.Object class.
private static int LENGTH
          Replace length to create default JNDI names.
private static JLog logger
          Logger.
private  java.util.Map<java.lang.String,java.lang.Object> map
          Map containing informations for enhancers.
private static java.lang.String ORB_ITF
          org.omg.CORBA.ORB interface.
private static java.lang.String SESSION_CONTEXT
          javax.ejb.SessionContext interface.
private  boolean staticMode
          Is that generated method is static (client case).
private static java.lang.String TIMERSERVICE_ITF
          javax.ejb.TimerService interface.
private static java.lang.String USERTRANSACTION_ITF
          javax.transaction.UserTransaction interface.
 
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
InjectionClassAdapter(ClassAnnotationMetadata classAnnotationMetadata, org.objectweb.asm.ClassVisitor cv, java.util.Map<java.lang.String,java.lang.Object> map, boolean staticMode)
          Constructor.
 
Method Summary
private  boolean accessTest(int access, int checkedAccess)
           
private  void addCallEntityManagerFactoryHelper(JavaxPersistenceUnit javaxPersistenceUnit, org.objectweb.asm.MethodVisitor mv)
          Add a call to EntityManagerHelper class (PersistenceUnit): EntityManagerHelper.getEntityManagerFactory(getEasyBeansSessionContext(), unitName).
private  void addCallEntityManagerHelper(JavaxPersistenceContext javaxPersistenceContext, org.objectweb.asm.MethodVisitor mv)
          Add a call to EntityManagerHelper class (PersistenceContext) : EntityManagerHelper.getEntityManager(getEasyBeansSessionContext(), unitName, type of persistence).
private  void addCallGetEasyBeansContext(org.objectweb.asm.MethodVisitor mv)
          Add a call to getEasyBeansSessionContext() method in the given method visitor.
private  void addDefaultMethods()
          Generated methods allowing to set a context and a factory.
private  void addInjectedMethod()
          Generates the injectedByEasyBeans() method on the current class.
private  void bindClassEJB(JEjbEJB jEJB, org.objectweb.asm.MethodVisitor mv)
          Bind a ref for an EJB in ENC environment.
private  void bindClassPersistenceContext(JavaxPersistenceContext javaxPersistenceContext, org.objectweb.asm.MethodVisitor mv)
          Bind a ref for a PersistenceContext in ENC environment.
private  void bindClassPersistenceUnit(JavaxPersistenceUnit javaxPersistenceUnit, org.objectweb.asm.MethodVisitor mv)
          Bind a ref for a PersistenceUnit in ENC environment.
private  void bindResource(JAnnotationResource jAnnotationResource, org.objectweb.asm.MethodVisitor mv)
          Bind a ref for a Resource in ENC environment.
private  void callAttributeJndi(java.lang.String jndiName, org.objectweb.asm.Type type, org.objectweb.asm.MethodVisitor mv, FieldAnnotationMetadata fieldMetaData, java.lang.String className, JNDILookupHelper.JndiType jndiType)
          Generates a call to JNDILookupHelper class to get the java:comp/env name requested.
private  void callBindAttributeJndi(java.lang.String encName, java.lang.String jndiName, org.objectweb.asm.MethodVisitor mv, FieldAnnotationMetadata fieldMetaData)
          Generates a call to JNDIBinderHelper class to bind an object into the java:comp/env context.
private  void callBindLookupJndiRef(java.lang.String encName, java.lang.String jndiName, org.objectweb.asm.MethodVisitor mv)
          Generates a call to JNDIBinderHelper class to link an object into the java:comp/env context.
private  void callJndi(java.lang.String jndiName, org.objectweb.asm.Type type, org.objectweb.asm.MethodVisitor mv, java.lang.String className, JNDILookupHelper.JndiType jndiType)
          Generates a call to JNDILookupHelper class to get the java:comp/env name requested.
private  void callMethodJndiEnv(java.lang.String jndiName, org.objectweb.asm.Type type, org.objectweb.asm.MethodVisitor mv, MethodAnnotationMetadata methodMetaData, java.lang.String className, JNDILookupHelper.JndiType jndiType)
          Generates a call to JNDILookupHelper class to get the java:comp/env name requested.
private  void callSetterMethod(org.objectweb.asm.MethodVisitor mv, java.lang.String className, MethodAnnotationMetadata methodMetaData, org.objectweb.asm.Type type)
          Sets the value of a field.
private  void generateAttributesInjection(org.objectweb.asm.MethodVisitor mv)
          Generates the calls to methods that will set the attributes value.
private  void generateBodyInjectedMethod(org.objectweb.asm.MethodVisitor mv)
          Generates the body of the injectedByEasyBeans() method if any.
Else, do nothing.
private  void generateClassInjection(org.objectweb.asm.MethodVisitor mv)
          Generates the calls to populate ENC environment by using annotations on the class itself.
private  void generateSettersInjection(org.objectweb.asm.MethodVisitor mv)
          Generates the calls to methods that will call the setters methods.
private  java.lang.String getJndiName(java.lang.String jndiName, FieldAnnotationMetadata fieldAnnotationMetadata)
          Ensures that jndiName is valid and return the default value if it is a null value.
private  java.lang.String getJndiName(java.lang.String jndiName, MethodAnnotationMetadata methodMetaData)
          Ensures that jndiName is valid and return the default value if it is a null value.
private  boolean isEnvEntry(org.objectweb.asm.Type type)
          Return true if the given type is a type used in env-entry.
private  int setField()
           
private  void setField(org.objectweb.asm.MethodVisitor mv, java.lang.String className, FieldAnnotationMetadata fieldMetaData, org.objectweb.asm.Type type)
          Sets the value of a field.
private  void validateAccessFieldAnnotation(FieldAnnotationMetadata field)
          Check that the annotation is not used on a final field or static field (except client mode).
private  void validateAccessMethodAnnotation(MethodAnnotationMetadata methodData)
          Check that the annotation is not used on a final field or static field (except client mode).
private  org.objectweb.asm.Type validateSetterMethod(MethodAnnotationMetadata methodMetaData)
          Ensure that this method is a valid setter method and return ASM type of the first arg of the method.
 void visitEnd()
          Visits the end of the class.
 
Methods inherited from class org.objectweb.asm.ClassAdapter
visit, visitAnnotation, visitAttribute, visitField, visitInnerClass, visitMethod, visitOuterClass, visitSource
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

logger

private static JLog logger
Logger.


classAnnotationMetadata

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


map

private java.util.Map<java.lang.String,java.lang.Object> map
Map containing informations for enhancers.


staticMode

private boolean staticMode
Is that generated method is static (client case).


SESSION_CONTEXT

private static final java.lang.String SESSION_CONTEXT
javax.ejb.SessionContext interface.


ORB_ITF

private static final java.lang.String ORB_ITF
org.omg.CORBA.ORB interface.


DATASOURCE_ITF

private static final java.lang.String DATASOURCE_ITF
javax.sql.DataSource interface.


USERTRANSACTION_ITF

private static final java.lang.String USERTRANSACTION_ITF
javax.transaction.UserTransaction interface.


ENTITYMANAGER_ITF

private static final java.lang.String ENTITYMANAGER_ITF
Entity Manager interface.


ENTITYMANAGERFACTORY_ITF

private static final java.lang.String ENTITYMANAGERFACTORY_ITF
Entity Manager Factory interface.


TIMERSERVICE_ITF

private static final java.lang.String TIMERSERVICE_ITF
javax.ejb.TimerService interface.


EZB_SESSIONCONTEXT_DESC

private static final java.lang.String EZB_SESSIONCONTEXT_DESC
EasyBeansSessionContext type descriptor.


JAVA_LANG_OBJECT

public static final java.lang.String JAVA_LANG_OBJECT
Defines java.lang.Object class.

See Also:
Constant Field Values

INJECTED_METHOD

public static final java.lang.String INJECTED_METHOD
Injected method name.

See Also:
Constant Field Values

INJECTED_JMETHOD

public static final JMethod INJECTED_JMETHOD
JMethod object for injectedByEasyBeans.


INJECTED_METHODS

public static final java.lang.String[] INJECTED_METHODS
List of injected methods.


LENGTH

private static final int LENGTH
Replace length to create default JNDI names.

See Also:
Constant Field Values
Constructor Detail

InjectionClassAdapter

public InjectionClassAdapter(ClassAnnotationMetadata classAnnotationMetadata,
                             org.objectweb.asm.ClassVisitor cv,
                             java.util.Map<java.lang.String,java.lang.Object> map,
                             boolean staticMode)
Constructor.

Parameters:
classAnnotationMetadata - object containing all attributes of the class
cv - the class visitor to which this adapter must delegate calls.
map - a map allowing to give some objects to the adapter.
staticMode - - Is that generated method is static (client case).
Method Detail

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

addDefaultMethods

private void addDefaultMethods()
Generated methods allowing to set a context and a factory. This allows to set on injectors the bean's session context and its factory.


addInjectedMethod

private void addInjectedMethod()
Generates the injectedByEasyBeans() method on the current class.


generateBodyInjectedMethod

private void generateBodyInjectedMethod(org.objectweb.asm.MethodVisitor mv)
Generates the body of the injectedByEasyBeans() method if any.
Else, do nothing.

Parameters:
mv - the method visitor object used to add some code.

generateClassInjection

private void generateClassInjection(org.objectweb.asm.MethodVisitor mv)
Generates the calls to populate ENC environment by using annotations on the class itself.

Parameters:
mv - the method visitor used to inject bytecode.

generateAttributesInjection

private void generateAttributesInjection(org.objectweb.asm.MethodVisitor mv)
Generates the calls to methods that will set the attributes value.

Parameters:
mv - the method visitor used to inject bytecode.

generateSettersInjection

private void generateSettersInjection(org.objectweb.asm.MethodVisitor mv)
Generates the calls to methods that will call the setters methods.

Parameters:
mv - the method visitor used to inject bytecode.

validateSetterMethod

private org.objectweb.asm.Type validateSetterMethod(MethodAnnotationMetadata methodMetaData)
Ensure that this method is a valid setter method and return ASM type of the first arg of the method.

Parameters:
methodMetaData - the metadata to check
Returns:
ASM type of the first arg of the method.

isEnvEntry

private boolean isEnvEntry(org.objectweb.asm.Type type)
Return true if the given type is a type used in env-entry.

Parameters:
type - an ASM type.
Returns:
true if this entry is used in env-entry.

addCallGetEasyBeansContext

private void addCallGetEasyBeansContext(org.objectweb.asm.MethodVisitor mv)
Add a call to getEasyBeansSessionContext() method in the given method visitor.

Parameters:
mv - the method visitor on which instructions are added

addCallEntityManagerHelper

private void addCallEntityManagerHelper(JavaxPersistenceContext javaxPersistenceContext,
                                        org.objectweb.asm.MethodVisitor mv)
Add a call to EntityManagerHelper class (PersistenceContext) : EntityManagerHelper.getEntityManager(getEasyBeansSessionContext(), unitName, type of persistence).

Parameters:
javaxPersistenceContext - informations on the persistence context
mv - the method visitor on which instructions are added

addCallEntityManagerFactoryHelper

private void addCallEntityManagerFactoryHelper(JavaxPersistenceUnit javaxPersistenceUnit,
                                               org.objectweb.asm.MethodVisitor mv)
Add a call to EntityManagerHelper class (PersistenceUnit): EntityManagerHelper.getEntityManagerFactory(getEasyBeansSessionContext(), unitName).

Parameters:
javaxPersistenceUnit - informations on the persistence unit
mv - the method visitor on which instructions are added

callJndi

private void callJndi(java.lang.String jndiName,
                      org.objectweb.asm.Type type,
                      org.objectweb.asm.MethodVisitor mv,
                      java.lang.String className,
                      JNDILookupHelper.JndiType jndiType)
Generates a call to JNDILookupHelper class to get the java:comp/env name requested.

Parameters:
jndiName - the name to lookup.
type - the ASM type
mv - the method visitor to write code.
className - the name of the generated class.
jndiType - the type of access (registry, java:comp/env, etc)

callAttributeJndi

private void callAttributeJndi(java.lang.String jndiName,
                               org.objectweb.asm.Type type,
                               org.objectweb.asm.MethodVisitor mv,
                               FieldAnnotationMetadata fieldMetaData,
                               java.lang.String className,
                               JNDILookupHelper.JndiType jndiType)
Generates a call to JNDILookupHelper class to get the java:comp/env name requested.

Parameters:
jndiName - the name to lookup.
type - the ASM type.
mv - the method visitor to write code.
fieldMetaData - the metadata of the attribute.
className - the name of the generated class.
jndiType - the type of access (registry, java:comp/env, etc)

setField

private void setField(org.objectweb.asm.MethodVisitor mv,
                      java.lang.String className,
                      FieldAnnotationMetadata fieldMetaData,
                      org.objectweb.asm.Type type)
Sets the value of a field.

Parameters:
mv - the method visitor to use
className - the class which contains the attribute.
fieldMetaData - the metadata corresponding to the attribute
type - the ASM type of the attribute

callSetterMethod

private void callSetterMethod(org.objectweb.asm.MethodVisitor mv,
                              java.lang.String className,
                              MethodAnnotationMetadata methodMetaData,
                              org.objectweb.asm.Type type)
Sets the value of a field.

Parameters:
mv - the method visitor to use
className - the class which contains the method.
methodMetaData - the metadata corresponding to the method *
type - the ASM type of the method

callMethodJndiEnv

private void callMethodJndiEnv(java.lang.String jndiName,
                               org.objectweb.asm.Type type,
                               org.objectweb.asm.MethodVisitor mv,
                               MethodAnnotationMetadata methodMetaData,
                               java.lang.String className,
                               JNDILookupHelper.JndiType jndiType)
Generates a call to JNDILookupHelper class to get the java:comp/env name requested.

Parameters:
jndiName - the name to lookup.
type - the ASM type.
mv - the method visitor to write code.
methodMetaData - the metadata of the method.
className - the name of the generated class.
jndiType - the type of access (registry, java:comp/env, etc)

getJndiName

private java.lang.String getJndiName(java.lang.String jndiName,
                                     FieldAnnotationMetadata fieldAnnotationMetadata)
Ensures that jndiName is valid and return the default value if it is a null value.

Parameters:
jndiName - to check
fieldAnnotationMetadata - attribute's metadata
Returns:
jndi name value.

getJndiName

private java.lang.String getJndiName(java.lang.String jndiName,
                                     MethodAnnotationMetadata methodMetaData)
Ensures that jndiName is valid and return the default value if it is a null value.

Parameters:
jndiName - to check
methodMetaData - attribute's metadata
Returns:
jndi name value.

callBindAttributeJndi

private void callBindAttributeJndi(java.lang.String encName,
                                   java.lang.String jndiName,
                                   org.objectweb.asm.MethodVisitor mv,
                                   FieldAnnotationMetadata fieldMetaData)
Generates a call to JNDIBinderHelper class to bind an object into the java:comp/env context.

Parameters:
encName - the name to bind
jndiName - the name to link to (LinkRef)
mv - the method visitor to write code.
fieldMetaData - the metadata of the attribute (that will be bound)

callBindLookupJndiRef

private void callBindLookupJndiRef(java.lang.String encName,
                                   java.lang.String jndiName,
                                   org.objectweb.asm.MethodVisitor mv)
Generates a call to JNDIBinderHelper class to link an object into the java:comp/env context.

Parameters:
encName - the name to bind
jndiName - the jndi name to use for the link
mv - the method visitor to write code.

bindClassEJB

private void bindClassEJB(JEjbEJB jEJB,
                          org.objectweb.asm.MethodVisitor mv)
Bind a ref for an EJB in ENC environment.

Parameters:
jEJB - annotation to analyze
mv - the visitor on which write the bytecode.

bindResource

private void bindResource(JAnnotationResource jAnnotationResource,
                          org.objectweb.asm.MethodVisitor mv)
Bind a ref for a Resource in ENC environment.

Parameters:
jAnnotationResource - annotation to analyze
mv - the visitor on which write the bytecode.

bindClassPersistenceContext

private void bindClassPersistenceContext(JavaxPersistenceContext javaxPersistenceContext,
                                         org.objectweb.asm.MethodVisitor mv)
Bind a ref for a PersistenceContext in ENC environment.

Parameters:
javaxPersistenceContext - annotation to analyze
mv - the visitor on which write the bytecode.

bindClassPersistenceUnit

private void bindClassPersistenceUnit(JavaxPersistenceUnit javaxPersistenceUnit,
                                      org.objectweb.asm.MethodVisitor mv)
Bind a ref for a PersistenceUnit in ENC environment.

Parameters:
javaxPersistenceUnit - annotation to analyze
mv - the visitor on which write the bytecode.

setField

private int setField()
Returns:
the opCode used for non-static or static mode.

validateAccessFieldAnnotation

private void validateAccessFieldAnnotation(FieldAnnotationMetadata field)
Check that the annotation is not used on a final field or static field (except client mode).

Parameters:
field - the attribute to check.

validateAccessMethodAnnotation

private void validateAccessMethodAnnotation(MethodAnnotationMetadata methodData)
Check that the annotation is not used on a final field or static field (except client mode).

Parameters:
methodData - the method to check.

accessTest

private boolean accessTest(int access,
                           int checkedAccess)
Parameters:
access - the full access modified to check
checkedAccess - the access to check
Returns:
true if it is ok, else false.