Hi,
I just thought I would run this past all the people that got scared at the last post knowing you can now create actionscript3 code with actionscript3 code.
Now you can create actionscript3 code with a String of actionscript3 code, then print out the actionscript3 code again.
Lets see, how many times did I say actionscript3, 6, OK that’s good.
Git Hub source code:
Input:
var code:String =
"package my.domain {" +
"public class UnitTest {" +
"protected const MY_CONSTANT:int = 42;" +
"[Test]public function testMethod1():void{}" +
"[Test]public function testMethod2():void{}" +
"[Test]public function testMethod3():void{}" +
"}}"
var file:AS3SourceFile = new AS3SourceFile(null, new SourceCode(code, "", ""));
file.buildAst();
var typeNode:ITypeNode = file.compilationNode.typeNode;
var method:IMethodNode = typeNode.getMethod("testMethod2");
method.description = "See, this editing a string with code is fun!";
method.newDocTag("author", "Yeah me");
var meta:IMetaDataNode = method.getMetaData("Test");
meta.description = "Just in case you didn't know, I unit test.";
typeNode.removeMethod(typeNode.getMethod("testMethod1"));
typeNode.newMetaData("TestSuite");
typeNode.description = "Class documentation is good also, but I am lazy!"
var result:String = BuilderFactory.instance.buildFile(file);
Output:
package my.domain {
[TestSuite]
/**
* Class documentation is good also, but I am lazy!
*/
public class UnitTest {
protected const MY_CONSTANT:int = 42;
/**
* Just in case you didn't know, I unit test.
*/
[Test]
/**
* See, this editing a string with code is fun!
*
* @author Yeah me
*/
public function testMethod2():void {
}
[Test]
public function testMethod3():void {
}
}
}
Notice this uses the parser and builder!
Mike
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
Recent Comments