I played with hxasm for about 1.5 hours today. Although I didn't succeed, I have seen enough to know that it is completely possible to dynamically generate strongly-typed mocks at runtime using AS3 alone. The trick is to write the class definition to a ByteArray and then use a flash.display.Loader to load the generated code into the swf. After this, the generated class definition can be instantiated.
I see two potential paths for doing this: either bytecode can be generated that defines a class that subclasses Proxy and implements your interface, or a class that implements your interface can be constructed from the bottom up. Clearly it will involve less work to subclass Proxy, so that is the avenue that I will be attempting first. Currently the hxasm code doesn't support defining classes that implement interfaces, but I think that the necessary alterations are rather minor. Are there any as3 bytecode enthusiasts out there who want to work on this with me? I believe it would really be useful to a lot of people. I guess I should post to the hxasm list as well. Adam --- In [email protected], "aduston1976" <[EMAIL PROTECTED]> wrote: > > Jens: Indeed, we need Proxy#newProxyInstance in AS. This is how > EasyMock creates its mocks. There is an equivalent mechanism in c# > that is used by Rhino Mocks, which is similar to EasyMock insofar as > it uses the record/replay metaphor. > > Cristophe: I would love to talk about this offline. Do you use Google > Talk? I am aduston. I think that doing this in run time is a losing > proposition. I think we have to resort to compile-time code > generation. We can create an Eclipse build task and have it generate > our mocks. > > Here's to the eventual end of hand-coding mocks in AS3, > Adam > > --- In [email protected], Jens Halm <mlist@> wrote: > > > > > > > I have also been looking into runtime interface implementation to > > > create Mock objects and a basic AOP framework. I'm stuck at the > > > exact same thing as you are and the others that are working on this. > > > > > My guess is that we will need to have some kind of bytecode > > > manipulation framework to achieve this. > > > > > > I think we should turn that into a feature request for a future Flash > > Player. Of course you can start a project for a bytecode manipulation > > framework, but for some of the simpler cases it should be the Player > > API itself that should provide a hook. Just look how Java projects > > deal with this. In Spring for example they only create a CGLIB proxy > > for AOP proxies when you proxy a class that does not implement any > > interfaces. Otherwise they proxy the implemented interfaces with JDK > > Dynamic Proxies. And that exact thing is something I would like to see > > in AS4! > > > > For those of you who don't know Java: The java.lang.reflect.Proxy > > class has the following method: > > > > newProxyInstance (ClassLoader loader, Class<?>[] interfaces, > > InvocationHandler h) > > > > This will return a proxy class that implements all interfaces > > specified with the second parameter and delegates all method > > invocations to the specified InvocationHandler instance. > > > > It would really be great to have something similar in AS4! > > Typesafe proxy objects are useful for: > > > > - AOP frameworks > > - Mock frameworks > > - Remote Service Stubs > > > > and very likely for a ton of other things! > > > > > > Jens Halm > > Spicefactory > > www.spicefactory.org > > >

