org.apache.felix.ipojo.manipulation
Class MethodCreator

java.lang.Object
  extended by org.objectweb.asm.ClassAdapter
      extended by org.apache.felix.ipojo.manipulation.MethodCreator
All Implemented Interfaces:
org.objectweb.asm.ClassVisitor, org.objectweb.asm.Opcodes

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

iPOJO Class Adapter. This class adapt the visited class to link the class with the container.

Author:
Felix Project Team

Field Summary
private static java.lang.String ENTRY
          onEntry method name.
private static java.lang.String ERROR
          on Error method name.
private static java.lang.String EXIT
          onExit method name.
private static java.lang.String FIELD_FLAG_PREFIX
          Filed flag prefix.
private static java.lang.String GET
          onGet method name.
static java.lang.String IM_FIELD
          Instance Manager Field.
private  java.util.Set m_fields
          Set of fields detected in the class.
private  boolean m_foundSuitableConstructor
          Set to true when a suitable constructor is found.
private  java.util.List m_methodFlags
          List of fields injected as method flag in the class.
private  java.util.List m_methods
          List of methods contained in the class.
private  java.lang.String m_owner
          Name of the current manipulated class.
private  java.lang.String m_superclass
          Name of the super class.
private  java.util.List m_visitedMethods
          The list of methods visited during the previous analysis.
private static java.lang.String METHOD_FLAG_PREFIX
          Method flag prefix.
private static java.lang.String POJO
          POJO class.
private static java.lang.String PREFIX
          All POJO method will be renamed by using this prefix.
private static java.lang.String SET
          onSet method name.
 
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, DOUBLE, DREM, DRETURN, DSTORE, DSUB, DUP, DUP_X1, DUP_X2, DUP2, DUP2_X1, DUP2_X2, F_APPEND, F_CHOP, F_FULL, F_NEW, F_SAME, F_SAME1, F2D, F2I, F2L, FADD, FALOAD, FASTORE, FCMPG, FCMPL, FCONST_0, FCONST_1, FCONST_2, FDIV, FLOAD, FLOAT, 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, INTEGER, 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, LONG, LOOKUPSWITCH, LOR, LREM, LRETURN, LSHL, LSHR, LSTORE, LSUB, LUSHR, LXOR, MONITORENTER, MONITOREXIT, MULTIANEWARRAY, NEW, NEWARRAY, NOP, NULL, 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, TOP, UNINITIALIZED_THIS, V1_1, V1_2, V1_3, V1_4, V1_5, V1_6
 
Constructor Summary
MethodCreator(org.objectweb.asm.ClassVisitor arg0, java.util.Map fields, java.util.List methods)
          Constructor.
 
Method Summary
private  void addIMField()
          Add the instance manager field (__im).
private  void addPOJOInterface(int version, int access, java.lang.String name, java.lang.String signature, java.lang.String superName, java.lang.String[] interfaces)
          Add the POJO interface to the visited class.
private  void createArrayGetter(java.lang.String name, java.lang.String desc, org.objectweb.asm.Type type)
          Create a setter method for an array.
private  void createArraySetter(java.lang.String name, java.lang.String desc, org.objectweb.asm.Type type)
          Create a getter method for an array.
private  void createGetComponentInstanceMethod()
          Create the getComponentInstance method.
private  void createSetInstanceManagerMethod()
          Create the setter method for the __cm field.
private  void createSimpleConstructor()
          Creates a simple constructor with an instance manager in argument if no suitable constructor is found during the visit.
private  void createSimpleGetter(java.lang.String name, java.lang.String desc, org.objectweb.asm.Type type)
          Create the getter for a field.
private  void createSimpleSetter(java.lang.String name, java.lang.String desc, org.objectweb.asm.Type type)
          Create the setter method for one property.
private  void generateBCConstructor(int access, java.lang.String signature, java.lang.String[] exceptions, java.util.List annotations)
          Create a constructor to call the manipulated constructor.
private  void generateEmptyConstructor(int access, java.lang.String signature, java.lang.String[] exceptions, java.util.List annotations)
          Create a constructor to call the manipulated constructor.
private  java.lang.String generateMethodFlag(java.lang.String name, java.lang.String desc)
          Generate a method flag name.
private  void generateMethodHeader(int access, java.lang.String name, java.lang.String desc, java.lang.String signature, java.lang.String[] exceptions, java.util.List annotations)
          Generate the method header of a POJO method.
private  java.lang.String generateMethodId(java.lang.String name, java.lang.String desc)
          Generate the method id based on the given method name and method descriptor.
