Packageorg.as3commons.bytecode.emit.impl
Classpublic class AbcBuilder
InheritanceAbcBuilder Inheritance flash.events.EventDispatcher
Implements IAbcBuilder

The AbcBuilder is the main entry point for the emit API. Here is an example of how to generate a simple class:
     function generateClass():void {
       var abcBuilder:IAbcBuilder = new AbcBuilder();
       var classBuilder:IClassBuilder = abcBuilder.definePackage("com.classes.generated").defineClass("MyGeneratedClass");
       classBuilder.defineProperty("myProperty", "String", "default value");
       classBuilder.defineAccessor("myOtherProperty", "int", 10).access = AccessorAccess.READ_ONLY;
       abcBuilder.addEventListener(Event.COMPLETE, completeHandler);
       abcBuilder.buildAndLoad(); //loads the generated class into the current application domain
     }
     function completeHandler(event:Event):void {
       var clazz:Class = ApplicationDomain.currentDomain.getDefinition("com.classes.generated.MyGeneratedClass") as Class;
       var instance:Object = new class();
       var propertyValue:String = instance.myProperty;
       var otherPropertyValue:int = instance.myOtherProperty;
       //propertyValue == "default value"
       //otherPropertyValue == 10
       instance.myOtherProperty = 20; //ERROR! Accessor is read-only (getter)
     }
     
To add methods and method bodies to a class a certain knowledge of AVM opcodes is required:
     function generateClass():void {
       var abcBuilder:IAbcBuilder = new AbcBuilder();
       var classBuilder:IClassBuilder = abcBuilder.definePackage("com.classes.generated").defineClass("MyGeneratedClass");
       var methodBuilder:IMethodBuilder = classBuilder.defineMethod("multiplyByHundred");
       methodBuilder.returnType("int");
       methodBuilder.defineArgument("int");
       methodBuilder.addOpcode(Opcode.getlocal_0)
                    .addOpcode(Opcode.pushscope)
                    .addOpcode(Opcode.getlocal_1)
                    .addOpcode(Opcode.pushint, [100])
                    .addOpcode(Opcode.multiply)
                    .addOpcode(Opcode.setlocal_1)
                    .addOpcode(Opcode.getlocal_1)
                    .addOpcode(Opcode.returnvalue);
       abcBuilder.addEventListener(Event.COMPLETE, completeHandler);
       abcBuilder.buildAndLoad(); //loads the generated class into the current application domain
     }
     function completeHandler(event:Event):void {
       var clazz:Class = ApplicationDomain.currentDomain.getDefinition("com.classes.generated.MyGeneratedClass") as Class;
       var instance:Object = new class();
       var i:int = instance.multiplyByHundred(10);
       // i == 1000
     }
     



Protected Properties
 PropertyDefined By
  loader : AbcClassLoader
[read-only] Internal AbcClassLoader instance used by the buildAndLoad() method.
AbcBuilder
  serializer : AbcSerializer
[read-only] Internal AbcSerializer instance used by the buildAndExport() method.
AbcBuilder
Public Methods
 MethodDefined By
  
AbcBuilder(abcFile:AbcFile = null)
Creates a new AbcBuilder instance.
AbcBuilder
  
build(applicationDomain:ApplicationDomain = null):AbcFile
Builds an AbcFile using the previously defined IPackageBuilder instances.
AbcBuilder
  
buildAndExport(applicationDomain:ApplicationDomain = null):ByteArray
Builds, serializes and wraps the AbcFile in a SWF, the resulting ByteArray can be saved to disk as a .swf file for later use.
AbcBuilder
  
buildAndLoad(applicationDomain:ApplicationDomain = null, newApplicationDomain:ApplicationDomain = null):AbcFile
Builds the AbcFile and immediately loads it into the AVM.
AbcBuilder
  
defineClass(fullName:String, superClassName:String = null):IClassBuilder
Creates an IClassBuilder instance for the specified fuly qualified class name and super class name.
AbcBuilder
  
defineInterface(fullName:String, superInterfaceNames:Array = null):IInterfaceBuilder
Creates an IInterfaceBuilder instance for the specified fully qualified interface name and super interface names.
AbcBuilder
  
