org.objectweb.fractal.julia.asm
Class AbstractCodeGenerator

java.lang.Object
  extended by org.objectweb.fractal.julia.asm.AbstractCodeGenerator
All Implemented Interfaces:
Opcodes, CodeGenerator
Direct Known Subclasses:
LifeCycleCodeGenerator, SimpleCodeGenerator, TraceCodeGenerator

public abstract class AbstractCodeGenerator
extends Object
implements Opcodes, CodeGenerator

An abstract CodeGenerator to add interception code "around" methods. More precisely, depending on the result of the getInterceptionType method, the code adapters returned by the generateInterceptionCode method (see CodeGenerator) transform the original methods into methods of the following form:

 method-signature {
   // pre code block generated by 'generateInterceptionCodeBlock'
   // original method code
 }
 
if getInterceptionType returns EMPTY,

 method-signature {
   return-type result;
   // pre code block generated by 'generateInterceptionCodeBlock'
   // original method code, where returns are replaced with gotos
   // post code block generated by 'generateInterceptionCodeBlock'
   return result;
 }
 
if getInterceptionType returns NORMAL,

 method-signature {
   // pre code block generated by 'generateInterceptionCodeBlock'
   try {
     // original method code
   } finally {
     // post code block generated by 'generateInterceptionCodeBlock'
   }
 }
 
if getInterceptionType returns FINALLY.


Field Summary
 
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
 
Fields inherited from interface org.objectweb.fractal.julia.asm.CodeGenerator
IN, IN_OUT, OUT
 
Constructor Summary
AbstractCodeGenerator()
           
 
Method Summary
 void close()
          Closes this code generator.
 void generateCloneCode(MethodVisitor cv)
          Generates the cloning code for this code generator.
 void generateInitCode(MethodVisitor cv)
          Generates the initialization code for this code generator.
 MethodVisitor generateInterceptionCode(Method m, MethodVisitor cv)
          Generates the interception code for the given method.
protected abstract  void generateInterceptionCodeBlock(Method m, boolean pre, MethodVisitor cv, int formals)
          Generates an interception code block for the given method.
 List getImplementedInterfaces()
          Returns the list of interfaces to be added to be implemented by the generated interceptor.
protected  int getInterceptionCodeFormals(Method m)
          Returns the number of local variables that are used by the generated interception code.
protected  int getInterceptionType(Method m)
          Returns the type of the interception code to be generated for the given method.
 int init(InterceptorClassGenerator icg)
          Initializes this code generator.
protected  boolean intercept(Method m)
          Returns true if this code generator must generate interception code for the given method.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

AbstractCodeGenerator

public AbstractCodeGenerator()
Method Detail

init

public int init(InterceptorClassGenerator icg)
Description copied from interface: CodeGenerator
Initializes this code generator.

Specified by:
init in interface CodeGenerator
Parameters:
icg - the interceptor class generator to which this code generator belongs.
Returns:
the type of this code generator, i.e., either IN, OUT or IN_OUT.

generateInitCode

public void generateInitCode(MethodVisitor cv)
                      throws ClassGenerationException
Description copied from interface: CodeGenerator
Generates the initialization code for this code generator. This code is added to the initFcController method by the interceptor class generator that calls this method. By hypothesis, the stack is empty at the beginning of the generated code. Moreover, the stack must also be empty at the end of the code generated by this method.

Specified by:
generateInitCode in interface CodeGenerator
Parameters:
cv - the method visitor to be used to generate the initialization code.
Throws:
ClassGenerationException - if a problem occurs.

generateInterceptionCode

public MethodVisitor generateInterceptionCode(Method m,
                                              MethodVisitor cv)
                                       throws ClassGenerationException
Description copied from interface: CodeGenerator
Generates the interception code for the given method.

Specified by:
generateInterceptionCode in interface CodeGenerator
Parameters:
m - the method for which the interception code must be generated.
cv - the method visitor to be used to generate the interception code.
Returns:
a method visitor to be used to visit the original method code. This method visitor should be a kind of CodeAdapter, that should add the interception code to the visited code on the fly. See CodeGenerator.
Throws:
ClassGenerationException - if a problem occurs.

generateCloneCode

public void generateCloneCode(MethodVisitor cv)
                       throws ClassGenerationException
Description copied from interface: CodeGenerator
Generates the cloning code for this code generator. This code is added to the clone method by the interceptor class generator that calls this method. By hypothesis, the stack is empty at the beginning of the generated code. Moreover, the stack must also be empty at the end of the code generated by this method.

Specified by:
generateCloneCode in interface CodeGenerator
Parameters:
cv - the method visitor to be used to generate the cloning code.
Throws:
ClassGenerationException - if a problem occurs.

close

public void close()
Description copied from interface: CodeGenerator
Closes this code generator. This method can be used to do some cleanup before computing the bytecode of the interceptor class.

Specified by:
close in interface CodeGenerator

intercept

protected boolean intercept(Method m)
Returns true if this code generator must generate interception code for the given method. The default implementation of this method returns true for all methods.

Parameters:
m - a method object.
Returns:
true if this code generator must generate interception code for the given method, or false if the given method must be left unchanged.

getInterceptionType

protected int getInterceptionType(Method m)
Returns the type of the interception code to be generated for the given method. The default implementation of this method returns NORMAL for all methods.

Parameters:
m - a method object.
Returns:
EMPTY to generate only a pre code block at the beginning of the method, NORMAL to generate both a pre code block and a post code block, or FINALLY to generate a pre code block, and a post code block inside a finally block (to be sure that the post code block will always be executed).

getInterceptionCodeFormals

protected int getInterceptionCodeFormals(Method m)
Returns the number of local variables that are used by the generated interception code. The default implementation of this method returns 0 for all methods.

Parameters:
m - a method object.
Returns:
the number of local variables that are used by the generated interception code.

generateInterceptionCodeBlock

protected abstract void generateInterceptionCodeBlock(Method m,
                                                      boolean pre,
                                                      MethodVisitor cv,
                                                      int formals)
Generates an interception code block for the given method.

Parameters:
m - the method for which the interception code must be generated.
pre - true to generate a pre code block, or false to generate a post code block.
cv - the method visitor to be used to generate the interception code.
formals - the index of the first local variable reserved for the code block. The generated code can use the local variables between indexes formals, inclusive, and formals + getInterceptionCodeFormals(m), exclusive.

getImplementedInterfaces

public List getImplementedInterfaces()
                              throws ClassGenerationException
Returns the list of interfaces to be added to be implemented by the generated interceptor.

This method provides an empty implementation, returning an empty List.

Subclasses which need to add interfaces to be implemented by the generated interceptor should override this method.

Specified by:
getImplementedInterfaces in interface CodeGenerator
Returns:
the names of all the interfaces this CodeGenerator requires to be implemented by the generated class.
Throws:
ClassGenerationException - if a problem occurs.
See Also:
CodeGenerator.getImplementedInterfaces()