AS3Commons Introduction

4,377 views

Published on

Slides from the Belgian Adobe Usergroup event from November 23rd, 2010.

Published in: Technology, Education
0 Comments
4 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
4,377
On SlideShare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
18
Comments
0
Likes
4
Embeds 0
No embeds

No notes for slide

AS3Commons Introduction

  1. 1. AS3 Commons Roland Zwaga Christophe Herreman Stack&Heap
  2. 2. What it AS3Commons? • Open source community project • Contains several ActionScript 3.0 libraries • Minimal dependencies between libraries: pick what you need • No Flex dependencies! • See Apache Commons
  3. 3. Why AS3Commons? • Central place for AS3 utility libraries • Assure quality of libraries • Unit tested • Started with core parts of the Spring ActionScript project
  4. 4. Libraries in AS3Commons? • Bytecode • Lang • Logging • Reflect • Collections • Concurrency • Serialization • AS3Parser , Async, EventBus (coming up)
  5. 5. AS3-commons-logging Logging API abstraction
  6. 6. AS3Commons Logging • Logging API abstraction over existing logging APIs • Wrappers for Flex logger, etc • Write your own logger/wrapper: SOS, De Monster Debugger, ...
  7. 7. AS3Commons Logging - Example var logger:ILogger = LoggerFactory.getClassLogger(MyClass); logger.debug(“A log statement: {0} {1}”, myVar, myVar2); logger.info... logger.warn... logger.error... logger.fatal... Writes to standard ouput console using trace() You can also log objects (Monster Debugger)
  8. 8. AS3Commons Logging - Example // disable logging LoggerFactory.loggerFactory = null; OR // redirect logging to Flex logger LoggerFactory.loggerFactory = new FlexLoggerFactory(); var traceTarget:TraceTarget = new TraceTarget(); Log.addTarget(traceTarget);
  9. 9. AS3Commons Lang General AS3 utilities
  10. 10. AS3Commons Lang • ArrayUtils, ObjectUtils, StringUtils, ... • Basic support for missing AS3 concepts: equality, string builders, comparing, enums, assertions, etc • Enums: allows mapping to java enums through BlazeDS • Assertions: fail fast
  11. 11. AS3Commons Lang – Enum public class Color extends Enum { public static const RED:Color = new Color(“RED”); public static const GREEN:Color = new Color(“GREEN”); public static const BLUE:Color = new Color(“BLUE”); public function Color(name:String) { super(name); } }
  12. 12. AS3Commons Lang – Equals public function equals(other:*):Boolean { if (this === other) { return true; } if (!(other is ThisClass)) { return false; } var other:ThisClass = ThisClass(other); return new EqualsBuilder() .append(prop, other.prop) .equals; }
  13. 13. AS3Commons Reflect Introspection
  14. 14. AS3Commons Reflect • Get class/object information at runtime • Clear API on top of describeType, no XML
  15. 15. AS3Commons Reflect - Example var type:Type = Type.forClass(String); for each (var method:Method in type.methods) { // do something really useful with method // e.g. Invoke method dynamically } OR var type:Type = Type.forInstance(myString); var type:Type = Type.forName(“String”);
  16. 16. AS3Commons Collections Data structures
  17. 17. AS3Commons Collections • New data structures and iterators • Lists, maps, sets, linked collections, sorted collections
  18. 18. AS3Commons Concurrency Pseudo threading
  19. 19. AS3Commons Concurrency • Pseudo threading • Break up CPU intensive operations in “Runnables” • Execute these runnables without blocking the UI (or at least not completely) • Asynchronous
  20. 20. AS3Commons Serialization XML serialization
  21. 21. AS3Commons Serialization • XML serialization and deserialization • Based on XStream • Write custom node (de)serializers
  22. 22. AS3Commons Bytecode Ehmmm…. What?
  23. 23. What it is • Reads ABC bytecode and converts it into an object model • This object model can be queried for all sorts of interesting data • It can generate ABC bytecode and load it into the VM
  24. 24. How? var ds:AbcDeserializer = new AbcDeserializer(byteStream); var abcFile:AbcFile = ds.deserialize(); Boring…
  25. 25. Bytecode based reflection ByteCodeType.fromLoader(Application.loaderInfo); var typeCache:TypeCache = ByteCodeType.getTypeProvider().getTypeCache(); for each (var key:String in keys) { var type:ByteCodeType = typeCache.get(key) as ByteCodeType; /* ... do something... */ }
  26. 26. Class list var definitionNames:Array = typeCache.definitionNames;
  27. 27. Metadata lookup var metaDataLookup:Object = ByteCodeType.metaDataLookupFromLoader(loader); var definitionNames:Array = metaDataLookup['Mixin']; Spring ActionScript’s Class scanning system uses this.
  28. 28. Class generation var abcBuilder:IAbcBuilder = new AbcBuilder(); var packageBuilder:IPackageBuilder = abcBuilder.definePackage("com.classes.generated"); var classBuilder:IClassBuilder = packageBuilder.defineClass("RuntimeClass");
  29. 29. Adding properties var propertyBuilder:IPropertyBuilder = classBuilder.defineProperty("name","String","def aultName");
  30. 30. Adding methods var methodBuilder:IMethodBuilder = classBuilder.defineMethod("multiplyByHundred"); var argument:MethodArgument = methodBuilder.defineArgument("int"); methodBuilder.returnType = "int";
  31. 31. Adding opcodes 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);
  32. 32. Loading the class into the VM abcBuilder.addEventListener(Event.COMPLETE, loadedHandler); abcBuilder.addEventListener(IOErrorEvent.IO_ERROR, errorHandler); abcBuilder.addEventListener(IOErrorEvent.VERIFY_ERROR, errorHandler); abcBuilder.buildAndLoad();
  33. 33. Instantiating the class var className:String = “com.generated.RuntimeClass”; var dom:ApplicationDomain = ApplicationDomain.currentDomain; var clazz:Class = dom.getDefinition(className) as Class; var instance:Object = new clazz(); var i:int = instance.multiplyByHundred(10); // i == 1000
  34. 34. The road ahead… • Manipulation of existing classes • Foundation for AS3Commons Aspect • All sorts of other goodies
  35. 35. AS3Commons • www.as3commons.org • www.springactionscript.org • Feel free to join us! • @as3commons • @mechhead (Roland) • @herrodius (Christophe)

×