如何转换多个函数来创建泛型函数

时间:2017-11-12 06:32:19

标签: java generics

有没有办法将下面的两个方法转换为一个通用方法,我无法将参数传递给泛型函数初始化

public List<Obj1> csvObjConverter(Reader reader) {
    CsvParserSettings parserSettings = new CsvParserSettings();
    parserSettings.getFormat().setLineSeparator("\n");
    CsvParser parser = new CsvParser(parserSettings);
    parser.beginParsing(reader);
    String[] row;
    ArrayList<Obj1> objects = new ArrayList<>();
    while ((row = parser.parseNext()) != null) {
        Obj1 obj = new Obj1(row).preprocess();
        objects.add(obj);
    }
    parser.stopParsing();
    return objects;
}

public List<Obj2> csvObjConverter(Reader reader) {
    CsvParserSettings parserSettings = new CsvParserSettings();
    parserSettings.getFormat().setLineSeparator("\n");
    CsvParser parser = new CsvParser(parserSettings);
    parser.beginParsing(reader);
    String[] row;
    ArrayList<Obj2> objects = new ArrayList<>();
    while ((row = parser.parseNext()) != null) {
        Obj2 obj = new Obj2(row).preprocess();
        objects.add(obj);
    }
    parser.stopParsing();
    return objects;
}

我想做类似下面的代码,但它不起作用:

public <T extends AbstractObj> List<T> csvObjConverter(Reader r, Class<T> clazz) throws IllegalAccessException, InstantiationException {
    CsvParserSettings parserSettings = new CsvParserSettings();
    parserSettings.getFormat().setLineSeparator("\n");
    parserSettings.setHeaderExtractionEnabled(true);
    parserSettings.detectFormatAutomatically();
    parserSettings.setMaxCharsPerColumn(90000);
    CsvParser parser = new CsvParser(parserSettings);
    parser.beginParsing(r);
    String[] row;
    ArrayList<T> objects = new ArrayList<>();
    while ((row = parser.parseNext()) != null) {
        T obj = clazz.newInstance();    
 // i need something like -> T obj = clazz.newInstance(row);
        objects.add(obj);
    }
    parser.stopParsing();
    return objects;
}

2 个答案:

答案 0 :(得分:2)

您需要调用非参数构造函数,因此clazz.newInstance()将无效。

尝试:

public <T extends AbstractObj> List<T> csvObjConverter(Reader r, Class<T> clazz) throws IllegalAccessException, InstantiationException, InvocationTargetException, NoSuchMethodException {
    CsvParserSettings parserSettings = new CsvParserSettings();
    parserSettings.getFormat().setLineSeparator("\n");
    parserSettings.setHeaderExtractionEnabled(true);
    parserSettings.detectFormatAutomatically();
    parserSettings.setMaxCharsPerColumn(90000);
    CsvParser parser = new CsvParser(parserSettings);
    parser.beginParsing(r);
    String[] row;
    ArrayList<T> objects = new ArrayList<>();
    Constructor<T> cons = clazz.getConstructor (String[].class);
    while ((row = parser.parseNext()) != null) {
        T obj = cons.newInstance((Object) row);    
        objects.add(obj);
    }
    parser.stopParsing();
    return objects;
}

答案 1 :(得分:1)

你能尝试这个解决方案吗?

public <T> List<T> csvObjConverter(Reader reader, T object) {
    CsvParserSettings parserSettings = new CsvParserSettings();
    parserSettings.getFormat().setLineSeparator("\n");
    CsvParser parser = new CsvParser(parserSettings);
    parser.beginParsing(reader);
    String[] row;
    ArrayList<T> objects = new ArrayList<>();
    while ((row = parser.parseNext()) != null) {
      T obj = new T(row).preprocess();
      objects.add(obj);
    }
    parser.stopParsing();
    return objects;
  }

无论何时调用此函数,都需要传递期望的对象类型。