Clone a object with type cast
January 29th, 2009
Wheel, I’m not writing another way of do it…
Is the same way that ObjectUtils of Flex Framework do, but… Imagine that we want to duplicate one object of the class Example, we can just do the clone with a code like this:
public function deepClone ( obj : * ) : *
{
var bytes : ByteArray = new ByteArray();
bytes.writeObject(obj);
bytes.position = 0;
return bytes.readObject();
}
Let’s first imagine our Example class like this:
package com.filipesilvestrim
{
public class Example
{
public var name : String;
public function Example () {}
}
}
Ok, but now if we try get the object with the cast like trace((deepClone(obj) as Example)). Man… shame on you, you’ll get “null”. but why? It occurs because when we are cloning the class we do it with the ByteArray and when you return that’s “new” bytes as an Object, the Flash Player don’t know that those bytes are affiliate with the class Example. So how to solve it?
Let’s register in the Flash Player the class Example, to that when it saw the bytes being cast, it understand that those bytes mean that class. To register we need to this:
registerClassAlias("com.filipesilvestrim.Example", Example);
And the code working will be like this:
package com.filipesilvestrim
{
import flash.net.registerClassAlias;
import com.filipesilvestrim.Example;
public class MainClone
{
public function MainClone ()
{
var obj : Example = new Example();
registerClassAlias("com.filipesilvestrim.Example", Example);
trace("Cloned Object with cast = " + (deepClone(obj) as Example)); // must trace "Cloned Object with cast = [object Example]"
}
private function deepClone ( obj : * ) : *
{
var bytes : ByteArray = new ByteArray();
bytes.writeObject(obj);
bytes.position = 0;
return bytes.readObject();
}
}
}
So, lets code and have fun

[...] to work. When I casted bytes.readObject() it always returned null. I found a solution for that on this blog. I put it all together in one [...]
Pingback by Easy deep clone method for ActionScript 3.0 | Rozengain.com - New Media Development Blog — February 10, 2009 @ 1:11 PM
Gosh…
Why didnt I ever realized that??? Great idea Filipe :0)
Comment by Gabriela — March 2, 2009 @ 5:27 AM
Ola, tava estudando sobre o metodo registerClassAlias e tava com uma duvida.
Se você importa a classe:
import com.filipesilvestrim.Example;
por que usar o metodo
registerClassAlias(”com.filipesilvestrim.Example”, Example);
qual é a verdadeira ultilidade deste metodo?
Eu pensava que ele servia para importar a classe dinamicamente sem precisar do import?
é possivel me exclarecer esta duvida?
Comment by Joisiney — June 1, 2009 @ 9:51 PM
Cara,
importar a classe não significa que o interpretador/compilador entenderá o conteúdo binário da classe Example.
Quando tu esta clonando um elemento tu clona ele como binário e a referência binária de alias dele, ou apelido, é o caminho completo. Então para que o compilador entenda que o conteúdo binário que referencia o caminho é realmente a classe, necessitamos a utilização do registerClassAlias.
Comment by Filipe Silvestrim — July 11, 2009 @ 4:09 PM
Great! thanks for the sharing.
Comment by Heng Li — December 17, 2009 @ 8:01 PM