org.objectweb.fractal.julia.asm
Interface CodeGenerator

All Known Implementing Classes:
AbstractCodeGenerator, LifeCycleCodeGenerator, MetaCodeGenerator, SimpleCodeGenerator, TraceCodeGenerator

public interface CodeGenerator

A code generator used by an InterceptorClassGenerator to generate interception code. The interception code for a given method is generated by using a kind of CodeAdapter: the original method code is visited by a code adapter, which adds the interception code on the fly. For example, to add pre and post code blocks to a method, the previous code adapter can be of the following form:

 public class PrePostCodeAdapter implements CodeVisitor {
   private CodeVisitor cv;
   private Label postBlockLabel;
   public PrePostCodeAdapter (CodeVisitor cv) {
     this.cv = cv;
     this.postBlockLabel = new Label();
     // GENERATES PRE CODE BLOCK using cv
   }
   public void visitInsn (int opcode) {
     // replaces xRETURN instructions with the following instructions:
     //   xSTORE result
     //   GOTO postBlockLabel
   }
   public void visitVarInsn (int opcode, int var) {
     // inserts a variable to store the result, just after the parameters
     // shifts the existing variables to make room for this new variable
   }
   public void visitMaxs (int maxStack, int maxLocals) {
     cv.visitLabel(postBlockLabel);
     // GENERATES POST CODE BLOCK using cv
     // generates appropriate xLOAD xRETURN instructions
     cv.visitMaxs(maxStack, maxLocals);
   }
   // the other CodeVisitor methods are implemented by just delegating to cv
 }
 
When the original method code is visited by this code adapter, the pre code block is generated immediately. Then the original method code is regenerated as is, except that returns are replaced with gotos. The post code block is inserted when the end of the original method code is visited (i.e., when visitMaxs is called).

The original method code can also be completely replaced with a code adapter of the following form:

 public class AroundCodeAdapter implements CodeVisitor {
   public AroundCodeAdapter (CodeVisitor cv) {
     // generates the code replacing the original method code
   }
   // the CodeVisitor methods are implemented by doing nothing
 }
 
These code adapters are returned by the generateInterceptionCode method. More precisely, given a code visitor, this method should return a code adapter that adds the desired interception code for the given method.

When several code generators are used by an interceptor class generator, the code adapters returned by the generateInterceptionCode methods of these code generators are chained together. The first code adapter in this chain visits the original code of each method. The second code adapter visits the code modified on the fly by the first code adapter. The third code adapter visits the code modified on the fly by the first and second code adapters, and so on. The result is a code that includes the interception code generated by each code generator.


Field Summary
static int IN
          The type of code generators that applies only to input interceptor classes.
static int IN_OUT
          The type of code generators that applies to input and output interceptor classes.
static int OUT
          The type of code generators that applies only to output interceptor classes.
 
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.
 List getImplementedInterfaces()
          Permits a CodeGenerator to add interfaces that must be implemented by the interceptor class being generated.
 int init(InterceptorClassGenerator icg)
          Initializes this code generator.
 

Field Detail

IN

static final int IN
The type of code generators that applies only to input interceptor classes. See here for more details.

See Also:
Constant Field Values

OUT

static final int OUT
The type of code generators that applies only to output interceptor classes. See here for more details.

See Also:
Constant Field Values

IN_OUT

static final int IN_OUT
The type of code generators that applies to input and output interceptor classes. See here for more details.

See Also:
Constant Field Values
Method Detail

init

int init(InterceptorClassGenerator icg)
Initializes this code generator.

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

void generateInitCode(MethodVisitor cv)
                      throws ClassGenerationException
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.

Parameters:
cv - the method visitor to be used to generate the initialization code.
Throws:
ClassGenerationException - if a problem occurs.

generateInterceptionCode

MethodVisitor generateInterceptionCode(Method m,
                                       MethodVisitor cv)
                                       throws ClassGenerationException
Generates the interception code for the given method.

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

void generateCloneCode(MethodVisitor cv)
                       throws ClassGenerationException
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.

Parameters:
cv - the method visitor to be used to generate the cloning code.
Throws:
ClassGenerationException - if a problem occurs.

close

void close()
Closes this code generator. This method can be used to do some cleanup before computing the bytecode of the interceptor class.


getImplementedInterfaces

List getImplementedInterfaces()
                              throws ClassGenerationException
Permits a CodeGenerator to add interfaces that must be implemented by the interceptor class being generated. This method is called from InterceptorClassGenerator.getImplementedInterfaces(). This method should return an empty list when no additional interfaces are required.

Returns:
the names of all the interfaces this CodeGenerator requires to be implemented by the generated class.
Throws:
ClassGenerationException - if a problem occurs.