Version 0.2 is ready to hit the market. Following are the improvements that have been made:
- The weaving process is 10 times faster.
- The overall structure has been improved.
- You can easily extend the framework with custom Aspects, Advices, Pointcuts and JoinPoints.
- The weaving algorithm has been sourced out into a Weaver.
- It is possible to directly weave into an object without affecting the underlying class.
- Advices do not have to be classes anymore, a method and some further information is all you need (example below).
To show you the differences we are going to compare the Version 0.1 Example with the following example, that does exactly the same.
Our basis is again the class Account.
-
import org.as2lib.core.BasicClass;
-
import InsufficientBalanceException;
-
-
/**
-
* @author Simon Wacker
-
*/
-
class Account extends BasicClass {
-
private var balance:Number;
-
-
public function Account(Void) {
-
balance = 0;
-
}
-
-
public function credit(amount:Number):Number {
-
setBalance(balance + amount);
-
return balance;
-
}
-
public function debit(amount:Number):Number {
-
if ((balance - amount) < 0) {
-
throw new InsufficientBalanceException("Balance [" + balance + "] is not sufficient.", this, arguments);
-
}
-
setBalance(balance - amount);
-
return balance;
-
}
-
public function getBalance(Void):Number {
-
return balance;
-
}
-
public function setBalance(newBalance:Number):Void {
-
balance = newBalance;
-
}
-
}
What this class does is debiting or crediting a specific amount. The debit() operation throws an InsufficientBalanceException in case the balance would be negative after debiting the amount.
-
import org.as2lib.env.except.Exception;
-
-
/**
-
* @author Simon Wacker
-
*/
-
class InsufficientBalanceException extends Exception {
-
public function InsufficientBalanceException(message:String, thrower, args:FunctionArguments) {
-
super(message, thrower, args);
-
}
-
}
As you can see these two classes are exactly the same as in the Version 0.1 Example.
The things changed are in the AO code. Following is the aspect.
-
import org.as2lib.aop.Aspect;
-
import org.as2lib.aop.aspect.AbstractAspect;
-
import org.as2lib.aop.advice.AbstractAdvice;
-
import org.as2lib.aop.JoinPoint;
-
import org.as2lib.env.except.Throwable;
-
-
/**
-
* @author Simon Wacker
-
*/
-
class LoggingAspect extends AbstractAspect implements Aspect {
-
public function LoggingAspect(Void) {
-
addAdvice(AbstractAdvice.TYPE_AROUND, getLoggedOperationsPointcut(), aroundLoggedOperationsAdvice);
-
}
-
-
private function getLoggedOperationsPointcut(Void):String {
-
return "execution(Account.debit()) || execution(Account.credit())";
-
}
-
-
private function aroundLoggedOperationsAdvice(joinPoint:JoinPoint, args:FunctionArguments) {
-
trace(joinPoint.getInfo().getDeclaringType().getName() + "." + joinPoint.getInfo().getName() + "(" + args + ")");
-
trace("Before: " + Account(joinPoint.getThis()).getBalance());
-
var result;
-
try {
-
result = joinPoint.proceed(args);
-
} catch (exception:InsufficientBalanceException) {
-
trace(joinPoint.getInfo() + " throwed: " + exception.getClass().getFullName());
-
}
-
trace("After: " + Account(joinPoint.getThis()).getBalance());
-
trace("——————————————");
-
return result;
-
}
-
}
When you compare this aspect with the one from Version 0.1 Example the first thing you probably recognize is the new operation aroundLoggedOperationsAdvice(). This operation contains exactly the programming logic that was in the AroundLoggedOperationsAdvice.execute() operation before. The AroundLoggedOperationsAdvice is now redundant and can be removed.
What has also changed is the way the advice is being added to the aspect. The first argument in the addAdvice() operation spcifies what type of advice it is: AbstractAspect.TYPE_AROUND. The second argument is the pointcut represented by a string and the third the method to be executed at a captured join point. It is of cource nevertheless possible to do it the old school way.
One little thing that has also changed is the method calls that have to be made to weave the whole thing. This is because the aspect is not responsible for weaving anymore but a seperate weaver.
-
import org.as2lib.aop.Aspect;
-
import org.as2lib.aop.Weaver;
-
import org.as2lib.aop.weaver.SimpleWeaver;
-
-
var weaver:Weaver = new SimpleWeaver();
-
weaver.addAspect(new LoggingAspect(), [Account]);
-
weaver.weave();
We first create a new weaver and add an aspect to it. The first argument in the Weaver.addAspect() operation is the aspect and the second is an array containing the affected types, that means the types that shall be considered while weaving this aspect. The last call to the Weaver.weave() operation weaves all added aspects.
And that’s it. If you now execute the following example code:
-
var account:Account = new Account();
-
account.credit(1000);
-
account.debit(500);
-
account.debit(520);
-
account.debit(480);
you will get the following output:
-
Account.credit(1000)
-
Before: 0
-
After: 1000
-
——————————————
-
Account.debit(500)
-
Before: 1000
-
After: 500
-
——————————————
-
Account.debit(520)
-
Before: 500
-
Account.debit() throwed: InsufficientBalanceException
-
After: 500
-
——————————————
-
Account.debit(480)
-
Before: 500
-
After: 20
-
——————————————
Download the AOP Framework for Flash Version 0.2 (This is actually a CVS snapshot of the as2lib. The AOP framework is in the org.as2lib.aop package.).
Downlaod the Example.

3 Responses to “AOP: AOP Framework for Flash Version 0.2”
This AOP Framework is really a great work!
What I am now developing is based on another wonderful work of yours, as2lib, and am thinking about using AOP to add some new functions to the existent job.
But last time, when I changed from as2lib_alpha_1.0 to as2lib_reviewed package, there are problems caused by the change of interface of the org.as2lib.util.Call class, which took me some time to find out because of flash’s very unattractive characteristic of neglecting errors rather than showing them.
So I was wondering if I can still use the reviewed as2lib package while using your new aop package. I tested your example, it worked just fine with reviewed package, but am not sure if there is any other reason that the reviewed package should not be used.
Nice to hear that somone is using the frameworks.
The AOP Version 0.2 framework should also work with the as2lib reviewed package. Updating from the as2lib alpha to the reviewd version caused problems, because we changed interfaces and namings.
Changes in the as2lib snapshot from 25 July 04 are mainly internal. Improved performance and all that stuff. Thus I would recommend updating, but it should not be necessary.
If you nevertheless encounter any problems without updating please let me know.
Sure, we are using the frameworks!!
Right click, save file as…..