ActionScript 3.0 supports class reflection using the following
functions in the flash.utils
package:
getQualifiedClassName
getQualifiedSuperclassName
getDefinitionByName
describeType
You can retrieve the name of the class for which an object is an
instance using the getQualifiedClassName()
function. The function
requires that you pass it a reference to an object; it then returns the
fully qualified class name.
var loader:URLLoader = new URLLoader(); var className:String = getQualifiedClassName(loader); trace(className); // Displays flash.net::URLLoader
If you want to retrieve the fully qualified superclass name for an
object, you can use the getQualifiedSuperclassName()
function:
var loader:URLLoader = new URLLoader(); var className:String = getQualifiedSuperclassName(loader); trace(className); // Displays flash.events::EventDispatcher
If you have a class name, you can retrieve a reference to the class
using the getDefinitionByName()
function.
The function requires a string parameter specifying a
fully qualified class name, and it returns an Object
type. The function returns an Object
type rather than a Class
type because it could also theoretically
return a reference to a function if you pass it a fully qualified
function name (e.g., flash.util.getTimer
).
If you’re certain that you’re retrieving a class reference, you
can cast the return value to Class
,
as in the following example:
var classReference:Class = Class(getDefinitionByName("flash.net.URLLoader"));
Once you’ve retrieved a reference to a class, you can create a
new
instance, as follows:
var instance:Object = new classReference();
Obviously, you can use the return value from getQualifiedClassName()
or getQualifiedSuperclassName()
in
conjunction with getDefinitionByName()
, as in the following
example:
var loader:URLLoader = new URLLoader(); var className:String = getQualifiedClassName(loader); var classReference:Class = Class(getDefinitionByName(className)); var instance:Object = new classReference();
You can use describeType()
to return a description of all the events, public properties, and public
methods of an object. Simply pass the method a reference to the object
you want to introspect. The method returns an XML
object that details the class name,
superclass, various class
settings, implemented interfaces, constructor signature, public method
signatures, and public properties descriptions.
The following example retrieves the description for a URLLoader
object:
var loader:URLLoader = new URLLoader(); var description:XML = describeType(loader); trace(description);
The preceding example outputs the following:
<type name="flash.net::URLLoader" base="flash.events::EventDispatcher" isDynamic="false" isFinal="false" isStatic="false"> <metadata name="Event"> <arg key="name" value="httpStatus"/> <arg key="type" value="flash.events.HTTPStatusEvent"/> </metadata> <metadata name="Event"> <arg key="name" value="securityError"/> <arg key="type" value="flash.events.SecurityErrorEvent"/> </metadata> <metadata name="Event"> <arg key="name" value="ioError"/> <arg key="type" value="flash.events.IOErrorEvent"/> </metadata> <metadata name="Event"> <arg key="name" value="progress"/> <arg key="type" value="flash.events.ProgressEvent"/> </metadata> <metadata name="Event"> <arg key="name" value="complete"/> <arg key="type" value="flash.events.Event"/> </metadata> <metadata name="Event"> <arg key="name" value="open"/> <arg key="type" value="flash.events.Event"/> </metadata> <extendsClass type="flash.events::EventDispatcher"/> <extendsClass type="Object"/> <implementsInterface type="flash.events::IEventDispatcher"/> <constructor> <parameter index="1" type="flash.net::URLRequest" optional="true"/> </constructor> <variable name="bytesTotal" type="uint"/> <variable name="data" type="*"/> <method name="load" declaredBy="flash.net::URLLoader" returnType="void"> <parameter index="1" type="flash.net::URLRequest" optional="false"/> </method> <method name="close" declaredBy="flash.net::URLLoader" returnType="void"/> <variable name="dataFormat" type="String"/> <variable name="bytesLoaded" type="uint"/> <method name="dispatchEvent" declaredBy="flash.events::EventDispatcher" returnType="Boolean"> <parameter index="1" type="flash.events::Event" optional="false"/> </method> <method name="toString" declaredBy="flash.events::EventDispatcher" returnType="String"/> <method name="willTrigger" declaredBy="flash.events::EventDispatcher" returnType="Boolean"> <parameter index="1" type="String" optional="false"/> </method> <method name="addEventListener" declaredBy="flash.events::EventDispatcher" returnType="void"> <parameter index="1" type="String" optional="false"/> <parameter index="2" type="Function" optional="false"/> <parameter index="3" type="Boolean" optional="true"/> <parameter index="4" type="int" optional="true"/> <parameter index="5" type="Boolean" optional="true"/> </method> <method name="hasEventListener" declaredBy="flash.events::EventDispatcher" returnType="Boolean"> <parameter index="1" type="String" optional="false"/> </method> <method name="removeEventListener" declaredBy="flash.events::EventDispatcher" returnType="void"> <parameter index="1" type="String" optional="false"/> <parameter index="2" type="Function" optional="false"/> <parameter index="3" type="Boolean" optional="true"/> </method> </type>
Get Programming Flex 3 now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.