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 ;)

 

8 Responses to Clone a object with type cast

  1. [...] 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 [...]

  2. Gabriela says:

    Gosh…
    Why didnt I ever realized that??? Great idea Filipe :0)

  3. Joisiney says:

    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?

  4. Filipe Silvestrim says:

    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.

  5. Heng Li says:

    Great! thanks for the sharing.

  6. baking toys says:

    That was stimulating . I admire your finesse that you put into your work. Please do move forward with more like this.

  7. Bettie says:

    You have shed a ray of susinhne into the forum. Thanks!

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>