title: How to Merge Java Objects Dynamically
body: <pre><code>public class MyClass{
     public String elem1;
     public int elem2;
     public MyType elem3;
  .................
  }
  
  MyClass object1=new MyClass();
  MyClass object2=new MyClass();
  object1.elem1=...
  object1.elem2=...
  ...
  object2.elem1=...
  object2.elem2=null
  .....
  </code></pre>
  
  <p>What I want is something like </p>
  
  <pre><code>object1.merge(object2);
  </code></pre>
  
  <p>where it will dynamically traverse all members on MyClass and run this on every member</p>
  
  <pre><code>if(object1.elem != object2.elem &amp;&amp; object2.elem!=null)
   object1.elem=object2.elem;
  </code></pre>
  
  <p>Is such a mechanism exist in Java?</p>