コード例 #1
0
  /** {@inheritDoc} */
  public Object mapRecord(Record record) throws RecordMappingException {

    List<Field> fields = record.getFields();

    Object instance;
    try {
      // create a new instance of the returned object
      instance = recordClass.newInstance();

      // for each field
      for (Field field : fields) {

        // get field content and index
        String content = field.getContent();
        int index = field.getIndex();

        // convert the String raw value to field type
        Object typedValue = null;
        Class<?> type = recordClassSetters[index].getParameterTypes()[0];
        TypeConverter typeConverter = typeConverters.get(type);
        if (typeConverter != null) {
          typedValue = typeConverter.convert(content);
        }

        // set the typed value to the object field
        recordClassSetters[index].invoke(instance, typedValue);

        /*
         * Note : using java.lang.reflect.Field.set(instance, typedValue) throws an IllegalAccessException :
         * Class io.github.benas.cb4j.core.impl.DefaultRecordMapperImpl can not access a member of class X with modifiers "private"
         * even if a setter is provided
         */
      }
    } catch (InstantiationException e) {
      throw new RecordMappingException(
          "An exception occurred during record mapping : could not create a new instance.", e);
    } catch (IllegalAccessException e) {
      throw new RecordMappingException(
          "An exception occurred during record mapping : could not access class/field.", e);
    } catch (InvocationTargetException e) {
      throw new RecordMappingException(
          "An exception occurred during record mapping : could not invoke setter.", e);
    }

    return instance;
  }
コード例 #2
0
 /** {@inheritDoc} */
 public boolean isValid(Field field) {
   return super.isValid(field) && field.getContent().length() >= minLength;
 }