AOP in ActionScript 3 is really hard. All aproaches have their problems:
- proxying: actually works fine and transparent to the programmer, but not to the runtime. If any function signature expects an object of type A, then a proxy to an object of type A will fail to pass the typecheck
- bytecode generation: well, sort of works, but not too good. loading bytecode in AVM2 is asynchronous, so you can't do it just in time. Also, once bytecode is loaded, there's no way of discarding it, so you can not modify a class a second time.
- anything else is verbous
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:
public function foo(param1:Type1):Type2 { /*body*/ }
public dynamic function bar(param1:Type1):Type2 { /*body*/ }
Is the equivalent of the following AS3 code:
public function foo(param1:Type1):Type2 { /*body*/ }
public var bar:Function = function (param1:Type1):Type2 { /*body*/ }
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:
public var foo(get_foo, set_foo):Type;
dynamic function get_foo() {//haXe will automatically infer types: http://haxe.org/ref/type_infer
return this.foo;
}
dynamic function set_foo(param) {
return this.foo = param;
}
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.
hope that helps
greetz
back2dos