Introduction
The 2 most important classes in this library are the Type and
ClassUtils classes.
The Type class will give you all the information
of a class while the
ClassUtils class contains utility
methods for working with class objects.
Using as3commons-reflect is really easy, after reading this short
introduction
the only thing you might need is having a look at the
API
to navigate through the properties and methods exposed by the
library. Here below
we give some short instructions on how to use the
library.
Assuming you have a class called Person in the package
com.company.domain)
you want to use reflection on, you start by
getting its "Type" in one of the
following methods:
By class...
var type:Type = Type.forClass(Person);
... by instance
var person:Person = new Person();
var type:Type = Type.forInstance(person);
... or by class name...
var type:Type = Type.forName("com.company.domain.Person");
// or var type:Type = Type.forName("com.company.domain::Person");
Once you have a type instance you can get access to all variables,
constants,
accessors (getters/setters) and methods defined by that type and the
properties
of the type:
type.alias
type.accessors
type.constants
type.constructor
type.fields
type.fullName
type.isDynamic
type.isFinal
type.isStatic
type.isInterface
type.methods
type.name
type.staticConstants
type.staticVariables
type.variables
type.extendsClasses
//Method and field acces:
method:Method = type.methods[0];
method:Method = type.getMethod("myMethodName");
retType:Type = method.getReturnType();
method.invoke(object, [arg1, arg2]);
accessor:Accessor = type.accessors[0];
accessor:Accessor = type.getField("myAccessorName") as Accessor;
//Metadata retrieval and querying:
metadata:Metadata = accessor.getMetadata("myMetadata");
arg:MetadataArgument = metadata.getArgument("myArg");
var has:Boolean = accessor.hasMetadata("myMetadata");
var argument:MetadataArgument = new MetadataArgument("myArg","myValue");
var metaDataInstance:Metadata = new Metadata("myMetadata",[argument]);
var hasExact:Boolean = accessor.hasExactMetadata(metaDataInstance);
var containers:Array = type.getMetadataContainers("Bindable");
for each(var metadataContainer:IMetadataContainer in containers){
if (metadataContainer is Method){
//do something...
} else if (metadataContainer is Field){
//do something else...
}
...
}
When Flash player 10.1 or higher is installed as3commons-reflect will make use of
the describeTypeJSON method available in these players. This will yield a small speed
optmization since no XML needs to be parsed.
The only limitation of describeTypeJSON is that any alias information (regarding the
registerAlias functionality) is not available. For this, or any other reason, ascommons-reflect
can be forced to use the regular describeType method by setting the static typeProviderKind
property on the Type class:
Type.typeProviderKind = TypeProviderKind.XML;