Introduction

cheap cialis pill certified cialis cheap viagra in canada cialis buy drug buy generic cialis viagra buy 25mg viagra cheap viagra without prescription buy cheapest viagra on line purchase viagra cialis 10mg buying generic viagra cialis pills viagra from india cheapest sildenafil citrate cheap cialis no rx viagra india cialis bangkok viagra for order buy sildenafil internet buy generic viagra online buying cialis online where to order cialis tablet cialis find cialis no prescription required viagra cheap drug order cialis cheap online online pharmacy cialis cialis no rx order generic cialis price of cialis viagra soft drug viagra cheap viagra from uk order cialis no prescription order cheap viagra viagra drug order cheap cialis cheap cialis pharmacy best price for viagra cheap viagra from usa cost cialis cialis overnight shipping cheapest generic cialis online generic viagra online online viagra viagra sales cheap cialis in canada compare cialis prices online cialis online drug viagra online purchase discount cialis without prescription no rx viagra cialis overnight viagra uk cialis order cheap cialis from usa buying cialis cialis overnight delivery cialis in bangkok buy and purchase sildenafil online impotence treatment cheap price viagra viagra sale cheap cialis tablet drug cialis generic cialis online cheap viagra pharmacy find discount cialis online viagra malaysia cialis without a prescription buy cialis online cheap viagra rx buy no rx viagra cialis 20mg viagra in malaysia discount viagra online buy sildenafil cheap buy viagra low price buy cialis cialis cheap price cialis cheap generic viagra cialis canada low cost viagra buy cheap viagra cialis vs viagra order cialis from us cialis tablets find no rx cialis buy generic cialis online buy viagra overnight delivery cheapest cialis price buy cheapest cialis on line order cialis in canada viagra tablet viagra no online prescription find cheap cialis online viagra price order viagra no prescription cheap generic cialis buy viagra online cheap cialis uk cialis without rx generic cialis cheap viagra vs cialis order cialis on internet viagra tablets viagra purchase impotence drugs buy cialis generic cialis tablet cialis cheapest price order viagra from canada viagra generic cheap viagra from canada order cialis compare viagra prices online find cheap cialis impotence cure pfizer viagra find discount cialis cheapest cialis buy cialis from india impotence buy cheapest viagra online cialis side effects viagra order discount cialis online cialis in malaysia cialis in uk viagra in uk cialis online without prescription cialis online pharmacy order viagra buy viagra online viagra side effects cialis sale discount cialis no rx cheapest viagra find cialis order cialis no rx buy cialis low price buy viagra cheap drug cialis online purchase order discount viagra online 50 mg viagra 100 mg viagra 10mg cialis cost of cialis cheapest cialis prices buy discount viagra online cialis sales 50mg viagra cialis price buy viagra on internet cialis pill cheapest cialis online purchase viagra overnight delivery cheap cialis from canada cheapest viagra price cialis 20 mg buy sildenafil low cost order viagra without prescription buy viagra lowest price no prescription cialis order viagra on internet discount cialis overnight delivery cialis cheap drug viagra approved viagra no rx required compare viagra prices no rx cialis cheap cialis on internet buy viagra from india buy discount cialis online viagra pharmacy online order viagra from us cialis free delivery cialis for order buy cialis from canada viagra without rx viagra online review 10 mg cialis cheap viagra no rx cheapest viagra prices viagra prices cialis pharmacy order no rx cialis buy cialis in us buy cialis no prescription required order cialis from canada lowest price cialis cheap cialis internet online pharmacy viagra cheapest generic cialis generic drugs cialis india find cialis without prescription best price cialis buy viagra without prescription cheap cialis in uk where to buy viagra 20 mg cialis cheap cialis from uk buy sildenafil canada cialis no rx required cialis in us buy cialis overnight delivery cialis cheap price order cheap viagra online 20mg cialis buy cheap viagra online viagra internet viagra without prescription free cialis buy cialis us cialis buy buy viagra in canada order viagra cheap online find viagra without prescription viagra pills cheap cialis no prescription viagra online without prescription order generic viagra cialis discount viagra cheapest price purchase viagra no rx viagra no rx viagra cheap discount viagra overnight delivery sale cialis cialis pharmacy online purchase cialis without prescription pharmacy online cialis medication discount viagra buy cheap cialis impotence medication viagra medication find cialis on internet impotence pills cialis prices discount viagra without prescription cialis online cheap cialis online review find cheap viagra online buy viagra us purchase cialis online certified viagra where to order viagra buy cheapest viagra buy cialis internet order cialis online buy sildenafil online buy cialis cheap cheap viagra purchase cialis find discount viagra buy cialis on internet cialis buy online buy sildenafil online without a prescription viagra buy online order cheap cialis online viagra information no prescription viagra cost of viagra buy cialis in canada buy cialis online buy viagra cheapest generic viagra cialis us cialis australia fda approved cialis lowest price for viagra viagra bangkok cialis prescription cialis cost buy no rx cialis buy viagra internet viagra discount order viagra overnight delivery generic cialis viagra australia 25 mg viagra order viagra online viagra overnight cialis rx order cialis in us order viagra no rx order discount cialis online viagra vendors order viagra in us buy sildenafil in uk viagra us buy generic viagra viagra canada viagra no prescription viagra cheap price cheap viagra tablet viagra free delivery overnight viagra purchase viagra online find cheap viagra cialis malaysia best price viagra cialis free sample find viagra on internet cialis generic buy sildenafil in canada order cialis no prescription required cheapest viagra online purchase cialis no rx viagra in us order discount cialis cheap viagra internet free viagra cialis approved best price for cialis cialis from india find no rx viagra generic viagra viagra from canada viagra online pharmacy buy viagra from canada cheapest generic viagra online buy cheapest cialis discount cialis viagra overnight delivery cialis without prescription 100mg viagra cialis in australia price of viagra order cialis overnight delivery cheap viagra in uk buying generic cialis viagra pill buy cialis on line low cost cialis find discount viagra online buying viagra cheap cialis overnight delivery pharmacy cialis cheap viagra pill viagra prescription find viagra online buy cialis lowest price discount viagra no rx online cialis viagra free sample cheap viagra in usa find viagra cheap viagra online buy viagra no rx generic viagra cheap buy cialis without prescription buy viagra in us cheap viagra overnight delivery cheap cialis in usa cheap cialis online viagra order no rx viagra viagra soft tab find cialis online lowest price viagra cialis drug cialis vendors viagra online stores erectile dysfunction order viagra in canada buy viagra on line viagra overnight shipping viagra online cheap lowest price for cialis approved viagra pharmacy cialis 10 mg cialis no online prescription cialis purchase cialis from canada order cialis without prescription viagra for sale viagra in australia approved cialis pharmacy buy viagra generic buy sildenafil in spain find viagra no prescription required cialis no prescription buy viagra from us order viagra no prescription required cost viagra purchase viagra without prescription buy cialis no rx cialis cheap cialis internet tablet viagra cheap viagra on internet viagra cost pharmacy viagra cialis soft tab cialis information buy cheap cialis internet purchase cialis overnight delivery cheap cialis without prescription buy viagra no prescription required compare cialis prices buy cheap cialis online overnight cialis where to buy cialis cheap cialis buy cheap viagra internet buy discount cialis viagra buy drug cheap viagra no prescription buy sildenafil citrate buying viagra online buy discount viagra fda approved viagra cialis online stores cheap cialis tablets buy cheapest cialis online cheap viagra tablets order discount viagra sale viagra viagra online cialis for sale cialis soft viagra pharmacy buy cialis from us viagra without a prescription viagra in bangkokFlash is a lightweight cross-platform runtime for rich media, enterprise applications and mobile applications, as well as an integrated development environment. Flash can be programmed in ActionScript 1/2/3.

