Introduction

You are currently browsing the weblog archives for July, 2004.

Archive for July, 2004

Sunday, July 25th, 2004

AOP: AOP Framework for Flash Version 0.2

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.

  1. import org.as2lib.core.BasicClass;
  2. import InsufficientBalanceException;
  3.  
  4. /**
  5. * @author Simon Wacker
  6. */
  7. class Account extends BasicClass {
  8.   private var balance:Number;
  9.  
  10.   public function Account(Void) {
  11.     balance = 0;
  12.   }
  13.  
  14.   public function credit(amount:Number):Number {
  15.     setBalance(balance + amount);
  16.     return balance;
  17.   }
  18.   public function debit(amount:Number):Number {
  19.     if ((balance - amount) < 0) {
  20.       throw new InsufficientBalanceException("Balance [" + balance + "] is not sufficient.", this, arguments);
  21.     }
  22.     setBalance(balance - amount);
  23.     return balance;
  24.   }
  25.   public function getBalance(Void):Number {
  26.     return balance;
  27.   }
  28.   public function setBalance(newBalance:Number):Void {
  29.     balance = newBalance;
  30.   }
  31. }

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.

  1. import org.as2lib.env.except.Exception;
  2.  
  3. /**
  4. * @author Simon Wacker
  5. */
  6. class InsufficientBalanceException extends Exception {
  7.   public function InsufficientBalanceException(message:String, thrower, args:FunctionArguments) {
  8.     super(message, thrower, args);
  9.   }
  10. }

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.

  1. import org.as2lib.aop.Aspect;
  2. import org.as2lib.aop.aspect.AbstractAspect;
  3. import org.as2lib.aop.advice.AbstractAdvice;
  4. import org.as2lib.aop.JoinPoint;
  5. import org.as2lib.env.except.Throwable;
  6.  
  7. /**
  8. * @author Simon Wacker
  9. */
  10. class LoggingAspect extends AbstractAspect implements Aspect {
  11.   public function LoggingAspect(Void) {
  12.     addAdvice(AbstractAdvice.TYPE_AROUND, getLoggedOperationsPointcut(), aroundLoggedOperationsAdvice);
  13.   }
  14.  
  15.   private function getLoggedOperationsPointcut(Void):String {
  16.     return "execution(Account.debit()) || execution(Account.credit())";
  17.   }
  18.  
  19.   private function aroundLoggedOperationsAdvice(joinPoint:JoinPoint, args:FunctionArguments) {
  20.     trace(joinPoint.getInfo().getDeclaringType().getName() + "." + joinPoint.getInfo().getName() + "(" + args + ")");
  21.     trace("Before: " + Account(joinPoint.getThis()).getBalance());
  22.     var result;
  23.     try {
  24.       result = joinPoint.proceed(args);
  25.     } catch (exception:InsufficientBalanceException) {
  26.       trace(joinPoint.getInfo() + " throwed: " + exception.getClass().getFullName());
  27.     }
  28.     trace("After: " + Account(joinPoint.getThis()).getBalance());
  29.     trace("——————————————");
  30.     return result;
  31.   }
  32. }

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.

  1. import org.as2lib.aop.Aspect;
  2. import org.as2lib.aop.Weaver;
  3. import org.as2lib.aop.weaver.SimpleWeaver;
  4.  
  5. var weaver:Weaver = new SimpleWeaver();
  6. weaver.addAspect(new LoggingAspect(), [Account]);
  7. 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:

  1. var account:Account = new Account();
  2. account.credit(1000);
  3. account.debit(500);
  4. account.debit(520);
  5. account.debit(480);

you will get the following output:

  1. Account.credit(1000)
  2. Before: 0
  3. After: 1000
  4. ——————————————
  5. Account.debit(500)
  6. Before: 1000
  7. After: 500
  8. ——————————————
  9. Account.debit(520)
  10. Before: 500
  11. Account.debit() throwed: InsufficientBalanceException
  12. After: 500
  13. ——————————————
  14. Account.debit(480)
  15. Before: 500
  16. After: 20
  17. ——————————————

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.