15 Replies Latest reply on Jun 25, 2009 10:15 AM by Flex harUI

    Fooling the runtime over type

    EgorKDie

      I’ve seen a similar question asked on flex coders before, but it was about 2006 so things may have moved on since then!

      I’m trying to fool the runtime into thinking one object is of a different type. The object is of type ObjectProxy so it can handle all the calls to the object it is proxying, and I’m trying to use the Proxy in place of the object it is proxying… Clear as mud?

       

      Basically…

       

      var obj : ObjectProxy = new ObjectProxy( new MyObject() );

      var newObj : * = obj;

       

      works because the newObj variable is untyped. This isn’t acceptable for a whole host of reasons (the loudest one appears to be that code hinting stops working at that point).

       

      var obj : ObjectProxy = new ObjectProxy( new MyObject() );

      var newObj : MyObject = obj;

       

      throws a Type Coercion error as it cannot convert the Proxy to the strong type.

       

      Is there any way of fooling the runtime into thinking the type of the ObjectProxy is actually that of MyObject?

      Am I actually going mad, and trying to do something foolhardy and impossible?

        • 1. Re: Fooling the runtime over type
          Flex harUI Adobe Employee

          Can't fool the runtime.  Maybe MyObject should be dynamic or extend Proxy or ObjectProxy

           

          Alex Harui

          Flex SDK Developer

          Adobe Systems Inc.

          Blog: http://blogs.adobe.com/aharui

          1 person found this helpful
          • 2. Re: Fooling the runtime over type
            EgorKDie Level 2

            Thanks Alex...

             

            My goal is to proxy the object without having to change the surrounding code at all, changing the rest of the code will partially defeat the point of having the proxy in the first place.

             

            It does seem however, that the Proxy Class is really only half a proxy. You can create a proxy for an object, but then cannot use it in place of that object.

             

            Is this worth a feature request? I'd imagine it would be a player rather than SDK request?

            • 3. Re: Fooling the runtime over type
              Flex harUI Adobe Employee

              I'm not clear how common your use case is.  I'm suggesting that Proxy be the base class for your object.  For example, ArrayCollection is based on Proxy.

               

              Alex Harui

              Flex SDK Developer

              Adobe Systems Inc.

              Blog: http://blogs.adobe.com/aharui

              • 4. Re: Fooling the runtime over type
                EgorKDie Level 2

                Its a fairly common enterprise requirement. Basically, I'm aiming for runtime AOP in Flex (similar to Spring or AspectJ's implementations).

                 

                Looking at the many, many half finished projects lying round source forge, and google code, many people have gone down the same track as me, and stumbled around the same point.

                 

                The only person that seems to be making headway on the AOP front is the guy who does Loom, and thats only through hacking up abc code prior to loading it...

                • 5. Re: Fooling the runtime over type
                  Flex harUI Adobe Employee

                  AS3 is not ready for that kind of stuff.  Maybe some future version.  There was an AS proposal for "like" that looked interesting...

                   

                  Alex Harui

                  Flex SDK Developer

                  Adobe Systems Inc.

                  Blog: http://blogs.adobe.com/aharui

                  • 6. Re: Fooling the runtime over type
                    EgorKDie Level 2

                    See, now you are just teasing

                     

                    How on earth can I google "Actionscript" and "like" and get anything meaningful!

                     

                    Go on... put it on your blog! I read that at least...

                    • 7. Re: Fooling the runtime over type
                      EgorKDie Level 2

                      Ok, using what passes for a brain in my head (half a pound of porridge and an athsmatic hamster), I went and checked out the ECMAScript 4 spec...

                       

                      Low and behold, "like" was revealed in all it's glory (along with "wrap" which also looks massively interesting).

                       

                      And now the elephant in the room...

                       

                      Is Actionscript going to continue along ECMAScript 4 lines, or is it going in its own direction?

                       

                      *Waits for Matt to weigh in with the standard party line about supporting open standards*

                      • 8. Re: Fooling the runtime over type
                        Ratsnackbar Level 2

                        How about:

                         

                        var obj : ObjectProxy = new ObjectProxy( new MyObject() );

                        var newObj : MyObject = obj as MyObject;

                         

                        Grins, Sorry could not resist.  Im sleep deprived right now and it seemed funny to me.

                         

                        Question: were you wanting this to implement AOP or a form of Reflection to use with an AOP implementation?  A more specific use case would be helpful.  Whatcha hoping to accomplish?

                         

                        -Joe

                        • 9. Re: Fooling the runtime over type
                          EgorKDie Level 2

                          I have a class such as the one below (it's a fairly representative exmple albeit simple)

                           

                           

                           

                           

                           

                           

                           

                          package com.inpses.app.business {

                           

                           

                           

                           

                          public class Model {

                           

                           

                           

                           

                          public var dao : DAO;

                           

                           

                          public function markAsNothing() : void {

                          dao.persist(

                           

                          new Object() );

                          }

                          }

                          }

                           

                           

                          The dao property is being configured by the framework when the class is created. Now sometimes (depending on a set of rules defined in XML) I want some peice of code to run before, after or instead of the dao.persist(...) call. Standard AOP would do it by creating an object which implements the DAO interface and which contains the injected code and the wrapped object to delegate the calls to once the advisors have been run.

                           

                          ObjectProxy manages the second part... the problem is making it look like the DAO type.

                          • 10. Re: Fooling the runtime over type
                            Flex harUI Adobe Employee

                            See, I knew you'd find it.

                             

                            I'm pretty sure we can't make any public statements of any detail about the future of AS, since I don't think anything has actually been decided.  AS3 is serving most of us pretty well and getting it to run efficiently on mobile is probably more important that adding more language features.

                             

                            So, I'm sure Matt will eventually say something containing the following keywords: "open standards", "performance", "community feedback", "apple pie"

                             

                            Alex Harui

                            Flex SDK Developer

                            Adobe Systems Inc.

                            Blog: http://blogs.adobe.com/aharui

                            • 11. Re: Fooling the runtime over type
                              Ratsnackbar Level 2

                              Ok I think I see what you are attempting.  You will have to forgive my disjointed answer as I have only had 4 hours of sleep in the last 48.  I may be rambling.

                               

                              I believe what you would want to do is implement something that is not exactly like but similar to a combination of the patterns "Finite State Machine", "Decorator" and a Singleton Factory.  Rather then states in the state machine though you would using IAdvice.  Somthing like:

                               

                               

                               

                               

                               

                               

                               

                              public interface IAdvice{

                               

                                   public function Before():void; // called before performing normal action

                               

                               

                                   public function InsteadOf():void; //called instead of performing normal action

                                   public function After():void;

                              // called after performing normal action

                              }

                               

                              FiniteStateMachine Example:  http://www.bigroom.co.uk/blog/finite-state-machines-for-ai-in-actionscript

                              Decorator Article:  http://www.as3dp.com/2009/04/26/actionscript-30-easy-and-practical-decorator-design-patter n/

                              (I am sure you already know what these are but I figured the reference could not hurt.)

                               

                                   I know I am not going to explain what I am thinking correctly and I do not have time to write it out in code to test.  I am hoping if nothing else this provides you with some creative ideas of your own if not an answer to your problem.

                               

                                   Too briefly, I am thinking that if you have a class that extends ObjectProxy, accepts (wraps) an IDAO (your DAO interface), proxey's it and also implements IAdvice.  You could then pass that along to your "AdviceMachine" (similar to a StateMachine). Using the proxied IAdvice object which is acting as our Object Proxy we watch for changes.  When a change occurs our AdviceMachine class (or a class that uses it) runs one or more of the Advice methods on the IAdvice Object we have proxied, Which in turn would then acquire (or have already loaded) the appropriate instructions and run them.

                                 

                                   Which instruction to run could be loaded into a DictionaryObject from an XML file or perhaps an returned query from a remote or local DB.  This would allow the different wrappers to dynamically pull their advice methods based on the Object Type of the proxied object.

                               

                                   Thinking further; a singleton factory could be used to store your DictionaryObject and be the central location to both pull Models from and associated Advice to be used with it.  The DictionaryObject would need to contain more then just the Model objects.  You would want to use an associate array (or perhaps an ArrayCollection or XMLListCollection) for each object.  The collection object would contain both Model and the Advice to use with that model.  Doing so allows for one one instance of the model which can be shared around the application with the set of advice applied to it.  The singleton factory would access the dictionary object to create model instances and another class would access it to acquire Advice based on the Model type.

                               

                                   Sorry that's a mouthful and does not clearly state what I am thinking but I am writing this on the fly while working on other things and my Coffee is running out.  So forgive me if I am not even remotely close to what you are wanting.  But with the above arrangement (or a better one) you would not need to worry about the ObjectProxy datatype because it would be referenced as IAdvice (or whatever name you wish).  And this would allow you to have the IAdvice object acquire it's advice to use or you could inject it.

                               

                              -Joe

                              • 12. Re: Fooling the runtime over type
                                EgorKDie Level 2

                                It's obvious to see where Adobe is going in general (opening up AIR to mobile would increase its audience massively), but I'm sure you guys have a more than enough people on board to do both at once!

                                 

                                Unless you are all bug fixing... I'd accept that

                                • 13. Re: Fooling the runtime over type
                                  EgorKDie Level 2

                                  Thanks for the wall of text

                                   

                                  Yeah, I see where you are going with that, but it breaks one of the central pillers of frameworks... that the code shouldn't be altered to accomodate them. I could get it to work by untyping everything, that isn't the problem. It's getting it to work in a legacy codebase (3k+ files and 300k+ LOC) without having to refactor everything to wedge it in.

                                  • 14. Re: Fooling the runtime over type
                                    Ratsnackbar Level 2

                                    Wow!  Yeah, been there, done that, and I do not envy you! 

                                     

                                    -Joe

                                    • 15. Re: Fooling the runtime over type
                                      Flex harUI Adobe Employee

                                      We don't have nearly enough people.  Plus, the economy sucks so we can't pay for more people.  That's why we have solicited community contributions for bug fixes and what not.

                                       

                                      Me personally, I'm fixing bugs right now...

                                       

                                      Alex Harui

                                      Flex SDK Developer

                                      Adobe Systems Inc.

                                      Blog: http://blogs.adobe.com/aharui