vendredi 31 août 2018

Java Reflection traverse into nested object and list and update the fields

I have the following complex POJO class Invoice

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<LineItem> 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<LineItem> getLineItems() { return lineItems; }
    public void setLineItems(List<LineItem> lineItems) { this.lineItems = lineItems; }

}

public class Invoice {


    private String originUid;
    private String vehicleUid;
    private List<LineItem> 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<LineItem> getLineItems() { return lineItems; }
    public void setLineItems(List<LineItem> lineItems) { this.lineItems = lineItems; }


}

Now I want to traverse deep into every single field including nested objects in the Invoice object and update them using Reflection.

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.

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) && incomingField.get(incomingObject) != null) {
                String trimmed = incomingField.get(incomingObject).toString().trim();
                incomingField.set(incomingObject, trimmed);
            }
        }
    }
    return incomingObject;
}

How do I turn field into object instance?





Aucun commentaire:

Enregistrer un commentaire