|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
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 |
---|
static final int IN
here
for more details.
static final int OUT
here
for more details.
static final int IN_OUT
here
for more details.
Method Detail |
---|
int init(InterceptorClassGenerator icg)
icg
- the interceptor class generator to which this code generator
belongs.
IN
,
OUT
or IN_OUT
.void generateInitCode(MethodVisitor cv) throws ClassGenerationException
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.
cv
- the method visitor to be used to generate the initialization
code.
ClassGenerationException
- if a problem occurs.MethodVisitor generateInterceptionCode(Method m, MethodVisitor cv) throws ClassGenerationException
m
- the method for which the interception code must be generated.cv
- the method visitor to be used to generate the interception code.
CodeAdapter
, that should add the
interception code to the visited code on the fly. See CodeGenerator
.
ClassGenerationException
- if a problem occurs.void generateCloneCode(MethodVisitor cv) throws ClassGenerationException
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.
cv
- the method visitor to be used to generate the cloning code.
ClassGenerationException
- if a problem occurs.void close()
List getImplementedInterfaces() throws ClassGenerationException
InterceptorClassGenerator.getImplementedInterfaces()
.
This method should return an empty list when no additional interfaces are required.
ClassGenerationException
- if a problem occurs.
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |