title: Deep reflective compare equals
body: <p>I am trying to validate serialize and de-serialize routines by comparing the resulting object  with the original object. The routines can serialize arbitrary and deeply nested classes and consequently I want a comparison routine which can be given the original and final instance and reflectively walk through each value type and compare the values and iteratively dive into reference types to compare values.</p>
  
  <p>I have tried the Apache Commons Lang <a href="http://commons.apache.org/proper/commons-lang/apidocs/org/apache/commons/lang3/builder/EqualsBuilder.html#reflectionEquals%28java.lang.Object,%20java.lang.Object,%20java.lang.String...%29"><code>EqualsBuilder.reflectionEquals(inst1, inst2)</code></a> but this does not appear to do a very deep comparison, it simply compares reference types for equality rather than diving deeper into them:</p>
  
  <p>The following code illustrates my issue. The first call to <code>reflectionEquals</code> returns true but the second returns false. </p>
  
  <p>Is there a library routine anyone could recommend?</p>
  
  <pre><code>class dummy {
      dummy2 nestedClass;
  }
  
  class dummy2 {
      int intVal;
  }
  
  @Test
  public void testRefEqu() {
  
      dummy inst1 = new dummy();
      inst1.nestedClass = new dummy2();
      inst1.nestedClass.intVal = 2;
      dummy inst2 = new dummy();
      inst2.nestedClass = new dummy2();
      inst2.nestedClass.intVal = 2;
      boolean isEqual = EqualsBuilder.reflectionEquals(inst1.nestedClass, inst2.nestedClass);
      isEqual = EqualsBuilder.reflectionEquals(inst1, inst2);
  }
  </code></pre>