Reading all classes under a package or reading classes with same Metadata in Actionscript 3.0
I am doing an Actionscript 3.0 project which involves introspection. I am wondering if there is a way to get all the classes within a given package structure. For e.g. Say there are three as3 classes: com.example.test.classOne com.example.test.classTwo com.example.test.classThree
I want to be able to say
getClassesUnderPackageName("com.example.test");
and get back
"com.example.test::classOne"
"com.example.test::classTwo"
"com.example.test::classThree".
Is there a way to do that?
If this is not possible, is there a way to read classes which have the same metadata?
E.g. If all the mentioned classes have the same metadata [MetadataName(type="example")] defined, is there a wa开发者_开发技巧y to say
getClassesWithSameMetadata("MetadataName");
and get back
"com.example.test::classOne"
"com.example.test::classTwo"
"com.example.test::classThree".
Thank you.
you can use flash.utils.describeType to return XML data containing this information. it works differently on base classes, like flash.display.Sprite
, but for custom classes/directories, you can write something like this:
package branchA.branchB.branchC
{
//Imports
import flash.utils.describeType;
//Class
public class Test
{
//Constructor
public function Test()
{
trace(describeType(this).@name);
}
}
}
//OUTPUT: branchA.branchB.branchC::Test
if you wanted to return the base class, you could write something like this:
package
{
//Imports
import flash.display.Sprite;
import flash.utils.describeType;
//Class
public class Test extends Sprite
{
//Constructor
public function Test()
{
trace(describeType(this).@base);
}
}
}
//OUTPUT: flash.display::Sprite
there is lots of other useful information you can get by parsing the returned XML data of describeType.
Update:
class objects do not need to have been instantiated first in order to retrieve their information via describeType(). you could build a public static function (or whatever) which accepts an array of your class objects and returns an array of strings containing the required describeType data.
something like this:
package
{
import flash.utils.describeType;
final public class Describe
{
public static function packageNames(classObjects:Array):Vector.<String>
{
var names:Vector.<String> = new Vector.<String>();
for each (var classObject in classObjects)
names.push(describeType(classObject.@name.toString()));
return names;
}
}
}
then from anywhere in your program, you can pass an array of all the classes like this:
var names:Vector.<String> = Describe.packageNames(new Array(classOne, classTwo, classThree));
trace(names);
//Output:
//com.example.test::classOne
//com.example.test::classTwo
//com.example.test::classThree
There's no inbuilt mechanism for finding classes without already knowing the class name. :(
However if you load in a SWF as a ByteArray then it's possible to iterate through the classes in it.
This might be overkill for what you want.
http://www.bytearray.org/?p=175
Take a look at AS3 Commons Bytecode. It allows you to do Bytecode based reflection. You can list all classes (you'll need to filter those if you just want a particular package), list classes with certain metadata or classes that implement a certain interface.
精彩评论