as3commons introduction

35
AS3 Commons Roland Zwaga Christophe Herreman Stack&Heap

Upload: christophe-herreman

Post on 22-Apr-2015

3.875 views

Category:

Technology


0 download

DESCRIPTION

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

TRANSCRIPT

Page 1: AS3Commons Introduction

AS3 Commons

Roland ZwagaChristophe Herreman Stack&Heap

Page 2: AS3Commons Introduction

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

Page 3: AS3Commons Introduction

Why AS3Commons?

• Central place for AS3 utility libraries• Assure quality of libraries• Unit tested• Started with core parts of the Spring

ActionScript project

Page 4: AS3Commons Introduction

Libraries in AS3Commons?

• Bytecode• Lang• Logging• Reflect• Collections• Concurrency• Serialization• AS3Parser , Async, EventBus (coming up)

Page 5: AS3Commons Introduction

AS3-commons-logging

Logging API abstraction

Page 6: AS3Commons Introduction

AS3Commons Logging

• Logging API abstraction over existing logging APIs

• Wrappers for Flex logger, etc• Write your own logger/wrapper: SOS, De

Monster Debugger, ...

Page 7: AS3Commons Introduction

AS3Commons Logging - Examplevar 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)

Page 8: AS3Commons Introduction

AS3Commons Logging - Example

// disable loggingLoggerFactory.loggerFactory = null;

OR

// redirect logging to Flex loggerLoggerFactory.loggerFactory = new FlexLoggerFactory();

var traceTarget:TraceTarget = new TraceTarget();

Log.addTarget(traceTarget);

Page 9: AS3Commons Introduction

AS3Commons Lang

General AS3 utilities

Page 10: AS3Commons Introduction

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

Page 11: AS3Commons Introduction

AS3Commons Lang – Enumpublic 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);

}

}

Page 12: AS3Commons Introduction

AS3Commons Lang – Equalspublic 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;

}

Page 13: AS3Commons Introduction

AS3Commons Reflect

Introspection

Page 14: AS3Commons Introduction

AS3Commons Reflect

• Get class/object information at runtime• Clear API on top of describeType, no XML

Page 15: AS3Commons Introduction

AS3Commons Reflect - Examplevar 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”);

Page 16: AS3Commons Introduction

AS3Commons Collections

Data structures

Page 17: AS3Commons Introduction

AS3Commons Collections

• New data structures and iterators• Lists, maps, sets, linked collections, sorted

collections

Page 18: AS3Commons Introduction

AS3Commons Concurrency

Pseudo threading

Page 19: AS3Commons Introduction

AS3Commons Concurrency

• Pseudo threading• Break up CPU intensive operations in

“Runnables”• Execute these runnables without blocking the

UI (or at least not completely)• Asynchronous

Page 20: AS3Commons Introduction

AS3Commons Serialization

XML serialization

Page 21: AS3Commons Introduction

AS3Commons Serialization

• XML serialization and deserialization• Based on XStream• Write custom node (de)serializers

Page 22: AS3Commons Introduction

AS3Commons Bytecode

Ehmmm…. What?

Page 23: AS3Commons Introduction

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

Page 24: AS3Commons Introduction

How?

var ds:AbcDeserializer = new AbcDeserializer(byteStream);

var abcFile:AbcFile = ds.deserialize();

Boring…

Page 25: AS3Commons Introduction

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... */

}

Page 26: AS3Commons Introduction

Class list

var definitionNames:Array = typeCache.definitionNames;

Page 27: AS3Commons Introduction

Metadata lookupvar metaDataLookup:Object =

ByteCodeType.metaDataLookupFromLoader(loader);

var definitionNames:Array = metaDataLookup['Mixin'];

Spring ActionScript’s Class scanning system usesthis.

Page 28: AS3Commons Introduction

Class generationvar abcBuilder:IAbcBuilder = new AbcBuilder();

var packageBuilder:IPackageBuilder = abcBuilder.definePackage("com.classes.generated");

var classBuilder:IClassBuilder = packageBuilder.defineClass("RuntimeClass");

Page 29: AS3Commons Introduction

Adding propertiesvar propertyBuilder:IPropertyBuilder =

classBuilder.defineProperty("name","String","defaultName");

Page 30: AS3Commons Introduction

Adding methodsvar methodBuilder:IMethodBuilder =

classBuilder.defineMethod("multiplyByHundred");

var argument:MethodArgument = methodBuilder.defineArgument("int");

methodBuilder.returnType = "int";

Page 31: AS3Commons Introduction

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);

Page 32: AS3Commons Introduction

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();

Page 33: AS3Commons Introduction

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

Page 34: AS3Commons Introduction

The road ahead…

• Manipulation of existing classes• Foundation for AS3Commons Aspect• All sorts of other goodies

Page 35: AS3Commons Introduction

AS3Commons

• www.as3commons.org• www.springactionscript.org

• Feel free to join us!

• @as3commons• @mechhead (Roland)• @herrodius (Christophe)