private void processProperty(T bean, String[] line, int col) throws InstantiationException, IllegalAccessException, InvocationTargetException, CsvBadConverterException { PropertyDescriptor prop = findDescriptor(col); if (null != prop) { String value = checkForTrim(line[col], prop); Object obj = convertValue(value, prop); prop.getWriteMethod().invoke(bean, obj); } }
@Override public T populateNewBean(String[] line) throws InstantiationException, IllegalAccessException, IntrospectionException, InvocationTargetException, CsvRequiredFieldEmptyException, CsvDataTypeMismatchException, CsvConstraintViolationException { verifyLineLength(line.length); T bean = createBean(); for (int col = 0; col < line.length; col++) { if (isAnnotationDriven()) { setFieldValue(bean, line[col], col); } else { processProperty(bean, line, col); } } return bean; }
@Override @Deprecated public PropertyDescriptor findDescriptor(int col) { BeanField beanField = findField(col); if(beanField != null) { return findDescriptor(beanField.getField().getName()); } String columnName = getColumnName(col); if(StringUtils.isNotBlank(columnName)) { return findDescriptor(columnName); } return null; }
@Override @Deprecated public T populateNewBeanWithIntrospection(String[] line) throws InstantiationException, IllegalAccessException, IntrospectionException, InvocationTargetException, CsvRequiredFieldEmptyException { verifyLineLength(line.length); T bean = createBean(); for (int col = 0; col < line.length; col++) { processProperty(bean, line, col); } return bean; }
@Override public String[] transmuteBean(T bean) throws CsvDataTypeMismatchException, CsvRequiredFieldEmptyException { List<String> transmutedBean; int numColumns = findMaxFieldIndex()+1; if(isAnnotationDriven()) { transmutedBean = writeWithReflection(bean, numColumns); } else { transmutedBean = writeWithIntrospection(bean, numColumns); } return transmutedBean.toArray(new String[transmutedBean.size()]); }
/** * Populates the field corresponding to the column position indicated of the * bean passed in according to the rules of the mapping strategy. * This method performs conversion on the input string and assigns the * result to the proper field in the provided bean. * * @param bean Object containing the field to be set. * @param value String containing the value to set the field to. * @param column The column position from the CSV file under which this * value was found. * @throws CsvDataTypeMismatchException When the result of data conversion returns * an object that cannot be assigned to the selected field * @throws CsvRequiredFieldEmptyException When a field is mandatory, but there is no * input datum in the CSV file * @throws CsvConstraintViolationException When the internal structure of * data would be violated by the data in the CSV file * @since 4.2 */ protected void setFieldValue(T bean, String value, int column) throws CsvDataTypeMismatchException, CsvRequiredFieldEmptyException, CsvConstraintViolationException { BeanField<T> beanField = findField(column); if (beanField != null) { beanField.setFieldValue(bean, value, findHeader(column)); } }
firstBeanField = findField(i); firstIndex = chooseMultivaluedFieldIndexFromHeaderIndex(i); String[] fields = firstBeanField != null ? firstBeanField.write(bean, firstIndex) subsequentBeanField = findField(displacedIndex); subsequentIndex = chooseMultivaluedFieldIndexFromHeaderIndex(displacedIndex); while(j < fields.length && displacedIndex < numColumns subsequentBeanField = findField(displacedIndex); subsequentIndex = chooseMultivaluedFieldIndexFromHeaderIndex(displacedIndex); subsequentBeanField = findField(i); subsequentIndex = chooseMultivaluedFieldIndexFromHeaderIndex(i); while(Objects.equals(firstBeanField, subsequentBeanField) && Objects.equals(firstIndex, subsequentIndex) && i < numColumns) { contents.add(StringUtils.EMPTY); subsequentBeanField = findField(++i); subsequentIndex = chooseMultivaluedFieldIndexFromHeaderIndex(i);
/** * This method returns an empty array. * The column position mapping strategy assumes that there is no header, and * thus it also does not write one, accordingly. * * @return An empty array */ // The rest of the Javadoc is inherited @Override public String[] generateHeader(T bean) throws CsvRequiredFieldEmptyException { String[] h = super.generateHeader(bean); columnIndexForWriting = new Integer[h.length]; // Once we support Java 8, this might be nicer with Arrays.parallelSetAll(). for (int i = 0; i < columnIndexForWriting.length; i++) { columnIndexForWriting[i] = i; } // Create the mapping for input column index to output column index. Arrays.sort(columnIndexForWriting, writeOrder); return ArrayUtils.EMPTY_STRING_ARRAY; }
@Override public Integer getColumnIndex(String name) { if (headerIndex.isEmpty()) { throw new IllegalStateException(ResourceBundle.getBundle(ICSVParser.DEFAULT_BUNDLE_NAME, errorLocale).getString("header.unread")); } return super.getColumnIndex(name); }
private List<String> writeWithIntrospection(T bean, int numColumns) { PropertyDescriptor desc; List<String> contents = new ArrayList<>(numColumns > 0 ? numColumns : 0); for(int i = 0; i < numColumns; i++) { try { desc = findDescriptor(i); Object o = desc != null ? desc.getReadMethod().invoke(bean, (Object[]) null) : null; contents.add(Objects.toString(o, "")); } catch(IllegalAccessException | InvocationTargetException e) { CsvBeanIntrospectionException csve = new CsvBeanIntrospectionException( bean, null, ResourceBundle.getBundle(ICSVParser.DEFAULT_BUNDLE_NAME, errorLocale).getString("error.introspecting.beans")); csve.initCause(e); throw csve; } } return contents; }