private  MethodDescriptor getMethodDescriptor(java.lang.String name, java.lang.String desc)
          Gets the method descriptor for the specified name and descriptor.
 void visit(int version, int access, java.lang.String name, java.lang.String signature, java.lang.String superName, java.lang.String[] interfaces)
          Visit method.
 void visitEnd()
          Visit end.
 org.objectweb.asm.FieldVisitor visitField(int access, java.lang.String name, java.lang.String desc, java.lang.String signature, java.lang.Object value)
          Visit a Field.
 org.objectweb.asm.MethodVisitor visitMethod(int access, java.lang.String name, java.lang.String desc, java.lang.String signature, java.lang.String[] exceptions)
          A method is visited.
 
Methods inherited from class org.objectweb.asm.ClassAdapter
visitAnnotation, visitAttribute, visitInnerClass, visitOuterClass, visitSource
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

IM_FIELD

public static final java.lang.String IM_FIELD
Instance Manager Field.

See Also:
Constant Field Values

PREFIX

private static final java.lang.String PREFIX
All POJO method will be renamed by using this prefix.

See Also:
Constant Field Values

POJO

private static final java.lang.String POJO
POJO class.

See Also:
Constant Field Values

FIELD_FLAG_PREFIX

private static final java.lang.String FIELD_FLAG_PREFIX
Filed flag prefix.

See Also:
Constant Field Values

METHOD_FLAG_PREFIX

private static final java.lang.String METHOD_FLAG_PREFIX
Method flag prefix.

See Also:
Constant Field Values

ENTRY

private static final java.lang.String ENTRY
onEntry method name.

See Also:
Constant Field Values

EXIT

private static final java.lang.String EXIT
onExit method name.

See Also:
Constant Field Values

ERROR

private static final java.lang.String ERROR
on Error method name.

See Also:
Constant Field Values

GET

private static final java.lang.String GET
onGet method name.

See Also:
Constant Field Values

SET

private static final java.lang.String SET
onSet method name.

See Also:
Constant Field Values

m_owner

private java.lang.String m_owner
Name of the current manipulated class.


m_fields

private java.util.Set m_fields
Set of fields detected in the class. (this set is given by the previous analysis)


m_methods

private java.util.List m_methods
List of methods contained in the class. This set contains method id.


m_methodFlags

private java.util.List m_methodFlags
List of fields injected as method flag in the class. This set contains field name generate from method id.


m_visitedMethods

private java.util.List m_visitedMethods
The list of methods visited during the previous analysis. This list allows getting annotations to move to generated method.


m_foundSuitableConstructor

private boolean m_foundSuitableConstructor
Set to true when a suitable constructor is found. If not set to true at the end of the visit, the manipulator injects a constructor.


m_superclass

private java.lang.String m_superclass
Name of the super class.

Constructor Detail

MethodCreator

public MethodCreator(org.objectweb.asm.ClassVisitor arg0,
                     java.util.Map fields,
                     java.util.List methods)
Constructor.

Parameters:
arg0 - : class visitor.
fields - : fields map detected during the previous class analysis.
methods - : the list of the detected method during the previous class analysis.
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)
Visit method. This method store the current class name. Moreover the POJO interface is added to the list of implemented interface. Then the Instance manager field is added.

Specified by:
visit in interface org.objectweb.asm.ClassVisitor
Overrides:
visit in class org.objectweb.asm.ClassAdapter
Parameters:
version - : version
access - : access flag
name - : class name
signature - : signature
superName - : parent class
interfaces - : implemented interface
See Also:
ClassAdapter.visit(int, int, java.lang.String, java.lang.String, java.lang.String, java.lang.String[])

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)
A method is visited. This method does not manipulate clinit and class$ methods. In the case of a constructor, this method will generate a constructor with the instance manager and will adapt the current constructor to call this constructor. For standard method, this method will create method header, rename the current method and adapt it.

Specified by:
visitMethod in interface org.objectweb.asm.ClassVisitor
Overrides:
visitMethod in class org.objectweb.asm.ClassAdapter
Parameters:
access - : access flag.
name - : name of the method
desc - : method descriptor
signature - : signature
exceptions - : declared exceptions.
Returns:
the MethodVisitor wich will visit the method code.
See Also:
ClassAdapter.visitMethod(int, java.lang.String, java.lang.String, java.lang.String, java.lang.String[])

getMethodDescriptor

private MethodDescriptor getMethodDescriptor(java.lang.String name,
                                             java.lang.String desc)
Gets the method descriptor for the specified name and descriptor. The method descriptor is looked inside the m_visitedMethods

Parameters:
name - the name of the method
desc - the descriptor of the method
Returns:
the method descriptor or null if not found.

visitField

public org.objectweb.asm.FieldVisitor visitField(int access,
                                                 java.lang.String name,
                                                 java.lang.String desc,
                                                 java.lang.String signature,
                                                 java.lang.Object value)