Tuesday, June 7th, 2005 at 4:35 pm

As2lib Reflection API

What are reflections? Is Flash supporting reflections? What functionalities is the As2lib Reflection API (org.as2lib.env.reflect) offering me and how can I use these functionalities?

Note that this article has also been published on the official as2lib homepage: As2lib Reflection API.

Reflection Basics

What are reflections? How do reflections help me with development? What other programming languages support reflections?

Reflections help you find out everything about your API at run-time; your API is reflected. For example the name of a class an instance was instantiated of or all methods declared by a class that match a given naming pattern. In theory there are nearly no bounds. In languages that support reflections by default, like Java or C#, it is even possible to find out about the parameters of methods and their types.

As I now already stated, the programming languages Java and C# have reflections support. The reflection mechanism of these languages is built-in. These reflection APIs are thus in some respects more powerful than the one I will present in this article.

The most common usage of reflections is to generate good string representations of objects. These string representations can then be used for logging and in error/detail messages. But you can also use reflections to map from a XML file, database or property file to an object. Another common usage is the creation of dynamic factories (GoF, Factory Design Pattern) that resolves classes by name at run-time or instantiates passed classes.

If you are an experienced ActionScript programmer you may have noticed that all these things are possible with a ‘little’ knowledge of ActionScript 1.0 and Flash right now; except finding out class, properties and methods names. But what is missing is a good API that accomplishes these tasks in a performant and save way. Performance is gained by caching results and by good algorithms, safety by comprehensive test suites, a well-typed API, good run-time checks and the assurance that everything works fine in Flash, Flex and with MTASC.

