I have the following complex POJO class
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 MeasureF shipped;
private List<LineItem> lineItems;
public Integer getLineNumber() { return lineNumber; }
public void setLineNumber(Integer lineNumber) { this.lineNumber = lineNumber; }
public MeasureF getShipped() { return shipped; }
public void setShipped(MeasureF shipped) { this.shipped = shipped; }
public List<LineItem> getLineItems() { return lineItems; }
public void setLineItems(List<LineItem> lineItems) { this.lineItems = lineItems; }
}
public class Base {
private String tenantUid;
private String sourceRecordId;
private String uid;
private String id;
public String getTenantUid() { return tenantUid; }
public void setTenantUid(String tenantUid) { this.tenantUid = tenantUid; }
public String getSourceRecordId() { return sourceRecordId; }
public void setSourceRecordId(String sourceRecordId) { this.sourceRecordId = sourceRecordId; }
public String getUid() { return uid; }
public void setUid(String uid) { this.uid = uid; }
public String getId() { return id; }
public void setId(String id) { this.id = id; }
}
public class Invoice extends Base {
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; }
}
I would like to use reflection to compare the values for all fields including child object and/or parent object and make update to one of the object. Something like this.
public Object updateIncomingObject(Object incomingObject, Object existingObject) {
try {
List<Field> incoming = incomingObject.getClass().getDeclaredFields();
for (Field incomingField : incoming) {
incomingField.setAccessible(true);
String incomingFieldName = incomingField.getName();
String typeName = incomingField.getType().getName();
if (incomingField.getType().IsSubType()) {
// get field instance and then recursive call to updateIncomingObject();
} else if (typeName.contains("java.util.List")) {
// Loop list and recursive call to updateIncomingObject()
} else {
Field existingField = getField(existingObject.getClass(), incomingFieldName);
existingField.setAccessible(true);
Object updatedTo = existingField.get(existingObject);
if (updatedTo != null) {
incomingField.set(incomingObject, updatedTo);
String updateMsg = incomingFieldName + " Updated to : " + updatedTo;
LOGGER.debug(updateMsg);
}
}
}
return incomingObject;
} catch (Exception e) {
throw new Exception("updateIncomingObject() - " + e.getMessage());
}
}
Can anyone tell me how to change the field object into a sub type object? Or filling the comment portion of the code? Or even better way to do this?
UPDATE: This is not a duplicate. I am looking for field to field update between to POJO objects with the same structure and the update is conditional. I didn't put the condition logic in updateIncomingObject() because I want to simplify things.
What I ma looking for is how to get to the fields of an field that is of non-primitive type, Such as Measure and LineItem as shown above.
Aucun commentaire:
Enregistrer un commentaire