@Test(expected = RuntimeException.class) public void testToCollection() { CsvEngine csvEngine = new CsvEngine(); csvEngine.fromString(expected, ArrayList.class); }
@Override @SuppressWarnings("unchecked") public <T> T fromString(String content, Class<T> classOfT) { if (!classOfT.isArray()) { if (Collection.class.isAssignableFrom(classOfT)) { throw new RuntimeException("Collection types are not supported. Please specify an array[] type."); } throw new RuntimeException(String.format("Array[] types are required. Please specify %s[]", classOfT.getName())); } Class<?> objectType = classOfT.getComponentType(); int currentLine = 0; try (CSVParser parser = new CSVParser(new StringReader(content), getCSVFormat().withHeader())) { Set<String> columns = parser.getHeaderMap().keySet(); Map<String, Field> fieldMap = getFieldMap(objectType); Constructor<?> objectConstructor; try { objectConstructor = objectType.getConstructor(); } catch (NoSuchMethodException e) { throw new RuntimeException("A default constructor is required for " + objectType.getName()); } List objects = new ArrayList<>(); for (CSVRecord record : parser) { currentLine++; Object o = objectConstructor.newInstance(); for (String column : columns) { Field field = fieldMap.get(caseSensitiveFieldNames ? column : column.toLowerCase()); String value = record.get(column); Object object = objectFromString(value, field.getType()); field.set(o, object); } objects.add(o); } Object array = Array.newInstance(objectType, objects.size()); for (int i = 0; i < objects.size(); i++) { Array.set(array, i, objects.get(i)); } return (T) array; } catch (Exception e) { throw new RuntimeException("Failed to parse CSV near line #" + currentLine, e); } }
CsvEngine implements ContentTypeEngine { @Override @SuppressWarnings("unchecked") public <T> T fromString(String content, Class<T> classOfT) { if (!classOfT.isArray()) { if (Collection.class.isAssignableFrom(classOfT)) { throw new RuntimeException("Collection types are not supported. Please specify an array[] type."); } throw new RuntimeException(String.format("Array[] types are required. Please specify %s[]", classOfT.getName())); } Class<?> objectType = classOfT.getComponentType(); int currentLine = 0; try (CSVParser parser = new CSVParser(new StringReader(content), getCSVFormat().withHeader())) { Set<String> columns = parser.getHeaderMap().keySet(); Map<String, Field> fieldMap = getFieldMap(objectType); Constructor<?> objectConstructor; try { objectConstructor = objectType.getConstructor(); } catch (NoSuchMethodException e) { throw new RuntimeException("A default constructor is required for " + objectType.getName()); } List objects = new ArrayList<>(); for (CSVRecord record : parser) { currentLine++; Object o = objectConstructor.newInstance(); for (String column : columns) { Field field = fieldMap.get(caseSensitiveFieldNames ? column : column.toLowerCase()); String value = record.get(column); Object object = objectFromString(value, field.getType()); field.set(o, object); } objects.add(o); } Object array = Array.newInstance(objectType, objects.size()); for (int i = 0; i < objects.size(); i++) { Array.set(array, i, objects.get(i)); } return (T) array; } catch (Exception e) { throw new RuntimeException("Failed to parse CSV near line #" + currentLine, e); } } }
CsvEngine implements ContentTypeEngine { @Override @SuppressWarnings("unchecked") public <T> T fromString(String content, Class<T> classOfT) { if (!classOfT.isArray()) { if (Collection.class.isAssignableFrom(classOfT)) { throw new RuntimeException("Collection types are not supported. Please specify an array[] type."); } throw new RuntimeException(String.format("Array[] types are required. Please specify %s[]", classOfT.getName())); } Class<?> objectType = classOfT.getComponentType(); int currentLine = 0; try (CSVParser parser = new CSVParser(new StringReader(content), getCSVFormat().withHeader())) { Set<String> columns = parser.getHeaderMap().keySet(); Map<String, Field> fieldMap = getFieldMap(objectType); Constructor<?> objectConstructor; try { objectConstructor = objectType.getConstructor(); } catch (NoSuchMethodException e) { throw new RuntimeException("A default constructor is required for " + objectType.getName()); } List objects = new ArrayList<>(); for (CSVRecord record : parser) { currentLine++; Object o = objectConstructor.newInstance(); for (String column : columns) { Field field = fieldMap.get(caseSensitiveFieldNames ? column : column.toLowerCase()); String value = record.get(column); Object object = objectFromString(value, field.getType()); field.set(o, object); } objects.add(o); } Object array = Array.newInstance(objectType, objects.size()); for (int i = 0; i < objects.size(); i++) { Array.set(array, i, objects.get(i)); } return (T) array; } catch (Exception e) { throw new RuntimeException("Failed to parse CSV near line #" + currentLine, e); } }  }
CsvEngine implements ContentTypeEngine { @Override @SuppressWarnings("unchecked") public <T> T fromString(String content, Class<T> classOfT) { if (!classOfT.isArray()) { if (Collection.class.isAssignableFrom(classOfT)) { throw new RuntimeException("Collection types are not supported. Please specify an array[] type."); } throw new RuntimeException(String.format("Array[] types are required. Please specify %s[]", classOfT.getName())); } Class<?> objectType = classOfT.getComponentType(); int currentLine = 0; try (CSVParser parser = new CSVParser(new StringReader(content), getCSVFormat().withHeader())) { Set<String> columns = parser.getHeaderMap().keySet(); Map<String, Field> fieldMap = getFieldMap(objectType); Constructor<?> objectConstructor; try { objectConstructor = objectType.getConstructor(); } catch (NoSuchMethodException e) { throw new RuntimeException("A default constructor is required for " + objectType.getName()); } List objects = new ArrayList<>(); for (CSVRecord record : parser) { currentLine++; Object o = objectConstructor.newInstance(); for (String column : columns) { Field field = fieldMap.get(caseSensitiveFieldNames ? column : column.toLowerCase()); String value = record.get(column); Object object = objectFromString(value, field.getType()); field.set(o, object); } objects.add(o); } Object array = Array.newInstance(objectType, objects.size()); for (int i = 0; i < objects.size(); i++) { Array.set(array, i, objects.get(i)); } return (T) array; } catch (Exception e) { throw new RuntimeException("Failed to parse CSV near line #" + currentLine, e); } }  void setCaseSensitiveFieldNames(boolean caseSensitiveFieldNames); void setDatePattern(String datePattern); void setDelimiter(char delimiter); void setEscapeCharacter(char escapeCharacter); void setQuoteCharacter(char quoteCharacter); void setQuoteMode(QuoteMode quoteMode); void setNullString(String nullString); void setRecordSeparator(String recordSeparator); CSVFormat getCSVFormat(); @Override void init(Application application); @Override String getContentType(); @Override String toString(Object object); String toCsv(Csv... records); @Override @SuppressWarnings("unchecked") T fromString(String content, Class<T> classOfT); String objectToString(Object object); Object objectFromString(String value, Class<?> objectClass); }
CsvEngine implements ContentTypeEngine { @Override @SuppressWarnings("unchecked") public <T> T fromString(String content, Class<T> classOfT) { if (!classOfT.isArray()) { if (Collection.class.isAssignableFrom(classOfT)) { throw new RuntimeException("Collection types are not supported. Please specify an array[] type."); } throw new RuntimeException(String.format("Array[] types are required. Please specify %s[]", classOfT.getName())); } Class<?> objectType = classOfT.getComponentType(); int currentLine = 0; try (CSVParser parser = new CSVParser(new StringReader(content), getCSVFormat().withHeader())) { Set<String> columns = parser.getHeaderMap().keySet(); Map<String, Field> fieldMap = getFieldMap(objectType); Constructor<?> objectConstructor; try { objectConstructor = objectType.getConstructor(); } catch (NoSuchMethodException e) { throw new RuntimeException("A default constructor is required for " + objectType.getName()); } List objects = new ArrayList<>(); for (CSVRecord record : parser) { currentLine++; Object o = objectConstructor.newInstance(); for (String column : columns) { Field field = fieldMap.get(caseSensitiveFieldNames ? column : column.toLowerCase()); String value = record.get(column); Object object = objectFromString(value, field.getType()); field.set(o, object); } objects.add(o); } Object array = Array.newInstance(objectType, objects.size()); for (int i = 0; i < objects.size(); i++) { Array.set(array, i, objects.get(i)); } return (T) array; } catch (Exception e) { throw new RuntimeException("Failed to parse CSV near line #" + currentLine, e); } }  void setCaseSensitiveFieldNames(boolean caseSensitiveFieldNames); void setDatePattern(String datePattern); void setDelimiter(char delimiter); void setEscapeCharacter(char escapeCharacter); void setQuoteCharacter(char quoteCharacter); void setQuoteMode(QuoteMode quoteMode); void setNullString(String nullString); void setRecordSeparator(String recordSeparator); CSVFormat getCSVFormat(); @Override void init(Application application); @Override String getContentType(); @Override String toString(Object object); String toCsv(Csv... records); @Override @SuppressWarnings("unchecked") T fromString(String content, Class<T> classOfT); String objectToString(Object object); Object objectFromString(String value, Class<?> objectClass); final static String TEXT_CSV; }