I'm using OpenCSV 3.8 to write Data into CSV files.
I have multiple class, Train, Tickets... So I don't want to write the same code for each class.
I have done this (I removed some lines that are not important):
public <T extends ExportDTO> void exportByTable(List<T> exportList) {
Writer csvWriter = new FileWriter(csvPath);
StatefulBeanToCsvBuilder<T> builder = new StatefulBeanToCsvBuilder<T>(csvWriter);
StatefulBeanToCsv<T> sbc = builder
.withSeparator(CSV_SEPARATOR)
.build();
sbc.write(exportList);
Every class has ExportDTO as a superclass, in ExportDTO there is nothing, but in Ticket or Train I have attributes with annotations (opencsv).
But this code write an empty CSV because it take ExportDTO class that has no attribute to write the CSV. (I call the method with a List<Train>
object)
Am I doing something wrong ? or is it impossible to do a generic method for this ?