>
Runtime class instantiation in Actionscript 3.0
Welcome, Guest

by pooja on Saturday, 28 November 2009
Runtime class instantiation in Actionscript 3.0


Many times we come across a situation where we need to create the instances of a class at runtime. And all we have is the name of the class we need the object of as a string. Here’s a simple example which actually instantiates the class at runtime :

import flash.utils.getDefinitionByName;

import com.acme.AClass;

var testobj:AClass;

var clazz:Class = Class(getDefinitionByName("com.acme.AClass"));

testobj = new clazz();

 

However, actionScript requires that the Class be referenced in the code that calls getDefinitionByName(), or at least in the application that loads that code. That makes it a little harder to dynamically load and instantiate classes. It’s not enough that the class be imported, since that’s only a compile-time directive. The class must be referenced in the code explicitly, as in the line “var testObj:AClass” above. This sheds a little light on the notion of ActionScript’s “classpath”: Only classes referenced explicitly are compiled into the swf – so only those classes are available for runtime instantiation.

How do you get around that? What if you don’t know which classes you want to load while you’re developing? Well, there are some things I know you can do, and I’m sure there are some other routes available beyond those. One thing you could do is get a rough idea which classes you’ll need and, somewhere in your application, include and reference them all.  Another is to load a swf that you know references the classes you need. All that’s necessary is that the swf is loaded before getDefinitionByName is called. 

There is a third option that makes it possible to instantiate a class at runtime that you is NOT referenced anywhere in your application.

Here’s how:

1. put the classes that you want to instantiate at runtime in a Flex Library Project.

2. In the project for you application, add the .swc the library path.

3. In the compiler arguments for your application add -include-libraries

The -include-libraries compiler argument tells the compiler that you want to load all of the classes in the library, even the ones that are not referenced anywhere.

Once you’ve done this, you will be able to load any of the classes in the library at runtime.

 

 



Blogger Image
My Blog Title

Product Engineering, software engineering company, Product Development, Product Migration, Product Re-engineering, Product Maintenance, Product Testing Commercial Application Development, Business Software development, commercial software for startups, Application Support and Maintenance, software testing Product Maintenance, Outsource product maintenance, product support and maintenance Product Migration, Product Re-engineering, product re-engineering services Product Research, Product Engineering, UI Prototyping Services Software Testing Services, Quality Assurance services, professional software testers, Load Testing, Functional Testing, Cross Platform, Browser Testing, Test Automation, Testing Tools, software quality analysis Functional Testing Services, software quality analysis, Software Testing Services, Application Testing Services, Functional Testing Types Automated Testing, Automated Testing Services, automation testing, test script development, Automation Test Tools, outsource automation testing Load Testing, Performance Testing Services, Load Testing Tools Offshore Software Development, Outsource software services, offshore outsourcing services, offshore software development services, IT outsourcing services, software quality assurance services, Offshore IT services, Custom Application Development Services, Offshore Product Engineering Benefits of IT Outsourcing, Offshore Software Development companies, offshore software development firms Outsource planning, IT outsourcing, IT development services, offshore IT companies, offshore software development Offshore Software Development, Outsource software services, offshore outsourcing services, offshore software development services, IT outsourcing services, software quality assurance services, Offshore IT services, Custom Application Development Services, Offshore Product Engineering Offshore Software Development, Outsource software services, offshore outsourcing services, offshore software development services, IT outsourcing services, software quality assurance services, Offshore IT services, Custom Application Development Services, Offshore Product Engineering