In case you have been hooked: go-on reading and step directly into the As2lib Reflection API.

The Reflection Utility

What is the light-weight reflection utility? What services does it offer? What are its advantages?

The As2lib Reflection API is split into two parts, a light-weight and a heavy-weight one. The light-weight part exists only of one class, the ReflectUtil. This class offers basic methods to look up names of types and methods and to find out whether a specific method is static (per class) or is a constructor.

Its advantages are:

  • It is light-weight. This means that the class has little dependencies and the SWF file size is thus only increased by a minimum.
  • Its usage is quite simple. You do not have to deal with multiple classes and instances, but only invoke static methods.
  • It is fast. Note that the heavy-weight part is also fast, but maybe a little slower because of more delegation. But this speed difference is really at a minimum.

Its disadvantages are:

  • It may not offer all needed functionalities.
  • It does not cache results.
  • It offers only a low-level API.
  • It is neither flexible nor extensible.

Depending on your needs this class may be all you need. The As2lib always uses this class internally to look up API names that are in most cases included in log or error messages.

The most common usage is to look up type names. You have two options: you can either get the type name directly for a class (this is a function in ActionScript) or for an instance of a class.

  1. import org.as2lib.env.reflect.ReflectUtil;
  2. import com.mydomain.myapplication.MyClass;
  3.  
  4. // traces the name of the class looked up by a class
  5. trace("By Class: " + ReflectUtil.getTypeName(MyClass));
  6.  
  7. // traces the name of the class looked up by an instance
  8. var myInstance:MyClass = new MyClass();
  9. trace("By Instance: " + ReflectUtil.getTypeName(myInstance));
  10.  

The output of the above code is:

  1. By Class: com.mydomain.myapplication.MyClass
  2. By Instance: com.mydomain.myapplication.MyClass

The same approach applies to looking up method names. You again have the option between using a class or an instance as basis. Note that either the class itself or any super-class must implement the searched for method. Note that the following example is not a real-life example but just a demonstration of the different ways of performing the wanted task.

  1. import org.as2lib.env.reflect.ReflectUtil;
  2. import com.mydomain.myapplication.MyClass;
  3.  
  4. // traces the name of the per class method looked up by class and method
  5.  
  6. trace("By Class: " + ReflectUtil.getMethodName(MyClass.myStaticMethod, MyClass));
  7.  
  8. // traces the name of the per instance method looked up by class and method
  9. trace("By Class: " + ReflectUtil.getMethodName(MyClass.prototype.myMethod, MyClass));
  10.  
  11. // traces the name of the per instance method looked up by instance and method
  12. var myInstance:MyClass = new MyClass();
  13. trace("By Instance: " + ReflectUtil.getMethodName(myInstance.myMethod, myInstance));

If you run the above code you get the following output:

  1. By Class: myStaticMethod
  2. By Class: myMethod
  3. By Instance: myMethod
  4.  

