Introduction

Flash is a lightweight cross-platform runtime for rich media, enterprise applications and mobile applications, as well as an integrated development environment. Flash can be programmed in ActionScript 1/2/3.

Sunday, June 6th, 2004 at 10:41 am

AOP: How to Use the Flash AOP Framework?

To show how you can use the AOP Framework for Flash, I announced in my previous post: AOP: AOP Framework for Flash Version 0.1, I have built a little example that logs the Account.debit() and Account.credit() operations using an around advice. Following is 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. }

As you can see this class is quite simple. I declares four operations, one to credit money from the Account, one to debit money, one to get the balance and one to set a new balance. The operation that is a little more complex than the other ones is the Account.debit() operation. It first checks if the balance is sufficient and if it is not the InsufficientBalanceException will be thrown.

  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. }

We now have all classes to begin writing our LoggingAspect and AroundLoggedOperationsAdvice.

  1. import org.swacker.aop.Aspect;
  2. import org.swacker.aop.aspect.AbstractAspect;
  3.  
  4. /**
  5. * @author Simon Wacker
  6. */
  7. class LoggingAspect extends AbstractAspect implements Aspect {
  8.   public function LoggingAspect(Void) {
  9.     // this ensures the the class Account is loaded when the weaving process starts
  10.     Account;
  11.     // adds the affected type ‘Account’ to search for advised join points in
  12.     addAffectedTypes("Account");
  13.     // adds the advice ‘AroundLoggedOperationsAdvice’ that advices join points it captures
  14.     addAdvice(new AroundLoggedOperationsAdvice(this));
  15.   }
  16.    
  17.   public function getLoggedOperationsPointcut(Void):String {
  18.     // the pointcut in form of a string specifying the join points to be captured
  19.     return "execution(Account.debit()) || execution(Account.credit())";
  20.   }
  21. }

What we are doing here is extending the class AbstractAspect that provides us with a few basic functionalities like the weaving algorithm, the addAdvice() and the addAffectedTypes() operation. In the constructor we add the affected type ‘Account’ and the advice ‘AroundLoggedOperationsAdvice’. When the LoggingAspect.weave() operation is called, the method will look inside the added affected types for join points that are being captured by added advices.

  1. import org.as2lib.env.except.Throwable;
  2. import org.swacker.aop.advice.AroundAdvice;
  3. import org.swacker.aop.advice.AbstractAdvice;
  4. import org.swacker.aop.JoinPoint;
  5.  
  6. /**
  7. * @author Simon Wacker
  8. */
  9. class AroundLoggedOperationsAdvice extends AbstractAdvice implements AroundAdvice {
  10.   public function AroundLoggedOperationsAdvice(aspect:LoggingAspect) {
  11.     // sets the pointcut
  12.     // this operation is declared by the class AbstractAdvice
  13.     setPointcut(aspect.getLoggedOperationsPointcut());
  14.   }
  15.    
  16.   public function execute(joinPoint:JoinPoint, args:FunctionArguments) {
  17.     // the code to be executed when a captured join point is called
  18.     trace(joinPoint.getInfo().getDeclaringType().getName() + "." + joinPoint.getInfo().getName() + "(" + args + ")");
  19.     trace("Before: " + Account(joinPoint.getThis()).getBalance());
  20.     var result;
  21.     try {
  22.       result = joinPoint.proceed(args);
  23.     } catch (exception:InsufficientBalanceException) {
  24.       trace(joinPoint.getInfo() + " throwed: " + exception.getClass().getFullName());
  25.     }
  26.     trace("After: " + Account(joinPoint.getThis()).getBalance());
  27.     trace("——————————————");
  28.     return result;
  29.   }
  30. }

We are extending the AbstractAdvice that like the AbstractAspect offers needed operations like setPointcut() and an implementation of the captures() operation. We add the captured pointcut in the constructor. Then we implement the execute() operation that will be executed when any captured join point is being called. Because we are working with an around advice we have to call the proceed() operation on the join point. Otherwise the execution of the join point will be bypassed.
Now let’s write a simple test to look if it works.

  1. import org.swacker.aop.Aspect;
  2.  
  3. var aspect:Aspect = new LoggingAspect();
  4. aspect.weave();
  5.  
  6. var account:Account = new Account();
  7. account.credit(1000);
  8. account.debit(500);
  9. account.debit(520);
  10. account.debit(480);

The output should look like this:

  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. ——————————————

That’s it. Easy isn’t it? ;) Download the source code of the example.