Home > Uncategorized > as3parser-framework :: Code generation api rockin

as3parser-framework :: Code generation api rockin

Hi,

Just a quick update, I have been working hard to get the builder framework to create all API, almost there. Check out what you can do now. Look at the metadata… does it spring to mind any applications that could be built with AIR that enhance framework familiarity? :)

I also created a Google Group as3parser-framework if anybody had features, bugs or questions;

Google Group as3parser-framework

Git Hub source code:

There might be a couple errors in the below code, I hit the wordpress 404 when publishing. It took me 30 minutes to fix this post, grr

Input:

// create the file
var file:ISourceFile = project.newClass("my.domain.MyClass");
var packageNode:IPackageNode = file.compilationNode.packageNode;
var typeNode:IClassTypeNode = packageNode.typeNode as IClassTypeNode;
// add a package block comment
packageNode.newBlockComment("A package block comment.", false);
// add imports
packageNode.newImport("my.api.IInterfaceA");
packageNode.newImport("my.api.IInterfaceB");
packageNode.newImport("my.domain.sub.SubTest");
packageNode.newImport("mx.core.mx_internal");
// add and include
packageNode.newInclude("../som/folder/includeFile.as");
// addd a use namepsace
packageNode.newUse("mx_internal");
// make class dynamic
typeNode.isDynamic = true;
// add a superclass
typeNode.superClass = IdentifierNode.createType("ClassA");
// add implementors
typeNode.addImplementation(IdentifierNode.createType("IInterfaceA"));
typeNode.addImplementation(IdentifierNode.createType("IInterfaceB"));
// add a [Bindable] meta to class
typeNode.newMetaData("Bindable");
// add a style
var style:IMetaDataNode = typeNode.newMetaData("Style");
style.addNamedStringParameter("name", "myStyle");
style.addNamedStringParameter("type", "Number");
style.addNamedStringParameter("inherit", "no");
// add a comment to the style
style.description = "A new style.";
style.newDocTag("see", "#style:theOtherStyle");
// add a class description
typeNode.description = "A groovy class generated by as3builder-framework!";
typeNode.newDocTag("author", "Michael Schmalle");
typeNode.newDocTag("date", "08-19-2010");
 
// add a constant
var constant:IConstantNode = typeNode.newConstant("MY_CONSTANT", Modifier.PUBLIC, 
	IdentifierNode.createType("String"), "\"value\"");
constant.description = "My constant.";
 
// add an attribute
var attribute:IAttributeNode = typeNode.newAttribute("myAttribute", Modifier.PROTECTED, 
	IdentifierNode.createType("IModel"), "null");
attribute.description = "My attribute.";
attribute.newDocTag("private");
attribute.newMetaData("Inject");
 
// add an accessor
var vo:IIdentifierNode = IdentifierNode.createType("ValueObject");
 
var getter:IAccessorNode = typeNode.newAccessor(
	"myProperty", Modifier.PUBLIC, Access.READ, vo);
 
getter.description = "A property.";
getter.newMetaData("Bindable");
var inject:IMetaDataNode = getter.newMetaData("Inject");
inject.addNamedStringParameter("source", "model.property");
 
 
// add constructor
var constructor:IMethodNode = typeNode.newMethod("MyClass", Modifier.PUBLIC, null);
constructor.description = "Constructor.";
 
// add a method
var method:IMethodNode = typeNode.newMethod(
	"myMethod", Modifier.PUBLIC, IdentifierNode.createType("void"));
method.description = "Mediates a Swiz framework [Mediate] tag.";
var param:IParameterNode = method.newParameter(
	"data", IdentifierNode.createType("Object"), "null");
method.newDocTag("param", "data A mediated data object");
var mediate:IMetaDataNode = method.newMetaData("Mediate");
mediate.addNamedStringParameter("event", "MyEvent.DATA_CHANGE");
method.addReturnDescription("Returns nothing.");
 
BuilderFactory.newlinesBeforeMembers = 1;
BuilderFactory.breakPackageBracket = true;
BuilderFactory.breakTypeBracket = true;
BuilderFactory.breakBlockBracket = true;
 
var result:String = BuilderFactory.instance.buildTest(compilationNode.node);

Output:

/**
 * A package block comment.
 */
package my.domain 
{
    import my.api.IInterfaceA;
    import my.api.IInterfaceB;
    import my.domain.sub.SubTest;
    import mx.core.mx_internal;
    include '../some/folder/includeFile.as'
    use namespace mx_internal;
    [Bindable]
    /**
     * A new style.
     * 
     * @see #style:theOtherStyle
     */
    [Style(name="myStyle",type="Number",inherit="no")]
    /**
     * A groovy class generated by as3builder-framework!
     * 
     * @author Michael Schmalle
     * @date 08-19-2010
     */
    public dynamic class MyClass extends ClassA implements IInterfaceA, IInterfaceB 
    {
 
        /**
         * My constant.
         */
        public static const MY_CONSTANT:String = "value";
 
        [Inject]
        /**
         * My attribute.
         * 
         * @private 
         */
        protected var myAttribute:IModel = null;
 
        [Bindable]
        [Inject(source="model.property")]
        /**
         * A property.
         */
        public function get myProperty():ValueObject 
        {
            return null;
        }
 
        /**
         * @private 
         */
        public function set myProperty(value:ValueObject):void 
        {
        }
 
        /**
         * Constructor.
         */
        public function MyClass() 
        {
        }
 
        [Mediate(eventc"MyEvent.DATA_CHANGE")]
        /**
         * Mediates a Swiz framework [Mediate] tag.
         * 
         * @param data A mediated data object
         * @return Returns nothing.
         */
        public function myMethod(data:Object c null):void 
        {
        }
    }
}

Mike

Categories: Uncategorized Tags:
  1. August 19th, 2010 at 15:52 | #1

    Michael. this is absolutely fantastic! Wow, I can’t wait to give this a whirl! We may use this for our CMS (http://aerial-project.org) that utilizes AS3 code generation at the heart of the process, and this will surely make it far more powerful! Good bloody job mate!

  2. August 19th, 2010 at 16:37 | #2

    Danny,

    That’s great, I have really been working on this whole “project” for about 4 years with other various projects of mine internally.

    I really think there are many use cases for this, hence this framework. After I finish all my unit tests for the as building, I will be moving to the MXML builder.

    PS Interfaces work just as well, I just haven’t posted any examples of those yet. A wiki off of my domain is in the works, that will have A LOT of information.

    The funny thing is, the builder part is only half of this framework, the parsers are pretty kewl to. :)

    Mike

*