as3 数据深度克隆

今天闲来无事,为了做一个小功能,牵涉到数据的深度克隆,在折腾再三还是找到了一个最完美的方法(声明:代码都是引用的)。

第一次用的是byteArray+类注册做的,因为有复杂数据和vector和Array类型,最后克隆出来的数据类型丢失,代码如下:

  1. private static var aliasList:Array = [];
  2.                 /**
  3.                  * 复制一个对象
  4.                  * @param sourceObj        要复制的源对象
  5.                  * @param deep                是否对对象进行AMF序列化以后再深度复制
  6.                  * @return
  7.                  */
  8.                 public static function clone(sourceObj:*, deep:Boolean=true):*
  9.                 {
  10.                         if(!sourceObj)
  11.                                 return null;
  12.                         var lastTime:Number = getTimer();
  13.                         if(deep)
  14.                         {
  15.                                 // 得到  sourceObj的类名
  16.                                 var qualifiedClassName:String = getQualifiedClassName(sourceObj);
  17.                                 if(aliasList.indexOf(qualifiedClassName) == -1)
  18.                                 {
  19.                                         // e.g  com.gdlib.test::RegisterClassAliasTest
  20.                                         var packageName:String = qualifiedClassName.replace("::", ".");
  21.                                         // 得到 sourceObj的类的类定义
  22.                                         var classType:Class = getDefinitionByName(qualifiedClassName) as Class;
  23.                                         // 注册此别名和类
  24.                                         if(classType)
  25.                                         {
  26.                                                 registerClassAlias(packageName, classType);
  27.                                                 aliasList.push(qualifiedClassName);
  28.                                                 trace("register class", packageName);
  29.                                         }
  30.                                         // 注册类公共属性(如果是复合类)
  31.                                         registerVariables(sourceObj);
  32.                                 }
  33.                         }
  34.                         // 复制此对象
  35.                         var b:ByteArray = new ByteArray();
  36.                         b.writeObject(sourceObj);
  37.                         b.position = 0;
  38.                         trace("Clone object takes", getTimer()-lastTime, "ms.\n");
  39.                         return b.readObject();
  40.                 }
  41.                 /**
  42.                  * 注册某个类的公共属性(如果是复合类)所属的类的别名.
  43.                  * @param sourceObj
  44.                  */
  45.                 private static function registerVariables(sourceObj:*):void
  46.                 {
  47.                         // 注册类公共属性(如果是复合类)
  48.                         var xml:XML = describeType(sourceObj);
  49.                         var variable:XML;
  50.                         var variableType:String;
  51.                         var variablePackageName:String;
  52.                         var variableClassType:Class;
  53.                         var variableXml:XMLList;
  54.                         if(sourceObj is Class)
  55.                                 variableXml = xml.factory.variable;
  56.                         else
  57.                                 variableXml = xml.variable;
  58. //                        <variable name="point" type="flash.geom::Point">
  59. //                                <metadata name="__go_to_definition_help">
  60. //                                  <arg key="pos" value="209"/>
  61. //                                </metadata>
  62. //                        </variable>
  63. //                        <variable name="name" type="String">
  64. //                                <metadata name="__go_to_definition_help">
  65. //                                  <arg key="pos" value="142"/>
  66. //                                </metadata>
  67. //                          </variable>
  68.                         for each (variable in variableXml)
  69.                         {
  70.                                 variableType = variable.@type;
  71.                                 if(variableType.indexOf("::") != -1)
  72.                                 {
  73.                                         if(aliasList.indexOf(variableType) == -1)
  74.                                         {
  75.                                                 // "flash.geom::Point"        转换为         "flash.geom.Point"
  76.                                                 variablePackageName = variableType.replace("::", ".");
  77.                                                 variableClassType = getDefinitionByName(variableType) as Class;
  78.                                                 // 注册此别名和类
  79.                                                 if(variableClassType)
  80.                                                 {
  81.                                                         registerClassAlias(variablePackageName, variableClassType);
  82.                                                         registerVariables(variableClassType);
  83.                                                         aliasList.push(variableType);
  84.                                                         trace("register variable", variablePackageName);
  85.                                                 }
  86.                                         }
  87.                                 }
  88.                         }
  89.                 }

于是乎,继续找结果找到了以下的方法:

首先,这个需要引用类库as3-commons-reflect(比较详细的解说可以看这:http://topmanopensource.iteye.com/blog/491080  下载类库地址:http://www.as3commons.org/as3-commons-reflect/

/**
* 深度clone某个对象,包含Vector 和 Array
*/
public static function clone (_obj : * ): *
{
var returnCloneObj :*;
var objType : Type = Type . forInstance (_obj );
if (_obj is Vector .<*> || _obj is Array )
{
returnCloneObj = new objType . clazz ();
for each (var eachCloneObj :* in _obj )
{
returnCloneObj . push (cloneObj (eachCloneObj ));
}
}
else
{
returnCloneObj = cloneObj (_obj );
}
return returnCloneObj ;
}
/**
* clone 只能clone简单对象的所有的public属性
*/
private static function cloneObj (_obj : * ): *
{
var objType : Type = Type . forInstance (_obj );
var cloneObj :* = new objType . clazz ();
for each (var field : Field in objType . properties )
{
if (field is Variable || field is Accessor && Accessor (field ). writeable && field . name != 'prototype' )
{
cloneObj [ field . name ] = _obj [ field . name ];
}
}
return cloneObj ;
}

 

/**
* 深度clone某个对象,包含Vector 和 Array
*/
public static function clone (_obj : * ): *
{
var returnCloneObj :*;
var objType : Type = Type . forInstance (_obj );
if (_obj is Vector .<*> || _obj is Array )
{
returnCloneObj = new objType . clazz ();
for each (var eachCloneObj :* in _obj )
{
returnCloneObj . push (cloneObj (eachCloneObj ));
}
}
else
{
returnCloneObj = cloneObj (_obj );
}
return returnCloneObj ;
}
/**
* clone 只能clone简单对象的所有的public属性
*/
private static function cloneObj (_obj : * ): *
{
var objType : Type = Type . forInstance (_obj );
var cloneObj :* = new objType . clazz ();
for each (var field : Field in objType . properties )
{
if (field is Variable || field is Accessor && Accessor (field ). writeable && field . name != 'prototype' )
{
cloneObj [ field . name ] = _obj [ field . name ];
}
}
return cloneObj ;
}

 

终于可以万能的深度克隆各种数据了。

posted on 2014-03-21 17:42  L . W  阅读(238)  评论(0编辑  收藏  举报