defineNamespace(fullScopeName:String, URI:String):INamespaceBuilder
AbcBuilder
  
If the specified package name already exists, the exisiting IPackageBuilder will be returned instead of a new instance. Creates an IPackageBuilder instance for the specified package name.
AbcBuilder
Protected Methods
 MethodDefined By
  
addAbcObjects(instances:*, abcFile:AbcFile, applicationDomain:ApplicationDomain, index:uint):uint
AbcBuilder
  
addClassInfo(abcFile:AbcFile, classInfo:ClassInfo):void
Adds the specified ClassInfo and its constructor MethodInfo to the specified AbcFile.
AbcBuilder
  
addInstanceInfo(abcFile:AbcFile, instanceInfo:InstanceInfo):void
Adds the specified InstanceInfo and its constructor MethodInfo to the specified AbcFile.
AbcBuilder
  
addMethodInfo(abcFile:AbcFile, methodInfo:MethodInfo):void
Adds the specified MethodInfo, its associated MethodBody and, if present, its associated Metadata entries to the specified AbcFile.
AbcBuilder
  
AbcBuilder
  
addScriptInfo(abcFile:AbcFile, instanceInfo:InstanceInfo, applicationDomain:ApplicationDomain, index:uint):void
Creates a ScriptInfo instance based on the specified InstanceInfo and adds it and its constructor MethodInfo to the specified AbcFile.
AbcBuilder
  
AbcBuilder
  
createClassScriptInfo(className:String, superClass:BaseMultiname, classInfo:ClassInfo, applicationDomain:ApplicationDomain, index:uint, isInterface:Boolean):ScriptInfo
Creates a ScriptInfo instance based on the specified InstanceInfo.
AbcBuilder
  
createClassScriptInitializer(className:String, superClass:BaseMultiname, classInfo:ClassInfo, applicationDomain:ApplicationDomain):MethodInfo
Creates a MethodInfo instance that represents the initializer method for a ScriptInfo.
AbcBuilder
  
createClassTrait(className:String, index:uint, classInfo:ClassInfo):ClassTrait
Creates a ClassTrait instance for the specified class name and class index.
AbcBuilder
  
AbcBuilder
  
AbcBuilder
  
AbcBuilder
  
extractExtendedClasses(superClassname:String, applicationDomain:ApplicationDomain):Array
AbcBuilder
  
redispatch(event:Event):void
Redispatches the specified AbcClassLoader Event.
AbcBuilder
Events
 Event Summary Defined By
  Dispatched when the class loader has finished loading the SWF/ABC bytecode in the Flash Player/AVM.AbcBuilder
  Dispatched when the class loader has encountered an IO related error.AbcBuilder
  Dispatched when the class loader has encountered a SWF verification error.AbcBuilder
Property Detail
loaderproperty
loader:AbcClassLoader  [read-only]

Internal AbcClassLoader instance used by the buildAndLoad() method.


Implementation
    protected function get loader():AbcClassLoader
serializerproperty 
serializer:AbcSerializer  [read-only]

Internal AbcSerializer instance used by the buildAndExport() method.


Implementation
    protected function get serializer():AbcSerializer
Constructor Detail
AbcBuilder()Constructor
public function AbcBuilder(abcFile:AbcFile = null)

Creates a new AbcBuilder instance.

Parameters
abcFile:AbcFile (default = null)
Method Detail
addAbcObjects()method
protected function addAbcObjects(instances:*, abcFile:AbcFile, applicationDomain:ApplicationDomain, index:uint):uint

Parameters

instances:* — Array of builder objects that represent the various elements to be added to the AbcFile.
 
abcFile:AbcFile — The AbcFile that will be populated with the builder results.
 
applicationDomain:ApplicationDomain — The ApplicationDomain used to lookup superclasses.
 
index:uint — The current class index in the AbcFile.

Returns
uint — The current class index in the AbcFile.
addClassInfo()method 
protected function addClassInfo(abcFile:AbcFile, classInfo:ClassInfo):void

Adds the specified ClassInfo and its constructor MethodInfo to the specified AbcFile.

Parameters

abcFile:AbcFile — The specified AbcFile.
 
classInfo:ClassInfo — The specified ClassInfo.

