Difference between revisions of "Understanding AspectJ"
m (Protected "Understanding AspectJ": Sysop page [edit=sysop:move=sysop]) |
|||
Line 1: | Line 1: | ||
+ | '''IMPORTANT''' In newer versions of opentaps, aspectWerkz has replaced aspectJ. See [[Aspect Oriented programming and opentaps]] | ||
+ | |||
* Some theory: | * Some theory: | ||
Revision as of 00:52, 21 July 2009
IMPORTANT In newer versions of opentaps, aspectWerkz has replaced aspectJ. See Aspect Oriented programming and opentaps
- Some theory:
First you have to create a file with your aspect. We are not talking anymore about class, but aspect. Actually, it use a specific compiler. (Compiler errors message are not really understandable in my mind... You can activate the options verbose and showWaveInfo to have some more info.)
AOP is a way to add code to several points of the project, based on method selections or pointcut.
Creation of a poincut:
pointcut testRunContainerStart() : execution(public boolean org.ofbiz.testtools.TestRunContainer.start());
You can use wildcard: *, +, ..
* replace a name or part of the name
public * fr.umlv.*.test.*.start*(int, String, *)
+ used to not tell the name of the parameter of the method or the name of the package
public void fr..Test.SetParams(..) all the public method of SetParam whatever are their parameters, returning void and available in the classes Test which are wherever in the package fr
.. used to define whatever sub-type of a class or interface.
void fr.umlv.test.IMouseListener+.set*(..) all the methods which begins with set in the classes implementing the interface IMouseListener.
- Some Practical comments:
In my case I almost always give the full method name with the argument. Because ofbiz and opentaps project are big projects, side effects could happen in case of not specifing the full method name I want to work on.
- Let's go back to theory:
You can define the poincut on various events: call, execution, get, set, handler, initialization, preinitialization, staticinitialization, adviceexecution
call call of the method
execution execution of the method.
- What is the difference between call and execution?
Mainly it is about heritance. In the case of call we are working with the reference which is used in the program (it can be the interface). In the case of execution we are working on the object which is instanciated.
There is more differences.
In case of a call, the compiler modifies the invoking code. In case of an execution, the compiler modifies the target code.
It's made problematic crosslibrary using of "call", if the execution used around pointcut is inlined in target method.
I.e.
someMethod() { Debug.logInfo("Enter method"); Debug.logInfo("Leave method") } execution(someMethod) around(someAdvice) { Debug.logInfo("Enter advice"); proceed() Debug.logInfo("Leave advice") } compiler will produce someMethod { Debug.logInfo("Enter advice"); Debug.logInfo("Enter method"); Debug.logInfo("Leave method") Debug.logInfo("Leave advice") }
A bit crazy:)
This can bring a problem with debugging and breakpoints in advises. Eclipse has special options for this case.
- During the creation of a pointcut, you can use logic and associate various call, execution with && or ||
The interesant method is args which let you access to the parameter of the method.
- When you pointcut is defined, you can realize a treatment.
before to realize the treatment before to reach the pointcut.
after to realize the treatment after to reach the pointcut.
- If the program is multithread (case of opentaps) and you have to do two treatment before and after the pointcut, you can use around and proceed. proceed will execute the code corresponding to the poincut in a around
instruction.