I am using univocity to parse some files to javabeans. These beans are compiled classes. However I wish to generate these classes during runtime and then parse the files to the at runtime generated classes.
Full code is here: gist
A snippet of the code that uses the Univocity library:
private static void parseBean(final Class<?> dynamicClass) throws FileNotFoundException {
@SuppressWarnings("unchecked")
final BeanListProcessor<?> rowProcessor = new BeanListProcessor<Class<?>>((Class<Class<?>>) dynamicClass);
final CsvParserSettings parserSettings = new CsvParserSettings();
parserSettings.setProcessor(rowProcessor);
parserSettings.setHeaderExtractionEnabled(false);
parserSettings.getFormat().setDelimiter('|');
parserSettings.setEmptyValue("");
parserSettings.setNullValue("");
final CsvParser parser = new CsvParser(parserSettings);
parser.parse(new FileReader("src/main/resources/person.csv"));
final List<?> beans = rowProcessor.getBeans();
for (final Object domain : beans) {
final Domain domainImpl = (Domain) domain;
System.out.println("Person id is: " + domainImpl.getIdentifier());
System.out.println("Person name is: " + domainImpl.getColumnByIndex(1));
System.out.println();
}
}
The file looks like this:
0|Eric
1|Maria
All the values seems to be null, so something is going wrong when parsing the file and mapping it to the bean...
Person id is: null
Person name is: null
Is it possible to parse files to runtime generated beans/classes using the Univocity library?