4 Replies Latest reply on Nov 8, 2007 9:11 AM by levancho

    Can AS do reflection and method invocation?

    kedward
      Hello,

      Can AS do class reflection, instantiation, and method invocation as in the below java example:

      Class c = IncidentRules.class;
      Class[] parameterTypes = null;
      Method methodName;

      methodName = c.getMethod("validate"+commandName,parameterTypes);
      methodName.setAccessible(true);
      methodName.invoke(this,null);
        • 1. Re: Can AS do reflection and method invocation?
          kedward Level 1
          I found one such example, but would appreciate if someone could provide a n example that equates to the java example I posted:

          http://www.cynergysystems.com/blogs/page/keunlee?entry=reflection_in_actionscript_3_0
          • 2. Re: Can AS do reflection and method invocation?
            atta707 Level 2
            Unlike Java, AS is dynamic:

            var ClassReference:Class = getDefinitionByName(“flash.display.Sprite”) as Class;
            var instance:Object = new ClassReference();
            instance.graphics.beginFill(bgColor);
            instance.graphics.drawRect(0, 0, size, size);
            instance.graphics.endFill();
            addChild(DisplayObject(instance));

            Note the type of variable called 'instance'. It's Object. Still, it's calling method(s) of the underlying object without a compile time warning.

            Also, unlike Java AS has construct like object["property"] which would either return the reference to the property of null if object doesn't have given property.

            Also in AS you have class called Function that represents a function.

            Now, the real fun begins! Although AS doesn't have OO reflection like Java, but we can combine above constructs to do the job.

            here is you basic class with two functions:

            public class XV
            {

            public function hello1(s:String) : void {
            trace('message from XV.hello1: ' + s);
            }

            public function hello2(s:String) : void {
            trace('message from XV.hello2: ' + s);
            }
            }

            now you want to call hello1 and hello2.

            Now, if you run code like:

            1 var xv:XV = new XV();
            2 var m:String = "hello";
            3 var f:Function = xv[m + "1"];
            4 f.call(this, 'Hello World!');

            note line 3 above. You're looking for a property called hello1 and you *know* it's a function so you get a reference of it. On line 4 so simply call it with only argument of type String!

            Hope this helps!

            ATTA


            • 3. Re: Can AS do reflection and method invocation?
              kedward Level 1
              AWESOME! works great. Thank You

              Is there a book or website that goes into detail about this?

              Ed
              • 4. Re: Can AS do reflection and method invocation?
                levancho Level 3
                atta07 your example is wrong
                var ClassReference:Class = getDefinitionByName(“flash.display.Sprite”) as Class;
                var instance:Object = new ClassReference();
                instance.graphics.beginFill(bgColor);
                instance.graphics.drawRect(0, 0, size, size);
                instance.graphics.endFill();
                addChild(DisplayObject(instance));

                does not work, unfortunately AS reflection does not support dinamic clas creation unless you harcode a class refference that you want to create dinamicaly.