lundi 24 avril 2017

JAVA get all values from Object (recursively)

i created a simple object named UserType.

public class UserType {

  protected Integer id;
  protected Integer accountID;
  protected String prename;
  protected String lastname;
  protected String title;
  protected Gender gender; // Just an Enum (MALE, FEMALE)
  protected String birthday;
  protected String nationality;
  protected List<AddressType> address;
  protected List<BankType> banks; 
}

And I created the AddressType

public class AddressType {

   protected String country;
   protected String zipcode;
   protected String street;
   protected String city;
   protected String housenumber;
   protected String housenumberAddition;
}

And the BankType

public class BankType {

   protected String iban;
   protected String bic;
   protected String bankname;
}

And now i need a function that will show all values recusivly like this

3,
5,
Paul,
Smith,
Dr.,
....
....
[
   {
      DE,
      21225,
      Hogwarts-Street,
      ....
   }
   {
      DE,
      44444,
      Lambda-Street,
      ....
   }
]
... and for BankType ....

I tried to use some Reflectionmethods but it doesnt work for me. I got many problems with this List class ...

I tried this but i cant find a flag to go to the next level....

public void showValues(Object first) {
    int differentValuesCounter = 0;
    Class<?> clazz = first.getClass();
    Field[] fields = clazz.getDeclaredFields();
    for (Field field : fields) {
        field.setAccessible(true);

        try {
            Class<?> subClazz = field.getType();
            logger.info(field.getName());
            logger.info(field.get(first).toString());
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }
}





Aucun commentaire:

Enregistrer un commentaire