Exemple #1
0
  /**
   * Converts the given value parameter (parameter) to a type that is accepted by the set method of
   * this property. This method attempts to convert the value regardless of the value being null.
   * However, this method short circuits and returns the value unchanged if value is runtime
   * assignable to the type of this BaseBeanProperty.
   *
   * @param values The String values to convert.
   * @param context The current context.
   * @param field The field that the conversion is occurring for. This is used to look for
   *     conversion annotations.
   * @return The value parameter converted to the correct type.
   * @throws ConversionException If there was a problem converting the parameter.
   */
  protected Object convert(final String[] values, Context context, Field field)
      throws ConversionException {
    Object newValue = values;

    // First look for annotations
    if (field != null) {
      Annotation[] annotations = field.getAnnotations();
      for (Annotation annotation : annotations) {
        ConverterAnnotation converterAnnotation =
            annotation.annotationType().getAnnotation(ConverterAnnotation.class);
        if (converterAnnotation != null) {
          AnnotationConverter converter = converterProvider.lookup(annotation);
          return converter.convertFromStrings(
              annotation, values, type, context.getAttributes(), context.getExpression());
        }
      }
    }

    // The converter does this, but pre-emptively checking these conditions will speed up conversion
    // times
    Class<?> typeClass = TypeTools.rawType(type);
    if (!typeClass.isInstance(values)) {
      GlobalConverter converter = converterProvider.lookup(typeClass);
      if (converter == null) {
        throw new ConverterStateException(
            "No type converter found for the type [" + typeClass.getName() + "]");
      }

      newValue =
          converter.convertFromStrings(
              values, type, context.getAttributes(), context.getExpression());
    }

    return newValue;
  }
Exemple #2
0
  /**
   * Creates a new instance of the current type.
   *
   * @param key This is only used when creating arrays. It is the next atom, which is always the
   *     size of the array.
   * @return The new value.
   */
  protected Object createValue(Object key) {
    Class<?> typeClass = TypeTools.rawType(type);
    Object value;
    if (Map.class == typeClass) {
      value = new HashMap();
    } else if (List.class == typeClass) {
      value = new ArrayList();
    } else if (Set.class == typeClass) {
      value = new HashSet();
    } else if (Queue.class == typeClass) {
      value = new LinkedList();
    } else if (Deque.class == typeClass) {
      value = new ArrayDeque();
    } else if (SortedSet.class == typeClass) {
      value = new TreeSet();
    } else if (typeClass.isArray()) {
      if (key == null) {
        throw new ExpressionException(
            "Attempting to create an array, but there isn't an index "
                + "available to determine the size of the array");
      }

      value = Array.newInstance(typeClass.getComponentType(), Integer.parseInt(key.toString()) + 1);
    } else {
      value = instantiate(typeClass);
    }

    return value;
  }