addInstanceInfo()method 
protected function addInstanceInfo(abcFile:AbcFile, instanceInfo:InstanceInfo):void

Adds the specified InstanceInfo and its constructor MethodInfo to the specified AbcFile.

Parameters

abcFile:AbcFile — The specified AbcFile.
 
instanceInfo:InstanceInfo — The specified InstanceInfo.

addMethodInfo()method 
protected function addMethodInfo(abcFile:AbcFile, methodInfo:MethodInfo):void

Adds the specified MethodInfo, its associated MethodBody and, if present, its associated Metadata entries to the specified AbcFile.

Parameters

abcFile:AbcFile — the specified AbcFile.
 
methodInfo:MethodInfo — the specified MethodInfo.

addNamespace()method 
protected function addNamespace(abcFile:AbcFile, slot:SlotOrConstantTrait):void

Parameters

abcFile:AbcFile
 
slot:SlotOrConstantTrait

addScriptInfo()method 
protected function addScriptInfo(abcFile:AbcFile, instanceInfo:InstanceInfo, applicationDomain:ApplicationDomain, index:uint):void

Creates a ScriptInfo instance based on the specified InstanceInfo and adds it and its constructor MethodInfo to the specified AbcFile.

Parameters

abcFile:AbcFile — The specified AbcFile.
 
instanceInfo:InstanceInfo — The specified InstanceInfo.
 
applicationDomain:ApplicationDomain — The ApplicationDomain used to lookup superclasses.
 
index:uint — The current class index in the AbcFile.

build()method 
public function build(applicationDomain:ApplicationDomain = null):AbcFile

Builds an AbcFile using the previously defined IPackageBuilder instances.

Parameters

applicationDomain:ApplicationDomain (default = null) — The applicationDomain that has access to the super classes that are defined for any classes defined in the AbcFile, defaults to ApplicationDomain.currentDomain.

Returns
AbcFile — The created AbcFile.
buildAndExport()method 
public function buildAndExport(applicationDomain:ApplicationDomain = null):ByteArray

Builds, serializes and wraps the AbcFile in a SWF, the resulting ByteArray can be saved to disk as a .swf file for later use.

Parameters

applicationDomain:ApplicationDomain (default = null) — The applicationDomain that has access to the super classes that are defined.

Returns
ByteArray — The ByteArray containing the serialized AbcFile wrapped inside a SWF.
buildAndLoad()method 
public function buildAndLoad(applicationDomain:ApplicationDomain = null, newApplicationDomain:ApplicationDomain = null):AbcFile

Builds the AbcFile and immediately loads it into the AVM.

This behaviour is asynchronous, so be sure to listen for the Event.COMPLETE, IOErrorEvent.IO_ERROR and IOErrorEvent.VERIFY_ERROR on the current IAbcBuilder.

Once the Event.COMPLETE event has been fired it will be possible to retrieve a reference to the generated classes through the specified application domain, like this:

var cls:Class = applicationDomain.getDefinition("com.myclasses.MyGeneratedClass") as Class

Parameters

applicationDomain:ApplicationDomain (default = null) — The applicationDomain that has access to the super classes that are defined for any classes defined in the AbcFile, defaults to ApplicationDomain.currentDomain.
 
newApplicationDomain:ApplicationDomain (default = null) — The ApplicationDomain in which the newly created AbcFile will be loaded, defaults to ApplicationDomain.currentDomain.

Returns
AbcFile — The created AbcFile.
classNotFoundHandler()method 
protected function classNotFoundHandler(event:ExtendedClassesNotFoundError):void

Parameters

event:ExtendedClassesNotFoundError

createClassScriptInfo()method 
protected function createClassScriptInfo(className:String, superClass:BaseMultiname, classInfo:ClassInfo, applicationDomain:ApplicationDomain, index:uint, isInterface:Boolean):ScriptInfo

Creates a ScriptInfo instance based on the specified InstanceInfo.

Parameters

className:String — The class name of the generated class
 
superClass:BaseMultiname — A BaseMultiName representing the super class.
 
classInfo:ClassInfo — The ClassInfo associated with the new ScriptInfo.
 
applicationDomain:ApplicationDomain — The ApplicationDomain used to lookup superclasses.
 
index:uint — The current class index in the AbcFile.
 
