I have x amount of classes in a package eg. "com.trevorboyle.lotsofclasses" and I keep adding more (These classes will probably all be static and will probably all extend the same class).
I wish to create a drop-down list of all these classes, preferably without having to manually create an array myself.
Once a class is selected from the list, I'll be able to use getDefinitionByName to return it, because I will know it's name at this point.
The question is, as I believe there is no support in AS3 to list all the classes in a specific package, is there a design pattern that handles this?
There are two ways:
First, traditional clean as3 implementation:
Create a class that registers all the classes of your interest at class initialization (when it's first used in code).
Something like this:
package {
import flash.utils.getQualifiedClassName;
public class ClassesRegister {
private static const PACKAGE_RESOLVER_PATTERN : RegExp = /^(?P<packageName>[a-zA-Z_\.]+)::/;
private static const registeredClasses : Object = { };
public static function registerClass ( clazz : Class ) : void {
var packageName : String = PACKAGE_RESOLVER_PATTERN.exec( getQualifiedClassName( clazz ) ).packageName || "";
if ( !registeredClasses[ packageName ] ) {
registeredClasses[ packageName ] = [];
}
registeredClasses[ packageName ].push( clazz );
}
public static function getClassesOfPackage ( packageName : String ) : Array {
return registeredClasses[ packageName ].concat();
}
}
}
Class registered on initialization:
package {
public class SomeClass {
// Add class to register in static initializer
{
ClassesRegister.registerClass( SomeClass );
}
/**
* Class constructor
*/
public function SomeClass () {
}
}
}
And there is another, not tested by myself but very promising:
Use as3commons bytecode library.
Class ByteCodeTypeCache in property definitionNames
stores all fully qualified definition names that have been encountered in all the bytecode that was scanned. You can just iterate through that list and take what you need. Of course you have to make sure that all classes you will need on runtime is compiled into bytecode (you have to initialize those classes somewhere or use as3 compiler directives).