lundi 2 mars 2020

How to call all getter methods of a class in a loop?

I have an object list and I want to create an excel file from the items in list but do not want to specify all columns one bye one. I want to take all properties of an object in a loop and put to excel.

for (CustomerDTO customerDto : customerDtoList) {
            Row row = sheet.createRow(rowNumber++);
            row.createCell(0).setCellValue(customerDto.getName());
            row.createCell(1).setCellValue(customerDto.getSurname());
            row.createCell(2).setCellValue(customerDto.getAddress());
            row.createCell(3).setCellValue(customerDto.isActive() ? "Enabled" : "Disabled");
        }

As you see in code I am only getting 4 columns but I want to get all properties but not hardcode all codes one bye one...

something like :

int index = 0
for (CustomerDTO customerDto : customerDtoList) {
index++;
row.createCell(index).setCellValue(customerDto.GETTERBLABLA);
}

I checked the "reflection", but could not get exact solution. How can I call all getters in a loop ?





Aucun commentaire:

Enregistrer un commentaire