Finding the class and details of an object
Reflection and introspection are in the Apache Royale Reflection
library.
Royale supports class reflection and introspection using the following functions in the org.apache.royale.reflection
package:
Use reflection to find the class of an object.
Sets up an alias mapping for serialization/deserialization purposes. The Royale compiler can auto-generate this when using class level metadata, e.g. [RemoteClass(alias=‘someAlias’)]
registerClassAlias("someAlias", com.acme.Foo);
Retrieves an alias for a class, based on an alias mapping previously registered with registerClassAlias
, or possibly using [RemoteClass(alias=‘someAlias’)] metadata.
var alias:String = getAliasByClass(com.acme.Foo); trace(alias); // Displays the alias name (i.e. someAlias)
Retrieves a class based on an alias mapping previously registered with registerClassAlias
, or possibly using [RemoteClass(alias=‘someAlias’)] metadata.
var classRef:Class = getClassByAlias("someAlias"); trace(classRef); // Displays the class (i.e. com.acme.Foo)
A utility function to return ancestry (base classes) as a set of qualified names.
var ancestry:Array = getAncestry(org.apache.royale.core.UIBase); trace(ancestry.join(\n))// will output a list of the inheritance tree
Returns the qualified name of a closure instance. We recommend comparing against this when checking for closures. It could insulate against future changes or future variation between targets.
(An app developer does not generally use this.)
trace(getClosureQualifiedName());// outputs "builtin.as$0.MethodClosure"
Pass to getQualifiedClassName()
a reference to an object, and it returns the object's fully qualified class name:
var loader:URLLoader = new URLLoader(); var className:String = getQualifiedClassName(loader); trace(className); // Displays flash.net::URLLoader
To retrieve the fully qualified superclass name of an object, use getQualifiedSuperclassName():
var loader:URLLoader = new URLLoader(); var className:String = getQualifiedSuperclassName(loader); trace(className); // Displays flash.events::EventDispatcher
Checks if a name is defined and will return a valid result when calling getDefinitionByName
.
If you have a class or function name, you can retrieve a reference to it with getDefinitionByName()
. Provide a string parameter specifying a fully qualified class or function name and the function and getDefinitionByName()
returns its Object type.
If you’re retrieving a class reference, you can cast the return value to Class:
var classReference:Class = Class(getDefinitionByName("flash.net.URLLoader"));
You can then create a new instance:
var instance:Object = new classReference();
You can also use the return value from getQualifiedClassName()
or getQualifiedSuperclassName()
with getDefinitionByName()
:
var loader:URLLoader = new URLLoader(); var className:String = getQualifiedClassName(loader); var classReference:Class = Class(getDefinitionByName(className)); var instance:Object = new classReference();
A utility method to check if an object is dynamic (i.e. can have non-sealed members added or deleted).
Note that static class objects are always dynamic, as are (static) Interface Objects.
The function can take four parameters:
true
to include that specific field in the results. If it returns false
, that property is excluded. Defaults to null
, which applies no filtering and returns all detected properties.true
. If it is known that the object is dynamic before calling this method, setting this to false
could improve performance. The results of calling this method on non-dynamic objects may be less reliable or less consistent across target platforms if checkFirst is false.true
, numeric fields will be returned as numeric values.It returns the dynamic fields as Strings in an Array.
A utility method to check if an object is dynamic (can have non-sealed members added or deleted).
Note that static class objects are always dynamic, as are Interface Objects
if(isDynamicObject(myObject)){ trace("yay!); }
Use describeType() to return a description of all the events, public properties, and public methods of an object. For example, to get the details of a URLLoader object, you could use a method like this with a reference to the object:
var loader:URLLoader = new URLLoader(); var description:XML = describeType(loader); trace(description);
The method returns an XML object that details the class name, superclass, class settings, implemented interfaces, constructor signature, public method signatures, and descriptions of the public properties of the object.