mercredi 22 août 2018

How do get Nested Fields through reflection rather than object as a field

This is my old class structure .

 class User {
 private int userId;
 private String userName;
 private String userEmail;
 private String firstName;
 private String lastName;
 //.. getters and setters 
 }

And i was generating excel reports from this class exactly having same column as of property .

 Class<Object> headers = (Class<Object>) excelDataMapper.getClass();
 Field[] objectFields = headers.getDeclaredFields();
 for (int j = 0; j < objectFields.length; j++) {
  objectFields[j].setAccessible(true);
  Object value = objectFields[j].get(excelDataMapper);
 }

Now due to some requirement , structure of User class has changed to this

class User {
 private int userId;
 private String userName;
 private String userEmail;
 private UserBasicInfo basicInfo;
 //.. getters and setters 
 }

And Structure of UserBasicInfo

   class UserBasicInfo {
   private String firstName;
   private String lastName;
   private Date lastLoginDate;
   private int invalidLoginAttempt;
   // ..getters and setters ; 
 }

With nested object UserBasicInfo now i have different field in excell sheets UserBasicInfo which is not required , i want to continue with reports as previous structure only , i.e firstName , lastName which is now inside UserBasicInfo . For this i want to have something like this

        List<String> colHeaders= new ArrayList<String>();
        Class<LoginsReport> headers = LoginsReport.class;
        Field[] objectFields = headers.getDeclaredFields();
        for (int j = 0; j < objectFields.length; j++) {
            String fieldName = objectFields[j].getName();
            if(fieldName.indexOf("encrypted") == -1 && fieldName.indexOf("Mongo") == -1 &&  !(ArrayUtils.contains( excluededPropertyInLoginReports, fieldName ))  )
                colHeaders.add(fieldName);
        }
        colHeaders.add("userBasicProfile.firstName");
        colHeaders.add("userBasicProfile.lastName");





Aucun commentaire:

Enregistrer un commentaire