VJ.
VJ.

Reputation: 374

java generics - specifying the generic type

Ok here is what I want to do.. opencsv has following constructor..

CsvToBean<T> csv2Bean=new CsvToBean<T>();

In it's current form, it will take any object. so following works fine for me.

CsvToBean<HoldbackModel> holdbackModelCsv=new CsvToBean<HoldbackModel>();

I would like to write a generic helper which will wrap the opencsv calls. I want to restrict it to any objects of classes which extend my marker class CsvRecord.

public static <T extends CsvRecord> List<T> readCsvRecords(InputStream srcRecords,     InputStream templateCsv, Class<? extends T> clazz) {
    // here i want to pass the class clazz to the CsvBean.. but don't know how!!
    CsvToBean<T> csv2Bean=new CsvToBean<T>();
}

Any help?

Upvotes: 0

Views: 408

Answers (1)

Genzer
Genzer

Reputation: 2991

You are going the right way, just need a little bit adjustment.

public static <T extends CsvRecord> List<T> readCsvRecords(InputStream srcRecords, InputStream templateCsv) {
    CsvToBean<T> csv2Bean = new CsvToBean<T>();
}

Upvotes: 1

Related Questions