Package org.springframework.core.convert.converter

Examples of org.springframework.core.convert.converter.GenericConverter


    @SuppressWarnings("unchecked")
    public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
      AnnotationConverterKey converterKey =
          new AnnotationConverterKey(sourceType.getAnnotation(annotationType), sourceType.getObjectType());
      GenericConverter converter = cachedPrinters.get(converterKey);
      if (converter == null) {
        Printer<?> printer = annotationFormatterFactory.getPrinter(
            converterKey.getAnnotation(), converterKey.getFieldType());
        converter = new PrinterConverter(fieldType, printer, FormattingConversionService.this);
        cachedPrinters.put(converterKey, converter);
      }
      return converter.convert(source, sourceType, targetType);
    }
View Full Code Here


    @SuppressWarnings("unchecked")
    public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
      AnnotationConverterKey converterKey =
          new AnnotationConverterKey(targetType.getAnnotation(annotationType), targetType.getObjectType());
      GenericConverter converter = cachedParsers.get(converterKey);
      if (converter == null) {
        Parser<?> parser = annotationFormatterFactory.getParser(
            converterKey.getAnnotation(), converterKey.getFieldType());
        converter = new ParserConverter(fieldType, parser, FormattingConversionService.this);
        cachedParsers.put(converterKey, converter);
      }
      return converter.convert(source, sourceType, targetType);
    }
View Full Code Here

      throw new IllegalArgumentException("The targetType to convert to cannot be null");
    }
    if (sourceType == null) {
      return true;
    }
    GenericConverter converter = getConverter(sourceType, targetType);
    return (converter != null);
  }
