mercredi 12 août 2020

Iterate over object fields which can be another object

Is there a nice way to iterate over object fields using reflection? The main problem is in object can be another object therefore it's needed to iterate over another object's properties too.

For example I have AllInclusiveDetails object

public class AllInclusiveDetails {
   
   @JsonProperty("renters_business")
   private String rentersBusiness;

   @Valid
   @NotNull
   @JsonProperty("main_property_owner_details")
   private ShortCustomer mainPropertyOwnerDetails;

   @Valid
   @NotNull
   @JsonProperty("main_tenant_details")
   private ShortCustomer mainTenantDetails;
}

And ShortCustomer is

public class ShortCustomer {

@NotNull
@Positive
private Long id;

@NotEmpty
@JsonProperty("full_name")
private String fullName;

@JsonProperty("organization_number")
private String organizationNumber;

@PastOrPresent
private LocalDate birthdate;

}

I want to iterate over AllInclusiveDetails object fields using reflection and if there is another object in it , I want to iterate over that object fields too.

The main purpose is to track if value of the same field in two different object are equal or not and if not to save old value and the new one.





Aucun commentaire:

Enregistrer un commentaire