Deep Copy

Hi all,
I have a question I can't find any documentation about, not on google, not here on the forums. We are developping a EJB J2EE application (with glassfish) with flex as the view. We are using BlazeDS to interchange objects back and forth between flex and java.
The problem we are having is we have to do a deep copy of an object tree, but blaze by default copies shallow.
The setup is like this:
We generated the actionscript classes that represent the java classes on the client side with a code generator named Gas3 (the only one out there, no?). This generator created classes with RemoteClass and Bindable annotations already in place. Gas3 also implemented the IExternalizable interface but we threw that out because it gave us problems in java. We have our EJB services configured in the remoting-config.xml file and everything works except for deep copy so I don't think there is anything missing, no?
Is somebody aware of a way we can achieve a deep copy? I imagine there would be an xml tag or attribute that would allow for this ...
Any help would be greatly appreciated!
Thank you,
Bruno Windels
3S

Or is it creating a deep copy of each element in the Array and placing them into a new array object?

Similar Messages

  • Creating a deep copy

    I wrote a stack implementation using a LinkedList, I have null constructor to create the first stack and it works fine but now i have to create a copy for a second stack using the element of the first constructor
    this my code
    import java.util.LinkedList;
    public class Stack<T>{
      private LinkedList<T> stack;
      public Stack(){
        stack =  new LinkedList<T>();
      public Stack(Stack<T> s) {// the problem is here it compile but when i run
                                                     // the program the second stack raised an exception because is empty
                                                     // i want to create a deep copy with the elements of the first stack
         stack = new LinkedList<T>();
      public boolean isEmpty(){
      return stack.isEmpty();
      public void push(T item){
       /*method to add object information
        *pre the object is not full
        * pos: the object is inserted
          stack.addFirst(item);
      public T top() throws EmptyStackException{
        if (!stack.isEmpty()){ 
          return stack.getFirst();
        else{
          throw new EmptyStackException("StackException on top " + " Stack empty");
      public T pop() throws EmptyStackException{
        /*method to remove an element of the stack
        *pre the object is not empty
        * pos: the object is remove
        * response throws an exception
        if (!stack.isEmpty()){ 
          return stack.removeFirst();
        else{
          throw new EmptyStackException("StackException on top" + "stack empty");
    public int numElements(){
        return stack.size();
    public void clear(){
      stack.clear();
    public class EmptyStackException extends Exception{
            public EmptyStackException( String message){
              super(message);
    }

    wel well... are you doing this on a mobile phone???
    neways to get to ur question u can use the clone() method to generate a clone of your object which is the same as a deep copy for eg:
    public class CloneTest{
        CloneTest test1 = new CloneTest();
        CloneTest test2;
        CloneTest(){
          //Some initialization code;
        CloneTest(CloneTest temp){
             test2 = test1.clone();
    }clone is a method of the object class.

  • Help with a deep copy

    What is the best practice for sending doing a deep copy between two View Controllers in an iPhone app. I've designed an app that sends over arrays of arrays and the deeper items aren't coming over as easily as I thought. Do you explicitly copy? Just alloc placeholders and set them equal? Links to any samples would be greatly appreciated.
    Is it a best practice to avoid that kind of data passing - as being overly complex?
    Background:
    I'm building an iPhone application where I need to send a complex object from one view controller to another - This complex object is an array of arrays of objects.
    I can't just copy a reference to the complex object over to a variable on the receiving view controller, as none of the nested array contents come over.
    So I think I need to do a deep copy - but need advice on best practices for that. The array of arrays contain objects, so ideally, Id just be able to :
    Thanks for any advice!

    There are many ways of solving this.
    One way is to centralize the information:
    You create a singleton class that is the datasource for both view, so you don't need to pass in anything. If you can't do that because the datasources are generally different except for this specific array of arrays, then make the singleton class be the source of that array and nothing else.

  • Clarification regarding deep cloning/deep copy

    Hi,
    While performing deep copy of an object should all the subobject that the parent object constitute also constitute the clone method.
    For Eg:
    I have a class called Document and a class called Field. Document constitutes of List<Field> fields. Field class has a String fieldName as its member variable.
    I need to perform a deep copy of Document object. Should Field class also implement the clone method or can I create field objects in the documents clone method and fill these fields with values from the existing field list to form a new Document with these new fields.
    Or do I call clone() method on each field to get a new field object and
    add it to the new Document object that I am creating in the clone() method .
    //First approach
    public Document clone(){
    Document doc = new Document();
    List<Field> fields = doc.getFields()
    Field newField = null;
    for(Field f: fields){
        newField = new Field();
        newField.setName(f.getName());
       doc.addField(newField);
    return doc;
    //Second Approach
    public Document clone(){
    Document doc = new Document();
    List<Field> fields = doc.getFields()
    Field newField = null;
    for(Field f: fields){
        newField = f.clone()
       doc.addField(newField);
    return doc;
    }Please let me know which is a better approach and the standard coding
    practise.
    Thanks

    the second method is better, because it lets you the possibility to create a subclass of Field with a specific clone method, integrate some instances in a Document object; without changing the Document.clone method.

  • How to deep copy a component configuration?

    Hi Experts,
    I want to deep copy a component configuration named EHHSS_RAS_OIF.
    But following was the error shown.
    Can you please help/guide to do the same.
    Thanks in advance.
    Sanket.

    Hi Sanket,
    I think you might be copying the config in Unit test client but not in Development client.
    Thanks
    KH

  • Deep copy of String

    As String is called by reference, how can I perform deep copy?
    Anything similar to System.arraycopy for String?

    That's funny, this works for me:
    String k = "kkkkkkkk";
    String m = k;
    Now, when I change 'k' (f.i. k = k.substring(0, 2); )
    'm' does not change.
    So that's a deep copy!
    ;JOOP!No - it's not a deep copy. Your statement k = k.substring(0, 2) does not change the string that was referenced by k previously, but changes the k variable itself so that it references a new string.
    It's really important to get a handle on the difference between objects
    on the one hand, and references to them on the other. Without that understanding you have little hope of writing correct and efficient Java programs.
    Sylvia.

  • Shallow copying and Deep copying ???

    hi,
    what is meant by shallow copying and deep copying in context to clone() method found in Object class?
    thanks,
    swapnaja

    swapnaja wrote:
    Shallow copying: only cloneable object is copied but any object references within that object would not be copied but would rather be shared.
    Deep copying: cloneable object along with any object references contained within that object will also be copied.
    Am i right?Yep; when you want to make a deep copy you have to watch out for circular references, i.e. if you have made a copy of an object already you shouldn't make another one during the deep copying process; (see how the ObjectOutputStream does things).
    kind regards,
    Jos

  • Shadow copy vs deep copy

    hi,
    any body tell me what is the difference between shadow copy and deep copy of a object.

    would you tell me any prectical example of this. i
    mean when i should use shallow copy and when i should
    use deep copy of object.I'm hoping someone else will step in here, because in my OO thinking, there's seldom a need for copying an object at all.
    One practical example, though: Say you're copying a list of objects. After the copy, you want to alter the objects in the copied list, but you don't want the objects in the original list altered. In this case, you need a deep copy. Had you only done a shallow copy, both lists would have referred to the same objects, and therefore, any change in an object would be visible through both lists.
    clone() method makes shallow copy or deep copy.You'd have to consult the doc for the class you are cloning. For example, the doc for LinkedList.clone() says "Returns a shallow copy of this LinkedList."

  • How does deep copy apply to arrays?

    I have a service object that has a method that returns an array of
    objects. The return type for the method is defined with the copy option.
    I found documentation that states that the copy option creates a deep
    copy of the return variable on the partition that called the method.
    My question is: If the return type for the method is an array of
    objects, will the copy option create copies of all objects/elements in
    the array?
    We wish to eliminate any object references to the service object's
    partition. Any insight would be greatly appreciated.
    Thanks in advance,
    Van Vuong
    Phone: 972.985.5289
    Pager: 972.320.2232
    VoiceNow Pager: 972.330.0822
    E-mail: [email protected]
    PAGE NET

    Copy option always copies deep. Remember, also if you pass the array
    accross partitions, whether you specify copy or not, it is going to copy
    and copy deep.
    In an array, I am not sure if have the problem, because unless the array
    in-turn holds a huge tree, the array object may be wide, but not deep.
    Some thing to think about??
    Venkat

  • ObjectUtil doesnt deep copy correctly

    I am using an ObjectProxy for a complex object containing
    arrays of objects. I bind a datagrid to the array and . notation to
    fields. This all works fine, datagrid behaiving properly, inserts
    and deletes working properly.
    I want to have an undo function that will put the objectproxy
    object back to its original state. So when i open the screen with
    the datagrid I do a ObjectUtil.copy() and use this latter if the
    user wants to revert changes. The problem is that the copy doesn't
    do a deep copy. In the Object is an ArrayCollection of userdefined
    objects. These objects instead of being copied as their type are
    being copied as plain objects.
    The serialize meathod the copy uses doesn't retain the types
    of user defined objects.
    Will I have to create clone or copy methods for every object
    type I have?
    If so do I just need to make them ezxternalizable? How will
    the objectutil copy pick up these methods?
    Thanks,
    Jeff

    Unfortunatlly you will need to implement it for every class
    that has complex objects, as it does not know how to deal with
    them. Your classes need to extend EventDispatcher and/or implement
    IExternalizable.
    Then create methods writeExternal and readExternal for every
    local variables you need to retain. eg..
    public function writeExternal(output:IDataOutput):void {
    output.writeUTF(msgString);
    output.writeObject(anyObject);
    output.writeInt(id);
    public function readExternal(input:IDataInput):void {
    msgString = input.readUTF()as String;
    anyObject = input.readObject()as Array;
    id = input.readInt() as int;
    }

  • Deep copy a PriorityQueue

    Dear all,
    I am struggle with deep copying a priorityQueue. Anybody has a clue about his. It seems onyl swallow copy is allowed.
    Cheers,
    Ke

    See
    http://java.sun.com/docs/books/tutorial/java/IandI/objectclass.html
    http://www.google.com/search?num=100&hl=en&c2coff=1&q=java+deep+shallow+copy&btnG=Search

  • Deep copy cloning

    hi guys, i have an object which extends defaultmutabletreenode, basically it is a leaf on a tree and these leafes (not leaves then i guess but you know what i mean) have leaves themselves, as shown below, every node has an arraylist and these nodes also contain arraylists. i want to clone this object to see if it is quicker than re-creating it all of the time but i am a little hesitent as i am worried about getting a deep clone to work and unfortunately i couldnt find one that someone has already written.
    am i right in thinking that you cant simply clone mychildren, you then have to go to every chidl and clone its children also??
    below is the top of the class so you cans ee what it looks like...could anyone post a loop that would clone the tree or if it is a lot of trouble just a description of what i need to do would be great!
    public class Node extends DefaultMutableTreeNode
    private RootNode myRootNode;
    private ArrayList<Node> myChildren;
    thanks in advance to all who reply
    Danny =)

    Yeah in that article I'm pretty sure he talks about putting it through a PipedInputStream to a PipedOutputStream (or vice versa?). That should be considerably faster than writing to a disk.
    I think you're going to be better off writing your own deep copy method though, it's a pretty simple structure, not worth the extra overhead...
    p.s. Are you positive that you need to clone everything all the time?

  • XSLT: how to deep copy (including DOCTYPE, encoding, etc.)

    I've written the following stylesheet to copy an existing XML. during testing, i noticed that the DOCTYPE was not copied at all and the XML encoding defaultet to UTF-8 (though the original XML has ISO-8859-1). to fix this, i have to set the encoding and doctype-system hard coded:
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
        <!-- adjust doctype-system to generate a different DOCTYPE SYSTEM value: -->
        <xsl:output method="xml" encoding="ISO-8859-1" doctype-system="mine.dtd"/>
        <!-- copy nodes: -->
        <xsl:template match="node()|@*" >
           <xsl:copy>
                <xsl:apply-templates select="node()|@*" />
           </xsl:copy>
        </xsl:template>
    </xsl:stylesheet>but this results in a specialized copy-XSL. is there a generalized way to copy a XML with all content (including <! and <? tags, etc.)? (of course,

    The encoding of an XML document is not a significant feature. If you look at the XML specification you'll see that it defines an XML document as a sequence of Unicode characters. How you encode that in bytes is not part of the specification.
    That means that if you take a document and make one version of it encoded in UTF-8 and another version encoded in ISO-8859-1, those are both the same XML document.
    You'll notice that XSLT doesn't give you a way to find the encoding of any of its input documents, because it is irrelevant to XML. So if you want to treat the encoding as a significant feature, then that's nothing to do with XML. You'll have to find some other way to deal with it.

  • Deep Copy Performance

    hai guys..
    iam using this method given below for deepycopy .....is there any other method which can improve me the performance of deepcopying......i need it since my logic uses deepcopying many times
    public static Object copy(Object orig) {
    Object obj = null;
    try {
    // Write the object out to a byte array
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    ObjectOutputStream out = new ObjectOutputStream(bos);
    out.writeObject(orig);
    out.flush();
    out.close();
    // Make an input stream from the byte array and read
    // a copy of the object back in.
    ObjectInputStream in = new ObjectInputStream(
    new ByteArrayInputStream(bos.toByteArray()));
    obj = in.readObject();
    catch(IOException e) {
    e.printStackTrace();
    catch(ClassNotFoundException cnfe) {
    cnfe.printStackTrace();
    return obj;
    }

    Hi,
    I am using the same method than you to perform the DeepCopy.
    Of course that implementing cloneable on each of the fields used in the class and then implement clone in the main parent class like this:
    public MyClass clone(){
              MyClass newclass = new MyClass();
              newclass.setField( this.getField().clone() ) ;
              return newclass;
    }kind of thing....
    The problem is that if you have a complicated class, it is much easier to implement Serializable in the classes that you are going to use and try your luck.
    Is there an Eclipse shortcut to creting clone methods??
    That would be quite nice I think, cos it is really tedious to do by hand.
    Cheers,
    Alf

  • Copy objects ('deep copy')

    Could you help me please, I'm trying to perform a copy between object A and object B they are different instances of the same object. Object A contains other objects inside it and goes like this into a level of 4 or 5 object inside others. The purpose is to copy the attributes from objects B which have a value of null in the object A so I have to go thru all the objects and compare each attribute. I thougth about some solutions but they are to complex and difficult to implement, could you give some ideas...
    PF

    Implement a copy method in each of your objects, that
    1. Copies all simple datatypes and objects directly to an object of the same type
    2. Invokes your copy method on all non-simple object attributes
    Your copy method should have a consistent method signature of the form..
    public void myCopy(thisObject obj);This is not an overly complicated solution.

Maybe you are looking for