View Full Code Here

    }
    if (source != null && !sourceType.getObjectType().isInstance(source)) {
      throw new IllegalArgumentException("The source to convert from must be an instance of " +
          sourceType + "; instead it was a " + source.getClass().getName());
    }
    GenericConverter converter = getConverter(sourceType, targetType);
    if (converter != null) {
      Object result = ConversionUtils.invokeConverter(converter, source, sourceType, targetType);
      return handleResult(sourceType, targetType, result);
    }
    else {
View Full Code Here

   * @return the generic converter that will perform the conversion, or <code>null</code> if no suitable converter was found
   * @see #getDefaultConverter(TypeDescriptor, TypeDescriptor)
   */
  protected GenericConverter getConverter(TypeDescriptor sourceType, TypeDescriptor targetType) {
    ConverterCacheKey key = new ConverterCacheKey(sourceType, targetType);
    GenericConverter converter = this.converterCache.get(key);
    if (converter != null) {
      return (converter != NO_MATCH ? converter : null);
    }
    else {
      converter = findConverterForClassPair(sourceType, targetType);
View Full Code Here

      LinkedList<Class<?>> classQueue = new LinkedList<Class<?>>();
      classQueue.addFirst(sourceObjectType);
      while (!classQueue.isEmpty()) {
        Class<?> currentClass = classQueue.removeLast();
        Map<Class<?>, MatchableConverters> converters = getTargetConvertersForSource(currentClass);
        GenericConverter converter = getMatchingConverterForTarget(sourceType, targetType, converters);
        if (converter != null) {
          return converter;
        }
        Class<?>[] interfaces = currentClass.getInterfaces();
        for (Class<?> ifc : interfaces) {
          classQueue.addFirst(ifc);
        }
      }
      Map<Class<?>, MatchableConverters> objectConverters = getTargetConvertersForSource(Object.class);
      return getMatchingConverterForTarget(sourceType, targetType, objectConverters);
    }
    else if (sourceObjectType.isArray()) {
      LinkedList<Class<?>> classQueue = new LinkedList<Class<?>>();
      classQueue.addFirst(sourceObjectType);
      while (!classQueue.isEmpty()) {
        Class<?> currentClass = classQueue.removeLast();
        Map<Class<?>, MatchableConverters> converters = getTargetConvertersForSource(currentClass);
        GenericConverter converter = getMatchingConverterForTarget(sourceType, targetType, converters);
        if (converter != null) {
          return converter;
        }
        Class<?> componentType = ClassUtils.resolvePrimitiveIfNecessary(currentClass.getComponentType());
        if (componentType.getSuperclass() != null) {
          classQueue.addFirst(Array.newInstance(componentType.getSuperclass(), 0).getClass());
        }
        else if (componentType.isInterface()) {
          classQueue.addFirst(Object[].class);
        }
      }
      return null;
    }
    else {
      HashSet<Class<?>> interfaces = new LinkedHashSet<Class<?>>();
      LinkedList<Class<?>> classQueue = new LinkedList<Class<?>>();
      classQueue.addFirst(sourceObjectType);
      while (!classQueue.isEmpty()) {
        Class<?> currentClass = classQueue.removeLast();
        Map<Class<?>, MatchableConverters> converters = getTargetConvertersForSource(currentClass);
        GenericConverter converter = getMatchingConverterForTarget(sourceType, targetType, converters);
        if (converter != null) {
          return converter;
        }
        Class<?> superClass = currentClass.getSuperclass();
        if (superClass != null && superClass != Object.class) {
          classQueue.addFirst(superClass);
        }
        for (Class<?> interfaceType : currentClass.getInterfaces()) {
          addInterfaceHierarchy(interfaceType, interfaces);
        }
      }
      for (Class<?> interfaceType : interfaces) {
        Map<Class<?>, MatchableConverters> converters = getTargetConvertersForSource(interfaceType);
        GenericConverter converter = getMatchingConverterForTarget(sourceType, targetType, converters);
        if (converter != null) {
          return converter;
        }
      }
      Map<Class<?>, MatchableConverters> objectConverters = getTargetConvertersForSource(Object.class);
View Full Code Here

      LinkedList<Class<?>> classQueue = new LinkedList<Class<?>>();
      classQueue.addFirst(targetObjectType);
      while (!classQueue.isEmpty()) {
        Class<?> currentClass = classQueue.removeLast();
        MatchableConverters matchable = converters.get(currentClass);
        GenericConverter converter = matchConverter(matchable, sourceType, targetType);
        if (converter != null) {
          return converter;
        }
        Class<?>[] interfaces = currentClass.getInterfaces();
        for (Class<?> ifc : interfaces) {
          classQueue.addFirst(ifc);
        }
      }
      return matchConverter(converters.get(Object.class), sourceType, targetType);
    }
    else if (targetObjectType.isArray()) {
      LinkedList<Class<?>> classQueue = new LinkedList<Class<?>>();
      classQueue.addFirst(targetObjectType);
      while (!classQueue.isEmpty()) {
        Class<?> currentClass = classQueue.removeLast();
        MatchableConverters matchable = converters.get(currentClass);
        GenericConverter converter = matchConverter(matchable, sourceType, targetType);
        if (converter != null) {
          return converter;
        }
        Class<?> componentType = ClassUtils.resolvePrimitiveIfNecessary(currentClass.getComponentType());
        if (componentType.getSuperclass() != null) {
          classQueue.addFirst(Array.newInstance(componentType.getSuperclass(), 0).getClass());
        }
        else if (componentType.isInterface()) {
          classQueue.addFirst(Object[].class);
        }
      }
      return null;
    }
    else {
      Set<Class<?>> interfaces = new LinkedHashSet<Class<?>>();
      LinkedList<Class<?>> classQueue = new LinkedList<Class<?>>();
      classQueue.addFirst(targetObjectType);
      while (!classQueue.isEmpty()) {
        Class<?> currentClass = classQueue.removeLast();
        MatchableConverters matchable = converters.get(currentClass);
        GenericConverter converter = matchConverter(matchable, sourceType, targetType);
        if (converter != null) {
          return converter;
        }
        Class<?> superClass = currentClass.getSuperclass();
        if (superClass != null && superClass != Object.class) {
          classQueue.addFirst(superClass);
        }
        for (Class<?> interfaceType : currentClass.getInterfaces()) {
          addInterfaceHierarchy(interfaceType, interfaces);
        }
      }
      for (Class<?> interfaceType : interfaces) {
        MatchableConverters matchable = converters.get(interfaceType);
        GenericConverter converter = matchConverter(matchable, sourceType, targetType);
        if (converter != null) {
          return converter;
        }
      }
      return matchConverter(converters.get(Object.class), sourceType, targetType);
View Full Code Here

        target.addAll(sourceCollection);
        return target;
      }
    }
    Collection target = CollectionFactory.createCollection(targetType.getType(), sourceCollection.size());
    GenericConverter converter = this.conversionService.getConverter(sourceElementType, targetElementType);
    if (converter == null) {
      throw new ConverterNotFoundException(sourceElementType, targetElementType);
    }
    for (Object element : sourceCollection) {
      target.add(ConversionUtils.invokeConverter(converter, element, sourceElementType, targetElementType));
View Full Code Here

          i++;
        }
        return string.toString();
      }
      else {
        GenericConverter converter = this.conversionService.getConverter(sourceElementType, targetType);
        if (converter == null) {
          throw new ConverterNotFoundException(sourceElementType, targetType);
        }
        StringBuilder string = new StringBuilder();
        int i = 0;
View Full Code Here

      if (ann == null) {
        throw new IllegalStateException(
            "Expected [" + this.annotationType.getName() + "] to be present on " + sourceType);
      }
      AnnotationConverterKey converterKey = new AnnotationConverterKey(ann, sourceType.getObjectType());
      GenericConverter converter = cachedPrinters.get(converterKey);
      if (converter == null) {
        Printer<?> printer = this.annotationFormatterFactory.getPrinter(
            converterKey.getAnnotation(), converterKey.getFieldType());
        converter = new PrinterConverter(this.fieldType, printer, FormattingConversionService.this);
        cachedPrinters.put(converterKey, converter);
      }
      return converter.convert(source, sourceType, targetType);
    }
View Full Code Here

TOP

Related Classes of org.springframework.core.convert.converter.GenericConverter

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.