Last active
May 26, 2016 08:40
-
-
Save MindScriptAct/2205739 to your computer and use it in GitHub Desktop.
Class to clone deep AS3 objects preserve types.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package utils.data { | |
import flash.utils.describeType; | |
import flash.utils.getDefinitionByName; | |
import flash.utils.getQualifiedClassName; | |
/** | |
* @author http://blog.another-d-mention.ro/programming/how-to-clone-duplicate-an-object-in-actionscript-3/, | |
* Modified by Raimundas Banevicius | |
*/ | |
public class ObjectHelper { | |
/** | |
* Returns clone of provided object. | |
* @param source | |
* @return Cloned object | |
*/ | |
public static function clone(source:Object):Object { | |
var retVal:Object; | |
if (source) { | |
retVal = createEmptyClass(source); | |
if (retVal) { | |
copyData(source, retVal); | |
} | |
} | |
return retVal; | |
} | |
/** | |
* Creates empty class of same type as provided object. | |
* @param sourceObj object to create empty class from. | |
* @return new empty class. | |
*/ | |
public static function createEmptyClass(sourceObj:Object):* { | |
var retVal:*; | |
if (sourceObj) { | |
try { | |
var classOfSourceObj:Class = getDefinitionByName(getQualifiedClassName(sourceObj)) as Class; | |
retVal = new classOfSourceObj(); | |
} catch (error:Error) { | |
trace("ObjectHelper ERROR !!! failed to create new empty class[type:" + classOfSourceObj + "] out of object.[object:" + sourceObj + "]"); | |
} | |
} | |
return retVal; | |
} | |
/** | |
* Copies data from one object to another. | |
* @param source copy data to this object. | |
* @param destination copy data from this object. | |
*/ | |
// TODO : add neste object, arrays, vector support. | |
public static function copyData(source:Object, destination:Object):void { | |
//copies data from commonly named properties and getter/setter pairs | |
if ((source) && (destination)) { | |
try { | |
var sourceInfo:XML = describeType(source); | |
var prop:XML; | |
for each (prop in sourceInfo.variable) { | |
if (destination.hasOwnProperty(prop.@name)) { | |
destination[prop.@name] = source[prop.@name]; | |
} | |
} | |
for each (prop in sourceInfo.accessor) { | |
if (prop.@access == "readwrite") { | |
if (destination.hasOwnProperty(prop.@name)) { | |
destination[prop.@name] = source[prop.@name]; | |
} | |
} | |
} | |
} catch (error:Error) { | |
trace("ObjectHelper ERROR !!! failed to copy data from[" + source + "] to [" + destination + "]. error:" + error); | |
} | |
} | |
} | |
public static function getTraceString(obj:Object, useIndent = false):String { | |
return traceObject(obj, "", useIndent); | |
} | |
private static function traceObject(obj:Object, identStr:String, useIndent:Boolean):String { | |
var retVal:String = ""; | |
if (obj is String) { | |
retVal = obj as String; | |
} else if (obj is Boolean || obj is int || obj is uint || obj is Number) { | |
retVal = obj.toString(); | |
} else { | |
retVal += "{"; | |
var isFirst:Boolean = true; | |
for (var id:String in obj) { | |
if (isFirst) { | |
isFirst = false; | |
} else { | |
retVal += ", "; | |
} | |
retVal += id + ":" + traceObject(obj[id], identStr, useIndent); | |
} | |
retVal += "}"; | |
} | |
return retVal; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment