Index
What
is Aspect Oriented Programming?
AOP examples
Xbase++
Aspect Oriented API
XppAspectEntry class
What is Aspect Oriented Programming (AOP)?
In
few words, AOP is new paradigm to extend
functionality of application, by allowing to
run some advice code around target code.
In our implementation,
advice code can be codeblock, function or special object (based on XppAspectEntry class).
Target (adviced) code can be any function (procedure) or any method
of any class. Wrapping over protected or hidden methods also possible
without any restrictions.
Advice code
have full control over target code, it can change any parameter for
target code and replace return value by own value. Advice code can
be called before, after and around of adviced
code.
For more
information about AOP concepts, see wikipedia article here.
AOP
examples
Most
popular example of AOP is logging.
The following
example show, how to log parameters and result of method checkPayment
of class Payment:
//LOG PARAMETERS EXAMPLE USING ADVICE FUNCTION
//make advice for single object
aspects():wrapMethod(oPayment,"checkPayment","logPayment")
...
Func logPayment(oAspect,lBefore)
If lBefore
?"object=",oAspect:getObject()
?" paymentPars=",oAspect:getParamA()
Else
?"paymentReturn=",oAspect:getReturn()
End
Return NIL
|
Another
good example of using AOP techniques is processing and changing parameters
of target code.
Code below
set up advice for XbpStatic:setCaption method. The custom advice object
MyCaptions change captions to uppercase on the fly.
//TRANSLATE EXAMPLE USING CUSTOM ADVICE CLASS
//create custom advice object
oAdvice:=MyCaptions():new()
//to trap calls from all objects
//delivered from XbpStatic we set lFilter to .F.
aspects():wrapMethod(XbpStatic(),"setCaption",oAdvice,.F.)
...
//custom class,
//only implementing beforeCall method
Class MyCaptions From XppAspectEntry
Exported:
//Uppercase all captions
Inline Method beforeCall()
If ::pCount()>0
//change caption to uppercase
::setParam(1,Upper(::getParam(1)))
End
Return NIL
EndClass |
Xbase++
Aspect Oriented API
aspects():wrapMethod(object|objectsList,
methodName, functionName|codeblock|oAdvice, lFilter) -> oRetAdvice
Weave
advice code over an method.
Parameter |
Description |
1. object|objectsList |
single
object or array of objects that will be adviced.
If third parameter
is oAdvice, object can be NIL. |
2. methodName |
name
of adviced method |
3. functionName
codeblock/codeblockA
oAdvice |
name
of advice function. This function receive two parameters, oAdvice and
lBefore. oAdvice is object of XppAspectEntry type
and if function called before target method, lBefore is
.T., otherwise is .F..
NOTE: Return of
advice function must be NIL.
advice block, called before adviced method or array of two blocks,
first for before action (can be NIL) and second
for after action (also can be NIL).
This
block(s) receive two parameters, oAdvice and lBefore.
NOTE: Return
of advice block must be NIL.
This object must be
delivered from XppAspectEntry class. Method ::beforeCall() called
before run of adviced method and ::afterCall()
immediately after.
For more info, see XppAspectEntry topic below. |
4. lFilter |
If
you need to contoll all objects of the same classes as object|objectsList,
pass .F.. In such cases, use oAdvice:getObject() call to determine actual adviced object.
By default (.T.), advice code only called for
objects specified by object|objectsList. |
Return
value |
Description |
oRetAdvice |
XppAspectEntry object
for managing advice |
aspects():wrapFunction(targetFunction, functionName|codeblock|oAdvice,
lFilter) -> oRetAdvice
Parameter |
Description |
1. targetFunction |
name
of adviced function. |
other
parameters the same as in wrapMethod
|
Return
value |
Description |
oRetAdvice |
XppAspectEntry object
for managing advice |
XppAspectEntry
class
This
class used by Aspect API for advicing method/function
Advice
methods automaticaly called by Aspect API
::beforeCall()
This method
called before adviced code
::afterCall()
This method
called after adviced code
::checkValid(object)
This method
called by Aspect API to determine is object valid for this
XppAspectEntry set of adviced objects.
You can implement you own logic for filtering.
NOTE: if
you set lFilter parameter for aspects():wrapMethod(..)
to .F., objects not filtered and ::checkValid()
method will be never called.
Methods
for maganing list of adviced objects
::addObjects(object|objectsList)
Add object|objectsList to
set of adviced objects by this XppAspectEntry object. Only this objects
will be adviced, and ::before/afterCall()
methods will be called.
::clearObjects()
Disable
any filtering without stopping advicing. All objects of the same
classes as specified in aspects():wrapMethod(..)
will be adviced. You can use ::getObject() call
to determine actual adviced object.
Parameters
and return methods
::PCount() ->
numParams
Returns
number of parameters passed to adviced code.
::getParamA() -> aParams
Returns
all parameters as array.
::getParam(nParam) -> paramValue
Returns
specified parameter of adviced code.
::setParam(nParam,value)
Change
parameter to new value.
::getReturn() -> returnValue
Returns
return value of adviced code.
::setReturn(returnValue)
Change
return value of adviced code.
::getObject()
Returns
currently adviced object.
Note: this call is not valid for XppAspectEntry objects returned
by aspects():wrapFunction(..) call.
Methods
related to adviced code
::disableBody()
This call
disables call to adviced code. Disabling call to body, do not disable
aspect itself, ::beforeCall() and ::afterCall() will be called anyway.
::enableBody()
Enable
call to adviced code.
©2006
Eleus Software |