1 Reply Latest reply on Jun 18, 2010 5:01 AM by liushuiluohua666

    Is it a bug get Class when use getDefinition

    liushuiluohua666

      I load swf lib file  in a application .The swf has three MovieClip, MovieClip Lib1 bind Class Lib1 ,MovieClip Lib2 bind Class Lib2,and MovieClip Lib3 bind Class Lib3. In the stage has a Lib3 instance. Lib3 has a instance Lib1 and Lib2.

      Then I load this swf to my application, and use getDefinition("Lib3")  get Lib3 .I try three type and two success. but one type can get the Lib3 ,but throw Error. can't turn MovieClip instance  to  Lib1 or Lib2.

      demo like this:

      ClassLoader.as

       

      package
      {
       import flash.display.Loader;
       import flash.errors.IllegalOperationError;
       import flash.events.Event;
       import flash.events.EventDispatcher;
       import flash.events.IOErrorEvent;
       import flash.events.SecurityErrorEvent;
       import flash.net.URLRequest;
       import flash.system.ApplicationDomain;
       import flash.system.LoaderContext;
       public class ClassLoader extends EventDispatcher {
           public static var CLASS_LOADED:String = "classLoaded";
           public static var LOAD_ERROR:String = "loadError";
           public var loader:Loader;
           private var swfLib:String;
           private var loadedClass:Class;
           public function ClassLoader() {
               loader = new Loader();
           }
           public function load(lib:String):void {
               swfLib = lib;
               var request:URLRequest = new URLRequest(swfLib);
               var context:LoaderContext = new LoaderContext();
               context.checkPolicyFile=false;
               context.applicationDomain=ApplicationDomain.currentDomain;
               //trace(ApplicationDomain.currentDomain==ApplicationDomain.currentDomain);
               loader.contentLoaderInfo.addEventListener(Event.COMPLETE,completeHandler);
               loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR,ioErrorHandler);
               loader.contentLoaderInfo.addEventListener(SecurityErrorEvent.SECURITY_ERROR,securityErrorHandler);
               loader.load(request,context);
           }
           private function completeHandler(e:Event):void {
            loader.contentLoaderInfo.removeEventListener(Event.COMPLETE,completeHandler);
               loader.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR,ioErrorHandler);
               loader.contentLoaderInfo.removeEventListener(SecurityErrorEvent.SECURITY_ERROR,securityErrorHandler);
               dispatchEvent(new Event(ClassLoader.CLASS_LOADED));
           }
       
           private function ioErrorHandler(e:Event):void {
            loader.contentLoaderInfo.removeEventListener(Event.COMPLETE,completeHandler);
               loader.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR,ioErrorHandler);
               loader.contentLoaderInfo.removeEventListener(SecurityErrorEvent.SECURITY_ERROR,securityErrorHandler);
               dispatchEvent(new Event(ClassLoader.LOAD_ERROR));
           }
       
           private function securityErrorHandler(e:Event):void {
            loader.contentLoaderInfo.removeEventListener(Event.COMPLETE,completeHandler);
               loader.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR,ioErrorHandler);
               loader.contentLoaderInfo.removeEventListener(SecurityErrorEvent.SECURITY_ERROR,securityErrorHandler);
               dispatchEvent(new Event(ClassLoader.LOAD_ERROR));
           }
           public function hasClass(className:String):Boolean{
            return loader.contentLoaderInfo.applicationDomain.hasDefinition(className);
           }
           public function getClass(className:String):Class {
               try {
                return loader.contentLoaderInfo.applicationDomain.getDefinition(className) as Class;
               } catch (e:Error) {
                   throw new IllegalOperationError(className + " definition not found in " + swfLib);
               }
               return null;
           }
       }
      }
      


      ApplicationDomainExample.as

       

      package {
       import flash.display.Sprite;
       import flash.events.Event;
       public class ApplicationDomainExample extends Sprite
       {
        private var loader:ClassLoader;
        private var loader2:ClassLoader;
              public function ApplicationDomainExample() {
                  loader = new ClassLoader();
                  loader.addEventListener(ClassLoader.CLASS_LOADED,classLoadedHandler);
                  loader.load("http://127.0.0.1/lib.swf");
                  loader2 = new ClassLoader();
                  loader2.addEventListener(ClassLoader.CLASS_LOADED,classLoadedHandler2);
                  /* this will success */
                  //loader2.load("http://127.0.0.1/lib2.swf");
      
                  /*
                     if loader and loader2 load in same method getClass("Lib3") and new Lib3 instance success;
                      if after the loader load success loader2 load another any swf. I use getClass("Lib3") will
                      throw error;
                     */
              }
              private function classLoadedHandler(e:Event):void {
               loader.removeEventListener(Event.COMPLETE,classLoadedHandler);
               getUI(loader);
               loader2.load("http://127.0.0.1/lib2.swf");  //load any swf inclue himself will error
               /* if load in this will success i think because they in same ApplicationDomain i can understand this */
               //loader2.load("lib.swf");
              }
              
              private function classLoadedHandler2(e:Event):void {
               loader2.removeEventListener(Event.COMPLETE,classLoadedHandler2);
               getUI(loader);
              }
              private function getUI(l:ClassLoader):void{
                if(l.hasClass("Lib3")){
                var ui:Object=l.getClass("Lib3");
                   trace(new ui);
               } 
              }
          }
      }
      
      

       

                     there has two ClassLoader. loader will load the swf which has Lib1 and Lib2 and Lib3 .loader2 of ClassLoader loader another any swf.

                     in this example we use a empty swf.

                     if loader and loader2 load in same method getClass("Lib3") and new Lib3 instance success;

                      if after the loader load success loader2 load another any swf. I use getClass("Lib3") will

                      throw error;

                    if  a flash bug here???

       

      I can't put my attachments to here. so if you need  all of the code  I will send you by email.

      My Email liushuiluohua666@gmail.com