Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Serialization is basically deep cloning. </p> <p>You need to track each object reference for reoccurrencies (for example by using <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/util/IdentityHashMap.html" rel="noreferrer">IdentityHashMap</a>). What ever is your final implementation method (if not external library) remember to check for object reoccurrencies or you may end up in an infinent loop (when object A has reference to object B that again has reference to object A or something more complex loop in an object graph). </p> <p>One way is to traverse through object graph with DFS-like algorithm and build the clone (serialized string) from there. </p> <p>This pseudo-code hopefully explains how:</p> <pre><code>visited = {} function visit(node) { if node in visited { doStuffOnReoccurence(node) return } visited.add(node) doStuffBeforeOthers(node) for each otherNode in node.expand() visit(otherNode) doStuffAfterOthers(node) } </code></pre> <p>The visited set in the example is where I would use identity set (if there was one) or IdentityHashMap.</p> <p>When finding out fields reflectively (thats the node.expand() part) remember to go through superclass fields also.</p> <p>Reflection should not be used in a "normal" development case. Reflection handles code as a data and you can ignore all normal object access restrictions. I've used this reflective deep copy stuff only for tests:</p> <ol> <li><p>In a test that checked different kinds of objects for deep object graph equality</p></li> <li><p>In a test that analyzed object graph size and other properties</p></li> </ol>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload