package io.ljunggren.csv.parser.mapper;

import java.lang.reflect.Field;

import org.apache.commons.lang3.ClassUtils;

/**
 * Fallback mapper that converts a string to the field's declared type by
 * invoking a single-argument {@code String} constructor.
 *
 * <p>Primitive-typed fields are converted via the corresponding wrapper class,
 * for example {@code int} -&gt; {@link Integer}.</p>
 */
public class CatchAllMapper extends MapperChain {

    @Override
    public Object map(Field field, String value) throws Exception {
        Class<?> type = field.getType();
        if (type.isPrimitive()) {
            type = ClassUtils.primitiveToWrapper(type);
        }
        return type.getConstructor(String.class).newInstance(value);
    }

}
