ASReflect: Java like reflection in AS3
flash.utils.describeType is an only way to do reflection in AS3 but it’s annoying to work with XML! So I created a library called ASReflect to manipulate the result of describeType more instinctively. ASReflect has some Java like reflection classes.
The following is an example. If you have a class MyClass like:
package
{
public class MyClass
{
public var a:String;
public var b:Number;
public function func():void
{
}
public function add(arg1:int, arg2:int):int
{
return arg1 + arg2;
}
}
}
You can get type information as an instance of Type class by ASReflect.getType method.
var type:Type = ASReflect.getType(MyClass);
Then, for example, you can output "class name", "property name and type" and "method name, parameter count and return type" by the following code:
trace('ClassName:', type.name);
trace('Properties:');
for each (var prop:Property in type.properties) {
trace(' ' + prop.name, prop.type);
}
trace('Methods:');
for each (var method:Method in type.methods) {
trace(' ' + method.name, method.parameters.length, method.returnType);
}
Output of above code:
ClassName: MyClass
Properties:
a [class String]
b [class Number]
length [class int]
Methods:
add 2 [class int]
func 0 null
There is ASDoc only in Japanese (sorry...), but I think you can guess what does property or method provide by its name. Please ask me if you have any question.
Source was committed to the Spark project. And also you can get a SWC package or a source code archive.
4 Comments
Cool, this looks useful. DescribeType / DescribeTypeCache is indeed very clunky.
Thank you!
I forgot to write: Of course, ASReflect.getType caches its result!
Thanks so much for the new blog. You guys have been a huge inspiration for me here in the US! Keep up the great work!
Very cool! This might become useful! Thx Yoshihiro!