org.objectweb.fractal.julia.asm
Class SimpleCodeGenerator

java.lang.Object
  extended by org.objectweb.fractal.julia.asm.AbstractCodeGenerator
      extended by org.objectweb.fractal.julia.asm.SimpleCodeGenerator
All Implemented Interfaces:
Opcodes, CodeGenerator, Initializable

public abstract class SimpleCodeGenerator
extends AbstractCodeGenerator
implements Initializable

A CodeGenerator to generate pre and post code to call an associated controller object. This abstract code generator can be used to easily generate interceptors of the following form, without knowing the Java bytecode instructions and ASM:

Figure 1: an interceptor and an associated controller object (see also here)

where the interceptor calls a void prem (String s) method and a void postm (String s) method on the controller object before and after each intercepted method. This abstract code generator can be configured in many ways:

The code adapters returned by the generateInterceptionCode method (see CodeGenerator) transform the original methods into methods of the following form (assuming a context is used):

 method-signature {
   delegate.prem(id);
   // original method code
 }
 
if getInterceptionType returns EMPTY,

 method-signature {
   return-type result;
   I context = delegate.prem(id);
   // original method code, where returns are replaced with gotos
   delegate.postm(id, context);
   return result;
 }
 
if getInterceptionType returns NORMAL,

 method signature {
   I context = delegate.prem(id);
   try {
     // original method code
   } finally {
     delegate.postm(id, context);
   }
 }
 
if getInterceptionType returns FINALLY.

The generateInitCode method adds a delegate field to the interceptor class, and adds a code fragment of the following form to the generated generateInitCode method (recall that N can be configured with the getControllerInterfaceName method):

 delegate = (...)ic.getFcInterface(N);
 
Finally this code generator takes into account the Julia optimizations options. So, for example, if the interceptors and controller objects are merged, the delegate field is not generated, and this is used instead.


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
SimpleCodeGenerator()
           
 
Method Summary
 void generateCloneCode(MethodVisitor cv)
          Generates the cloning code for this code generator.
 void generateInitCode(MethodVisitor cv)
          Generates the initialization code for this code generator.
protected  void generateInterceptionCodeBlock(Method m, boolean pre, MethodVisitor cv, int formals)
          Generates an interception code block for the given method.
protected abstract  Class getContextType()
          Returns the type of the context to be passed from the "pre method" to the "post method".
protected abstract  String getControllerInterfaceName()
          Returns the name of the interface provided by the controller object to be associated to the interceptor.
protected  int getInterceptionCodeFormals(Method m)
          Returns the number of local variables that are used by the generated interception code.
protected abstract  String getMethodName(Method m)
          Returns the String to be passed as argument to the pre and post methods, for the given method.
protected abstract  String getPostMethodName()
          Returns the name of the method to be called on the associated controller object, after each intercepted call.
protected abstract  String getPreMethodName()
          Returns the name of the method to be called on the associated controller object, before each intercepted call.
 int init(InterceptorClassGenerator icg)
          Initializes this code generator.
 void initialize(Tree args)
          Initializes this object with the given arguments.
protected  boolean needsInterfaceName()
          Returns true if the target interface name must be passed as argument to the pre and post methods.
protected  boolean needsTargetObject()
          Returns true if the target object must be passed as argument to the pre and post methods.
 
Methods inherited from class org.objectweb.fractal.julia.asm.AbstractCodeGenerator
close, generateInterceptionCode, getImplementedInterfaces, getInterceptionType, intercept
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

SimpleCodeGenerator

public SimpleCodeGenerator()
Method Detail

initialize

public void initialize(Tree args)
Description copied from interface: Initializable
Initializes this object with the given arguments.

Specified by:
initialize in interface Initializable
Parameters:
args - the arguments to be used to initialize this object. The format of these arguments depends on the class of this object.

init

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

Specified by:
init in interface CodeGenerator
Overrides:
init in class AbstractCodeGenerator
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
Overrides:
generateInitCode in class AbstractCodeGenerator
Parameters:
cv - the method visitor to be used to generate the initialization code.
Throws:
ClassGenerationException - if a problem occurs.

getInterceptionCodeFormals

protected int getInterceptionCodeFormals(Method m)
Description copied from class: AbstractCodeGenerator
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.

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

generateCloneCode

public void generateCloneCode(MethodVisitor cv)
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
Overrides:
generateCloneCode in class AbstractCodeGenerator
Parameters:
cv - the method visitor to be used to generate the cloning code.

generateInterceptionCodeBlock

protected void generateInterceptionCodeBlock(Method m,
                                             boolean pre,
                                             MethodVisitor cv,
                                             int formals)
Description copied from class: AbstractCodeGenerator
Generates an interception code block for the given method.

Specified by:
generateInterceptionCodeBlock in class AbstractCodeGenerator
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.

getControllerInterfaceName

protected abstract String getControllerInterfaceName()
Returns the name of the interface provided by the controller object to be associated to the interceptor.

Returns:
the name of the interface provided by the controller object to be associated to the interceptor (called N in the above figure).

getPreMethodName

protected abstract String getPreMethodName()
Returns the name of the method to be called on the associated controller object, before each intercepted call.

Returns:
the name of the method to be called on the associated controller object, before each intercepted call (called prem above).

getPostMethodName

protected abstract String getPostMethodName()
Returns the name of the method to be called on the associated controller object, after each intercepted call.

Returns:
the name of the method to be called on the associated controller object, after each intercepted call (called postm above).

getContextType

protected abstract Class getContextType()
Returns the type of the context to be passed from the "pre method" to the "post method".

Returns:
the type of the context to be passed from the "pre method" to the "post method" (called I above).

getMethodName

protected abstract String getMethodName(Method m)
Returns the String to be passed as argument to the pre and post methods, for the given method.

Parameters:
m - a method object.
Returns:
the String to be passed as argument to the pre and post methods, for the given method.

needsInterfaceName

protected boolean needsInterfaceName()
Returns true if the target interface name must be passed as argument to the pre and post methods. The default implementation of this method returns false.

Returns:
true if the name of the fractal interface associated to the generated interceptor must be passed as argument in the pre and post methods.

needsTargetObject

protected boolean needsTargetObject()
Returns true if the target object must be passed as argument to the pre and post methods. The default implementation of this method returns false.

Returns:
true if the target object of intercepted method calls must be passed as argument in the pre and post methods.