title: Object in Object to HashMap with reflection
body: <h3>Project:</h3>
  <p>I am currently using a tool called <a href="https://www.netuno.org/" rel="nofollow noreferrer">Netuno</a> to develop an api, so to do the export of objects to json it is necessary to do the transformation of the object to HashMap, for this a method was developed in a parent class to export the object.</p>
  The method:
  <pre class="lang-java prettyprint-override"><code>   public Map&lt;String, Object&gt; export() {
          Object obj = this;
          Map&lt;String, Object&gt; map = new HashMap&lt;&gt;();
          for (Field field : obj.getClass().getDeclaredFields()) {
              field.setAccessible(true);
              try {
                  map.put(field.getName(), field.get(obj));
              } catch (Exception e) {
                  //todo e
              }
          }
          return map;
      }
  </code></pre>
  <p>This works, but only for simple objects.</p>
  <h3>My problem:</h3>
  <p>If the object has complex objects inside my method it has no ability to export them to HashMap either.</p>
  <p>Example structure:</p>
  <pre class="lang-java prettyprint-override"><code>public abstract class Master {
   public Map &lt; String, Object &gt;
    export () {
     Object obj = this;
     Map &lt; String, Object &gt; map = new HashMap &lt; &gt; ();
     for (Field field: obj.getClass().getDeclaredFields()) {
      field.setAccessible(true);
      try {
       map.put(field.getName(), field.get(obj));
      } catch (Exception e) {
       //todo e
      }
     }
     return map;
    }
  }
  
  public class Foo extends Master {
   private int a;
   private int b;
   private String c;
   private Bar bar;
  //...
  }
  
  public class Bar extends Master {
   private int q;
   private int w;
   private String e;
  //...
  }
  
  </code></pre>
  <p>I use it this way:</p>
  <pre><code>return new Bar(/*data*/).export();
  </code></pre>
  <p>Output:</p>
  <pre><code>{
    &quot;a&quot;: 2,
    &quot;b&quot;: 5,
    &quot;c&quot;: &quot;abc&quot;,
    &quot;bar&quot;: &quot;myproject.myPackage.Bar@XXXXX&quot;
  }
  </code></pre>
  <p>Expected Output:</p>
  <pre><code>{
    &quot;a&quot;: 2,
    &quot;b&quot;: 5,
    &quot;c&quot;: &quot;abc&quot;,
    &quot;bar&quot;: {
      &quot;q&quot;: 10,
      &quot;w&quot;: 15,
      &quot;e&quot;: &quot;it works&quot;
    }
  }
  </code></pre>