A futher important method is the “getTypeAndMethodInfo” method. This method returns all needed information about the type and the method. This is whether the method is static (per class), the fully qualified name of the type and the name of the method. You may wonder why not just using the available methods to fulfill all these tasks step-by-step. This would work, but it is slower and needs more method calls. Another more important issue is that the method may not be directly implemented by the passed type, but by its super-type. In such a case you’d want the type name to be the one that implements the method and not the passed one. All this is taken into account by this method, which makes it important to mention.

  1. import org.as2lib.env.reflect.ReflectUtil;
  2. import com.mydomain.myapplication.MyClass;
  3.  
  4. // gets the type and method information
  5. var info:Array = ReflectUtil.getTypeAndMethodInfo(MyClass, MyClass.myStaticMethod);
  6.  
  7. // if the method is static ‘info[0]’ is ‘true’ otherwise ‘false’
  8. var output:String = info[0] ? "static " : "";
  9.  
  10. // adds the fully qualified type name
  11. output += info[1];
  12.  
  13. // adds the method name
  14. output += "." + info[2] + "()";
  15.  
  16. // traces the output
  17. trace(output);

The output is:

  1. static com.mydomain.myapplication.MyClass.myStaticMethod()

If the method were not static and were implemented by a super-class the output would have looked something like this:

  1. com.mydomain.myapplication.MySuperClass.myMethod()

For the full-range of methods and their usage take a look at the API documentation. Now let’s go-on to the full-featured API.

The Class Info

How can I find out everything about a class? What methods and properties does it declare? What are its super-classes? What is its namespace and of which package is it a member of? How can I get all its methods and properties that match a given criteria?

When you use the As2lib Reflection API the first thing you need is in most cases a ClassInfo instance. Given this instance for a specific class you can perform almost all operations offered by the API related to your class. Note that the “ClassInfo” represents classes as well as interfaces because a differentiation is not possible at run-time. To get a “ClassInfo” instance for your specific class you need either the class itself, an instance of that class or the name of the class.

  1. import org.as2lib.env.reflect.ClassInfo;
  2. import com.mydomain.myapplication.MyClass;
  3.  
  4. // gets a class info by class
  5. var classInfoByClass:ClassInfo = ClassInfo.forClass(MyClass);
  6.  
  7. // gets a class info by instance
  8. var myInstance:MyClass = new MyClass();
  9. var classInfoByInstance:ClassInfo = ClassInfo.forInstance(myInstance);
  10.  
  11. // gets a class info by name
  12. var classInfoByName:ClassInfo = ClassInfo.forName("com.mydomain.myapplication.MyClass");
  13.  

All these class infos are functionaly the same, they are even the same instances; the only thing that differs is how they were obtained. Given such a class info instance you can get the following basic information.

  1. trace("The class’s name: " + classInfo.getName());
  2. trace("The class’s fully qualified name: " + classInfo.getFullName());
  3. trace("The super class’s fully qualified name: " + classInfo.getSuperType().getFullName());
  4. trace("The class’s namespace: " + classInfo.getPackage().getFullName());
  5. trace("The class’s constructor: " + classInfo.getConstructor());
  6. trace("The declared methods: " + classInfo.getMethods(true));
  7. trace("The declared properties: " + classInfo.getProperties(true));
  8.  

If you like to you can also create a new instance of the represented class.

  1. var newInstance:MyClass = classInfo.newInstance("arg1", 2);

To get a MethodInfo instance that represents a method of your class or any super-class you have the name of or the concrete method as function you can use the “ClassInfo.getMethod” method. Note that the following works totally the same for properties, but with a PropertyInfo and the “ClassInfo.getProperty” method.

  1. import org.as2lib.env.reflect.MethodInfo;
  2.  
  3. // gets a method info by name
  4. var methodInfoByName:MethodInfo = classInfo.getMethod("myMethod");
  5.  
  6. // gets a method info by concrete method
  7. var myInstance:MyClass = new MyClass();
  8. var concreteMethod:Function = myInstance.myMethod;
  9. var methodInfoByConcreteMethod:MethodInfo = classInfo.getMethod(concreteMethod);
  10.  

