|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object org.objectweb.fractal.julia.asm.AbstractCodeGenerator org.objectweb.fractal.julia.asm.SimpleCodeGenerator
public abstract class SimpleCodeGenerator
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:
getControllerInterfaceName
method.getPreMethodName
and getPostMethodName
methods. These methods must be defined
in the C class. They can also be declared in the T
interface, but this is not mandatory.getMethodName
method.getContextType
method. It can be a primitive type, an object type, or void
if no context is required. If a context is used, then the prem and
postm methods defined in C must have the following
signatures (where I is the type of the context): I prem
(String s) and void postm (String s, I ctxt).needsInterfaceName
method, so that it returns true. The prem
and postm methods must then have an additional parameter of type
String: I prem (String itf, String s) and void postm
(String itf, String s, I ctxt).needsTargetObject
method, so that it returns true. The prem
and postm methods must then have an additional parameter of type
Object: I prem (Object target, String s) and void postm
(Object target, String s, I ctxt) (if the previous option is also
enabled, the parameters must be placed in the following order: I prem
(String itf, Object target, String s) and void postm (String itf,
Object target, String s, I ctxt)).getInterceptionType
method can be
overriden to choose between three type of pre/post semantics. See AbstractCodeGenerator
.intercept
method can be overriden to intercept
only some specific methods, while leaving the other unchanged.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.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 |
---|
public SimpleCodeGenerator()
Method Detail |
---|
public void initialize(Tree args)
Initializable
initialize
in interface Initializable
args
- the arguments to be used to initialize this object. The format
of these arguments depends on the class of this object.public int init(InterceptorClassGenerator icg)
CodeGenerator
init
in interface CodeGenerator
init
in class AbstractCodeGenerator
icg
- the interceptor class generator to which this code generator
belongs.
IN
,
OUT
or IN_OUT
.public void generateInitCode(MethodVisitor cv) throws ClassGenerationException
CodeGenerator
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.
generateInitCode
in interface CodeGenerator
generateInitCode
in class AbstractCodeGenerator
cv
- the method visitor to be used to generate the initialization
code.
ClassGenerationException
- if a problem occurs.protected int getInterceptionCodeFormals(Method m)
AbstractCodeGenerator
getInterceptionCodeFormals
in class AbstractCodeGenerator
m
- a method object.
public void generateCloneCode(MethodVisitor cv)
CodeGenerator
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.
generateCloneCode
in interface CodeGenerator
generateCloneCode
in class AbstractCodeGenerator
cv
- the method visitor to be used to generate the cloning code.protected void generateInterceptionCodeBlock(Method m, boolean pre, MethodVisitor cv, int formals)
AbstractCodeGenerator
generateInterceptionCodeBlock
in class AbstractCodeGenerator
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.protected abstract String getControllerInterfaceName()
protected abstract String getPreMethodName()
protected abstract String getPostMethodName()
protected abstract Class getContextType()
protected abstract String getMethodName(Method m)
m
- a method object.
protected boolean needsInterfaceName()
protected boolean needsTargetObject()
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |