I’m looking for a full featured AOP Library for Actionscript 3.
The following projects I noticed so far, but they all seem to have their problems:
- http://farmcode.org/page/Sodality.aspx
looks most promising so far, however it requires you to create a whole new class for every AOP “call” I believe, and it forces you to follow quite a lot of restrictions, anyone has experience with it? - http://code.google.com/p/loom-as3/
this one is discontinued - http://code.google.com/p/floxy/
dynamic proxy generation? this isn’t really AOP as I know it, right? - http://code.google.com/p/flemit/
dynamic byte code generation? this is something AOP needs I think, but not the full featured AOP framework I am looking for
Does anyone know of a better solution? Or does anyone have any experiences with AOP in Actionscript 3?
Best regards,
Tom
AOP in ActionScript 3 is really hard. All aproaches have their problems:
What you can do is to use Haxe. Not only does Haxe have other advantages over AS3, but it also allows some AOP. There are two approaches:
Dynamic Methods
In Haxe, you can declare methods to be dynamic. These can be replaced at runtime to provide advices. Let me explain, what happens under the hood. The following Haxe code:
Is the equivalent of the following AS3 code:
Using the latter always performs worse than the former, since there’s a lot of runtime type checking involved. In Haxe however, you do not lose the compile time benefits of strict typing, unlike in AS3.
Likewise, using Haxe’s accessors (that are indeed very different from AS3’s), you can also use AOP for properties:
Any methods, that are dynamic can be replaced at runtime to do whatever you wish. AS2 allowed this, and libraries like as2lib provided AOP. unfortunately, it is no longer available. But I guess you can figure out the rest on you own.
Proxying
Haxe has the notion of anonymous types. At runtime, these are merely
*(thus a perfomance loss is to be expected), but at compile time, they are type safe. What you can do, is create a proxy to an object of whatever type you need and use it as AOP container. In your whole code, you never use its type explicetely, but rather an equivalent anonymous type. The problem with proxying I described will be gone. The only thing you need to do is to make an unsafe cast of your proxy to that anonymous type.