Visit a Field. This field access is replaced by an invocation to the getter method or to the setter method. (except for static field). Inject the getter and the setter method for this field.

Specified by:
visitField in interface org.objectweb.asm.ClassVisitor
Overrides:
visitField in class org.objectweb.asm.ClassAdapter
Parameters:
access - : access modifier
name - : name of the field
desc - : description of the field
signature - : signature of the field
value - : value of the field
Returns:
FieldVisitor : null
See Also:
ClassVisitor.visitField(int, java.lang.String, java.lang.String, java.lang.String, java.lang.Object)

generateEmptyConstructor

private void generateEmptyConstructor(int access,
                                      java.lang.String signature,
                                      java.lang.String[] exceptions,
                                      java.util.List annotations)
Create a constructor to call the manipulated constructor. This constructor does not have any argument. It will call the manipulated constructor with a null instance manager.

Parameters:
access - : access flag
signature - : method signature
exceptions - : declared exception
annotations - : the annotations to move to this constructor.

generateBCConstructor

private void generateBCConstructor(int access,
                                   java.lang.String signature,
                                   java.lang.String[] exceptions,
                                   java.util.List annotations)
Create a constructor to call the manipulated constructor. This constructor has one argument (the bundle context). It will call the manipulated constructor with a null instance manager.

Parameters:
access - : access flag
signature - : method signature
exceptions - : declared exception
annotations - : the annotations to move to this constructor.

generateMethodHeader

private void generateMethodHeader(int access,
                                  java.lang.String name,
                                  java.lang.String desc,
                                  java.lang.String signature,
                                  java.lang.String[] exceptions,
                                  java.util.List annotations)
Generate the method header of a POJO method. This method header encapsulate the POJO method call to signal entry exit and error to the container.

Parameters:
access - : access flag.
name - : method name.
desc - : method descriptor.
signature - : method signature.
exceptions - : declared exceptions.
annotations - : the annotations to move to this method.

generateMethodFlag

private java.lang.String generateMethodFlag(java.lang.String name,
                                            java.lang.String desc)
Generate a method flag name.

Parameters:
name - : method name.
desc - : method descriptor.
Returns:
the method flag name

generateMethodId

private java.lang.String generateMethodId(java.lang.String name,
                                          java.lang.String desc)
Generate the method id based on the given method name and method descriptor. The method Id is unique for this method and serves to create the flag field (so must follow field name Java restrictions).

Parameters:
name - : method name
desc - : method descriptor
Returns:
method ID

addIMField

private void addIMField()
Add the instance manager field (__im).


addPOJOInterface

private void addPOJOInterface(int version,
                              int access,
                              java.lang.String name,
                              java.lang.String signature,
                              java.lang.String superName,
                              java.lang.String[] interfaces)
Add the POJO interface to the visited class.

Parameters:
version - : class version
access - : class access
name - : class name
signature - : class signature
superName - : super class
interfaces - : implemented interfaces.

visitEnd

public void visitEnd()
Visit end. Create helper methods.

Specified by:
visitEnd in interface org.objectweb.asm.ClassVisitor
Overrides:
visitEnd in class org.objectweb.asm.ClassAdapter
See Also:
ClassAdapter.visitEnd()

createSimpleConstructor

private void createSimpleConstructor()
Creates a simple constructor with an instance manager in argument if no suitable constructor is found during the visit.


createSetInstanceManagerMethod

private void createSetInstanceManagerMethod()
Create the setter method for the __cm field.


createGetComponentInstanceMethod

private void createGetComponentInstanceMethod()
Create the getComponentInstance method.


createArraySetter

private void createArraySetter(java.lang.String name,
                               java.lang.String desc,
                               org.objectweb.asm.Type type)
Create a getter method for an array.

Parameters:
name - : field name
desc - : method description
type - : contained type (inside the array)

createArrayGetter

private void createArrayGetter(java.lang.String name,
                               java.lang.String desc,
                               org.objectweb.asm.Type type)
Create a setter method for an array.

Parameters:
name - : field name
desc - : method description
type - : contained type (inside the array)

createSimpleGetter

private void createSimpleGetter(java.lang.String name,
                                java.lang.String desc,
                                org.objectweb.asm.Type type)
Create the getter for a field.

Parameters:
name - : field of the dependency
desc - : description of the getter method
type - : type to return

createSimpleSetter

private void createSimpleSetter(java.lang.String name,
                                java.lang.String desc,
                                org.objectweb.asm.Type type)
Create the setter method for one property. The name of the method is _set+name of the field

Parameters:
name - : name of the field representing a property
desc - : description of the setter method
type - : type of the property


Copyright © 2006-2010 The Apache Software Foundation. All Rights Reserved.