Search code examples
javabeansopencsv

how to parse non-string values in Opencsv HeaderColumnNameMappingStrategy


I'm using a HeaderColumnNameMappingStrategy to map a csv file with a header into a JavaBean. String values parse fine but any "true" or "false" value in csv doesn't map to JavaBean and I get the following exception from the PropertyDescriptor:

java.lang.IllegalArgumentException: argument type mismatch

The code where it occurs is in CsvToBean, line 64:

protected T processLine(MappingStrategy<T> mapper, String[] line) throws  
 IllegalAccessException, InvocationTargetException, InstantiationException, IntrospectionException {
    T bean = mapper.createBean();
    for(int col = 0; col < line.length; col++) {
        String value = line[col];
        PropertyDescriptor prop = mapper.findDescriptor(col);
        if (null != prop) {
            Object obj = convertValue(value, prop);
            // this is where exception is thrown for a "true" value in csv
            prop.getWriteMethod().invoke(bean, new Object[] {obj});
        }
    }
    return bean;
}

 protected PropertyEditor getPropertyEditor(PropertyDescriptor desc) throws   
       InstantiationException, IllegalAccessException {
    Class<?> cls = desc.getPropertyEditorClass();
    if (null != cls) return (PropertyEditor) cls.newInstance();
    return getPropertyEditorValue(desc.getPropertyType());
}

I can confirm (via debugger) that the setter method id correctly retrieved at this point.

The problem occurs in desc.getPropertyEditorClass() since it returns null. I assumed primitive types and its wrappers are supported. Are they not?


Solution

  • I resolved this by extending CsvToBean and adding my own PropertyEditors. Turns out opencsv just supports primitive types and no wrappers.