isInterface:Boolean

Returns
ScriptInfo — The new ScriptInfo instance.
createClassScriptInitializer()method 
protected function createClassScriptInitializer(className:String, superClass:BaseMultiname, classInfo:ClassInfo, applicationDomain:ApplicationDomain):MethodInfo

Creates a MethodInfo instance that represents the initializer method for a ScriptInfo.

Parameters

className:String — The class name associated with the ScriptInfo.
 
superClass:BaseMultiname — The super class name associated with the ScriptInfo.
 
classInfo:ClassInfo — The ClassInfo name associated with the ScriptInfo.
 
applicationDomain:ApplicationDomain — The ApplicationDomain used to lookup the superClass.

Returns
MethodInfo — The new MethodInfo instance.
createClassTrait()method 
protected function createClassTrait(className:String, index:uint, classInfo:ClassInfo):ClassTrait

Creates a ClassTrait instance for the specified class name and class index.

Parameters

className:String — The specified class name.
 
index:uint — The current class index in the AbcFile.
 
classInfo:ClassInfo

Returns
ClassTrait — the new ClassTrait.
createInterfaceScriptInitializer()method 
protected function createInterfaceScriptInitializer(className:String, classInfo:ClassInfo):MethodInfo

Parameters

className:String
 
classInfo:ClassInfo

Returns
MethodInfo
createNamespaceScriptInfo()method 
protected function createNamespaceScriptInfo():ScriptInfo

Returns
ScriptInfo
createNamespaceScriptInitializer()method 
protected function createNamespaceScriptInitializer():MethodInfo

Returns
MethodInfo
defineClass()method 
public function defineClass(fullName:String, superClassName:String = null):IClassBuilder

Creates an IClassBuilder instance for the specified fuly qualified class name and super class name.

Parameters

fullName:String — The fully qualified name of the new class. I.e. com.myclasses.MyGeneratedClass.
 
superClassName:String (default = null) — The fully qualified super class name. I.e. mx.events.FlexEvent, defaults to Object.

Returns
IClassBuilder — The specified IClassBuilder instance.
defineInterface()method 
public function defineInterface(fullName:String, superInterfaceNames:Array = null):IInterfaceBuilder

Creates an IInterfaceBuilder instance for the specified fully qualified interface name and super interface names.

Parameters

fullName:String — The fully qualified name of the new interface. I.e. com.myinterfaces.IMyGeneratedInterface.
 
superInterfaceNames:Array (default = null) — The fully qualified super interface names. I.e. mx.styles.IStyleClient.

Returns
IInterfaceBuilder — The specified IInterfaceBuilder instance.
defineNamespace()method 
public function defineNamespace(fullScopeName:String, URI:String):INamespaceBuilder

Parameters

fullScopeName:String
 
URI:String

Returns
INamespaceBuilder
definePackage()method 
public function definePackage(name:String):IPackageBuilder

If the specified package name already exists, the exisiting IPackageBuilder will be returned instead of a new instance.

Creates an IPackageBuilder instance for the specified package name.

An example of a package name: com.myclasses.test

Parameters

name:String — The specified package name

Returns
IPackageBuilder — The specified IPackageBuilder instance.
extractExtendedClasses()method 
protected function extractExtendedClasses(superClassname:String, applicationDomain:ApplicationDomain):Array

Parameters

superClassname:String
 
applicationDomain:ApplicationDomain

Returns
Array
redispatch()method 
protected function redispatch(event:Event):void

Redispatches the specified AbcClassLoader Event.

Parameters

event:Event — The specified AbcClassLoader Event.

Event Detail
complete Event
Event Object Type: flash.events.Event
Event.type property = flash.events.Event.COMPLETE

Dispatched when the class loader has finished loading the SWF/ABC bytecode in the Flash Player/AVM.

ioError Event  
Event Object Type: flash.events.IOErrorEvent
IOErrorEvent.type property = flash.events.IOErrorEvent.IO_ERROR

Dispatched when the class loader has encountered an IO related error.

verifyError Event  
Event Object Type: flash.events.IOErrorEvent
IOErrorEvent.type property = flash.events.IOErrorEvent.VERIFY_ERROR

Dispatched when the class loader has encountered a SWF verification error.