Now let us come to a little more complex method, the “ClassInfo.getMethods” method. In its simplest from, which I used in an example at the beginning, this method simply returns either only the methods declared by the class itself or this method and the one of all super-classes. But it is also possible to use your own filter criteria. You must therefore at first create a class that implements the TypeMemberFilter interface. You can then implement your filter criteria in the “filter” method that returns “true” if the passed-in type member shall be excluded/filtered or “false” if not. Let us say we want to filter all methods that do not start with the string “add”.

  1. import org.as2lib.env.reflect.TypeMemberFilter;
  2. import org.as2lib.env.reflect.TypeMemberInfo;
  3.  
  4. class MyMethodFilter implements TypeMemberFilter {
  5.  
  6.     public function TypeMemberFilter(Void) {
  7.     }
  8.  
  9.     public function filter(typeMember:TypeMemberInfo):Boolean {
  10.         if (typeMember.getName().indexOf("add") == 0) {
  11.             return false;
  12.         }
  13.         return true;
  14.     }
  15.  
  16.     public function filterSuperTypes(Void):Boolean {
  17.         return false;
  18.     }
  19. }
  20.  

You can now use an instance of the filter “MyFilter” as parameter for the “ClassInfo.getMethods” method to receive only methods that have the prefix “add”.

  1. var methods:Array = classInfo.getMethods(new MyMethodFilter());
  2.  

The above mechanism for methods works the same for properties. You only have to use the equivalent property methods. Take a look for these in the API documentation of the “ClassInfo” class.

The Package Info

How can I find out everything about a package? What members, classes and packages, does it have? What are its parent packages? How can I get all its members that match a given criteria?

As with class infos you can get PackageInfo instances either by a concrete package or by name.

  1. import org.as2lib.env.reflect.PackageInfo;
  2.  
  3. // gets a package info by concrete package
  4. var packageInfoByPackage:PackageInfo = PackageInfo.forPackage(com.mydomain.myapplication);
  5.  
  6. // gets a package info by name
  7. var packageInfoByName:PackageInfo = PackageInfo.forName("com.mydomain.myapplication");
  8.  

The two returned package infos are functionally and physically the same, they are the same instance. Given either of these instances you can find out the following basic information.

  1. trace("The package’s name: " + packageInfo.getName());
  2. trace("The package’s fully qualified name: " + packageInfo.getFullName());
  3. trace("The parent package’s name: " + packageInfo.getParent().getName());
  4. trace("Is this package a/the root package?: " + packageInfo.isRoot());
  5. trace("The package’s member classes: " + packageInfo.getMemberClasses());
  6. trace("The package’s member packages: " + packageInfo.getMemberPackages());

A specific member class or package can also be obtained either by concrete class or package or by name.

  1. import org.as2lib.env.reflect.ClassInfo;
  2. import com.mydomain.myapplication.MyClass;
  3.  
  4. // gets a class member by name
  5. var classMemberInfoByName:ClassInfo = packageInfo.getMemberClass("MyClass");
  6.  
  7. // gets a class member by concrete class
  8. var classMemberInfoByClass:ClassInfo = packageInfo.getMemberClass(MyClass);
  9.  
  10. // gets a package member by name
  11. var packageMemberInfoByName:PackageInfo = packageInfo.getMemberPackage("mymodule");
  12.  
  13. // gets a package member by concrete package
  14. var packageMemberInfoByPackage:PackageInfo = packageInfo.getMemberPackage(com.mydomain.myapplication.mymodule);
  15.  

In the previous article on class infos we talked about getting methods or properties that match a given criteria. This is also possible with package infos but the criteria must now be met by member classes and member packages. We do again need a filter that filters/excludes unwanted package members. This filter must implement the PackageMemberFilter interface. This time we want to include all abstract classes that start with the “Abstract” string to be included in the result; sub-packages shall also be searched through.

  1. import org.as2lib.env.reflect.PackageMemberFilter;
  2. import org.as2lib.env.reflect.PackageMemberInfo;
  3.  
  4. class MyClassMemberFilter implements PackageMemberFilter {
  5.  
  6.     public function PackageMemberFilter(Void) {
  7.     }   
  8.  
  9.     public function filter(packageMember:PackageMemberInfo):Boolean {
  10.         if (packageMember.getName().indexOf("Abstract") == 0) {
  11.             return false;
  12.         }
  13.         return true;
  14.     }
  15.  
  16.     public function filterSubPackages(Void):Boolean {
  17.         return false;
  18.     }
  19. }

We can now use our filter as follows:

  1. var classMemberInfos:Array = packageInfo.getMemberClasses(new MyClassMemberFilter());

Exactly the same mechanism can also be applied to member packages. Take a look at the API documentation of the “PackageInfo” class for the appropriate methods.