Package com.ancientprogramming.fixedformat4j.exception

Examples of com.ancientprogramming.fixedformat4j.exception.FixedFormatException


      if (instructions.getFixedFormatBooleanData().getTrueValue().equals(string)) {
        result = true;
      } else if (instructions.getFixedFormatBooleanData().getFalseValue().equals(string)) {
        result = false;
      } else {
        throw new FixedFormatException("Could not convert string[" + string + "] to boolean value according to booleanData[" + instructions.getFixedFormatBooleanData() + "]");
      }
    }
    return result;
  }
View Full Code Here


    FixedFormatter formatter = getFixedFormatterInstance(formatterClass, context.getClass(), context);
    if (formatter == null) {
      formatter = getFixedFormatterInstance(formatterClass, null, null);
    }
    if (formatter == null) {
      throw new FixedFormatException("could not create instance of [" + formatterClass.getName() + "] because the class has no default constructor and no constructor with " + FormatContext.class.getName() + " as argument.");
    }
    return formatter;
  }
View Full Code Here

      try {
        result = formatterClass.getConstructor(paramType).newInstance(paramValue);
      } catch (NoSuchMethodException e) {
        result = null;
      } catch (Exception e) {
        throw new FixedFormatException("Could not create instance with one argument constructor", e);
      }
    } else {
      try {
        result = formatterClass.getConstructor().newInstance();
      } catch (NoSuchMethodException e) {
        result = null;
      } catch (Exception e) {
        throw new FixedFormatException("Could not create instance with no arg constructor", e);
      }
    }
    return result;
  }
View Full Code Here

    if (formatterClass != null) {
      try {
        return formatterClass.getConstructor().newInstance();
      } catch (NoSuchMethodException e) {
        throw new FixedFormatException("Could not create instance of[" + formatterClass.getName() + "] because no default constructor exists");
      } catch (Exception e) {
        throw new FixedFormatException("Could not create instance of[" + formatterClass.getName() + "]", e);
      }
    } else {
      throw new FixedFormatException(ByTypeFormatter.class.getName() + " cannot handle datatype[" + dataType.getName() + "]. Provide your own custom FixedFormatter for this datatype.");
    }
  }
View Full Code Here

    if (!StringUtils.isEmpty(string)) {
      try {
        result = getFormatter(instructions.getFixedFormatPatternData().getPattern()).parse(string);
      } catch (ParseException e) {
        throw new FixedFormatException("Could not parse value[" + string + "] by pattern[" + instructions.getFixedFormatPatternData().getPattern() + "] to " + Date.class.getName());
      }
    }
    return result;
  }
View Full Code Here

    T instance;
    try {
      Constructor<T> constructor = fixedFormatRecordClass.getConstructor();
      instance = constructor.newInstance();
    } catch (NoSuchMethodException e) {
      throw new FixedFormatException(format("%s is missing a default constructor which is nessesary to be loaded through %s", fixedFormatRecordClass.getName(), getClass().getName()));
    } catch (Exception e) {
      throw new FixedFormatException(format("unable to create instance of %s", fixedFormatRecordClass.getName()), e);
    }

    //look for setter annotations and read data from the 'data' string
    Method[] allMethods = fixedFormatRecordClass.getMethods();
    for (Method method : allMethods) {
      String methodName = stripMethodPrefix(method.getName());
      Field fieldAnnotation = method.getAnnotation(Field.class);
      Fields fieldsAnnotation = method.getAnnotation(Fields.class);
      if (fieldAnnotation != null) {
        Object loadedData = readDataAccordingFieldAnnotation(data, method, fieldAnnotation);
        foundData.put(methodName, loadedData);
      } else if (fieldsAnnotation != null) {
        //assert that the fields annotation contains minimum one field anno
        if (fieldsAnnotation.value() == null || fieldsAnnotation.value().length == 0) {
          throw new FixedFormatException(format("%s annotation must contain minimum one %s annotation", Fields.class.getName(), Field.class.getName()));
        }
        Object loadedData = readDataAccordingFieldAnnotation(data, method, fieldsAnnotation.value()[0]);
        foundData.put(methodName, loadedData);
      }
    }

    Set<String> keys = foundData.keySet();
    for (String key : keys) {
      String setterMethodName = "set" + key;

      Object foundDataObj = foundData.get(key);
      if (foundDataObj != null) {
        Class datatype = foundData.get(key).getClass();
        Method method;
        try {
          method = fixedFormatRecordClass.getMethod(setterMethodName, datatype);
        } catch (NoSuchMethodException e) {
          throw new FixedFormatException(format("setter method named %s.%s(%s) does not exist", fixedFormatRecordClass.getName(), setterMethodName, datatype));
        }
        try {
          method.invoke(instance, foundData.get(key));
        } catch (Exception e) {
          throw new FixedFormatException(format("could not invoke method %s.%s(%s)", fixedFormatRecordClass.getName(), setterMethodName, datatype), e);
        }
      }

    }
    return instance;
View Full Code Here


  private <T> Record getAndAssertRecordAnnotation(Class<T> fixedFormatRecordClass) {
    Record recordAnno = fixedFormatRecordClass.getAnnotation(Record.class);
    if (recordAnno == null) {
      throw new FixedFormatException(format("%s has to be marked with the record annotation to be loaded", fixedFormatRecordClass.getName()));
    }
    return recordAnno;
  }
View Full Code Here

  private Class getDatatype(Method method, Field fieldAnno) {
    Class datatype;
    if (isGetter(method)) {
      datatype = method.getReturnType();
    } else {
      throw new FixedFormatException(format("%s annotations must be placed on getter methods", fieldAnno.getClass().getName()));
    }
    return datatype;
  }
View Full Code Here

    FormatInstructions formatdata = getFormatInstructions(method, fieldAnno);
    Object valueObject;
    try {
      valueObject = method.invoke(fixedFormatRecord);
    } catch (Exception e) {
      throw new FixedFormatException(format("could not invoke method %s.%s(%s)", fixedFormatRecord.getClass().getName(), method.getName(), datatype), e);
    }
    result = formatter.format(valueObject, formatdata);
    if (LOG.isDebugEnabled()) {
      LOG.debug(format("exported %s ", result));
    }
View Full Code Here

    FixedFormatter formatter = getFixedFormatterInstance(formatterClass, context.getClass(), context);
    if (formatter == null) {
      formatter = getFixedFormatterInstance(formatterClass, null, null);
    }
    if (formatter == null) {
      throw new FixedFormatException("could not create instance of [" + formatterClass.getName() + "] because the class has no default constructor and no constructor with " + FormatContext.class.getName() + " as argument.");
    }
    return formatter;
  }
View Full Code Here

TOP

Related Classes of com.ancientprogramming.fixedformat4j.exception.FixedFormatException

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.