title: Java Reflection traverse into nested object and list and update the fields
body: <p>I have the following complex POJO class Invoice</p>
  
  <pre><code>public class Measure {
  
      private String id;
      private Float value;
  
      public String getId() { return id; }
      public void setId(String id) { this.id = id; }    
  
      public Float getValue() { return value; }
      public void setValue(Float value) { this.value = value; }
  
  }
  
  public class LineItem {
  
      private Integer lineNumber;      
      private Measure shipped;       
      private List&lt;LineItem&gt; lineItems;  
  
  
      public Integer getLineNumber() { return lineNumber; }
      public void setLineNumber(Integer lineNumber) { this.lineNumber = lineNumber; }
  
      public Measure getShipped() { return shipped; }
      public void setShipped(Measure shipped) { this.shipped = shipped; }
  
      public List&lt;LineItem&gt; getLineItems() { return lineItems; }
      public void setLineItems(List&lt;LineItem&gt; lineItems) { this.lineItems = lineItems; }
  
  }
  
  public class Invoice {
  
  
      private String originUid;
      private String vehicleUid;
      private List&lt;LineItem&gt; lineItems;
  
      public String getOriginUid() { return originUid; }
      public void setOriginUid(String originUid) { this.originUid = originUid; }
  
      public String getVehicleUid() { return vehicleUid; }
      public void setVehicleUid(String vehicleUid) { this.vehicleUid = vehicleUid; }
  
      public List&lt;LineItem&gt; getLineItems() { return lineItems; }
      public void setLineItems(List&lt;LineItem&gt; lineItems) { this.lineItems = lineItems; }
  
  
  }
  </code></pre>
  
  <p>Now I want to traverse deep into every single field including nested objects in the Invoice object and update them using Reflection.</p>
  
  <p>I can call updateIncomingObject() recursively. But I need to know how to get the nested objects out of the field as shown in the commented section.</p>
  
  <pre><code>public Object updateIncomingObject(Object incomingObject) {
  
      Field[] incoming =  incomingObject.getClass().getDeclaredFields(); 
      for (Field incomingField : incoming) {
          incomingField.setAccessible(true);
          if (incomingField.getType().isArray()) {
           // for (each occurrence in thisArray ???) {
           //     Object result = updateIncomingObject(occurrence);
           //     thisArray.set(index,result);
           // }
           // incomingField.set(incomingObject, thisArray);
          } 
          else if (!incomingField.getType().getName().startsWith("java.lang")) {
           // Object objInstance = incomingField.???; 
           // Object result = updateIncomingObject(objInstance);
           // incomingField.set(incomingObject, result);
          }
          else {
              if (incomingField.getType().equals(String.class) &amp;&amp; incomingField.get(incomingObject) != null) {
                  String trimmed = incomingField.get(incomingObject).toString().trim();
                  incomingField.set(incomingObject, trimmed);
              }
          }
      }
      return incomingObject;
  }
  </code></pre>
  
  <p>How do I turn field into object instance?</p>