Package org.springframework.binding.convert.converters

Examples of org.springframework.binding.convert.converters.Converter


    List lst = new ArrayList();
    assertSame(lst, service.getConversionExecutor(ArrayList.class, List.class).execute(lst));
  }

  public void testOverrideConverter() {
    Converter customConverter = new StringToBoolean("ja", "nee");
    DefaultConversionService service = new DefaultConversionService();
    StaticConversionExecutor executor = (StaticConversionExecutor) service.getConversionExecutor(String.class,
        Boolean.class);
    assertNotSame(customConverter, executor.getConverter());
    try {
View Full Code Here


        return new StaticConversionExecutor(sourceClass, targetClass, new ArrayToCollection(this));
      }
    }
    if (targetClass.isArray()) {
      if (Collection.class.isAssignableFrom(sourceClass)) {
        Converter collectionToArray = new ReverseConverter(new ArrayToCollection(this));
        return new StaticConversionExecutor(sourceClass, targetClass, collectionToArray);
      } else {
        return new StaticConversionExecutor(sourceClass, targetClass, new ObjectToArray(this));
      }
    }
    Converter converter = findRegisteredConverter(sourceClass, targetClass);
    if (converter != null) {
      // we found a converter
      return new StaticConversionExecutor(sourceClass, targetClass, converter);
    } else {
      if (parent != null) {
View Full Code Here

  public ConversionExecutor getConversionExecutor(String id, Class sourceClass, Class targetClass)
      throws ConversionExecutorNotFoundException {
    Assert.hasText(id, "The id of the custom converter is required");
    Assert.notNull(sourceClass, "The source class to convert from is required");
    Assert.notNull(targetClass, "The target class to convert to is required");
    Converter converter = (Converter) customConverters.get(id);
    if (converter == null) {
      if (parent != null) {
        return parent.getConversionExecutor(id, sourceClass, targetClass);
      } else {
        throw new ConversionExecutorNotFoundException(sourceClass, targetClass,
            "No custom ConversionExecutor found with id '" + id + "' for converting from sourceClass ["
                + sourceClass.getName() + "] to targetClass [" + targetClass.getName() + "]");
      }
    }
    sourceClass = convertToWrapperClassIfNecessary(sourceClass);
    targetClass = convertToWrapperClassIfNecessary(targetClass);
    if (converter.getSourceClass().isAssignableFrom(sourceClass)) {
      if (!converter.getTargetClass().isAssignableFrom(targetClass)) {
        throw new ConversionExecutorNotFoundException(sourceClass, targetClass,
            "Custom ConversionExecutor with id '" + id + "' cannot convert from sourceClass ["
                + sourceClass.getName() + "] to targetClass [" + targetClass.getName() + "]");
      }
      return new StaticConversionExecutor(sourceClass, targetClass, converter);
    } else if (converter.getTargetClass().isAssignableFrom(sourceClass) && converter instanceof TwoWayConverter) {
      if (!converter.getSourceClass().isAssignableFrom(targetClass)) {
        throw new ConversionExecutorNotFoundException(sourceClass, targetClass,
            "Custom ConversionExecutor with id '" + id + "' cannot convert from sourceClass ["
                + sourceClass.getName() + "] to targetClass [" + targetClass.getName() + "]");
      }
      TwoWayConverter twoWay = (TwoWayConverter) converter;
View Full Code Here

      LinkedList classQueue = new LinkedList();
      classQueue.addFirst(sourceClass);
      while (!classQueue.isEmpty()) {
        Class currentClass = (Class) classQueue.removeLast();
        Map sourceTargetConverters = findConvertersForSource(currentClass);
        Converter converter = findTargetConverter(sourceTargetConverters, targetClass);
        if (converter != null) {
          return converter;
        }
        Class[] interfaces = currentClass.getInterfaces();
        for (int i = 0; i < interfaces.length; i++) {
          classQueue.addFirst(interfaces[i]);
        }
      }
      Map objectConverters = findConvertersForSource(Object.class);
      return findTargetConverter(objectConverters, targetClass);
    } else {
      LinkedList classQueue = new LinkedList();
      classQueue.addFirst(sourceClass);
      while (!classQueue.isEmpty()) {
        Class currentClass = (Class) classQueue.removeLast();
        Map sourceTargetConverters = findConvertersForSource(currentClass);
        Converter converter = findTargetConverter(sourceTargetConverters, targetClass);
        if (converter != null) {
          return converter;
        }
        if (currentClass.getSuperclass() != null) {
          classQueue.addFirst(currentClass.getSuperclass());
View Full Code Here

    Set entries = sourceMap.entrySet();
    Set conversionExecutors = new HashSet(entries.size() + parentExecutors.size());
    for (Iterator it = entries.iterator(); it.hasNext();) {
      Map.Entry entry = (Map.Entry) it.next();
      Class targetClass = (Class) entry.getKey();
      Converter converter = (Converter) entry.getValue();
      conversionExecutors.add(new StaticConversionExecutor(sourceClass, targetClass, converter));
    }
    conversionExecutors.addAll(parentExecutors);
    return conversionExecutors;
  }
View Full Code Here

    if (targetClass.isInterface()) {
      LinkedList classQueue = new LinkedList();
      classQueue.addFirst(targetClass);
      while (!classQueue.isEmpty()) {
        Class currentClass = (Class) classQueue.removeLast();
        Converter converter = (Converter) sourceTargetConverters.get(currentClass);
        if (converter != null) {
          return converter;
        }
        Class[] interfaces = currentClass.getInterfaces();
        for (int i = 0; i < interfaces.length; i++) {
          classQueue.addFirst(interfaces[i]);
        }
      }
      return (Converter) sourceTargetConverters.get(Object.class);
    } else {
      LinkedList classQueue = new LinkedList();
      classQueue.addFirst(targetClass);
      while (!classQueue.isEmpty()) {
        Class currentClass = (Class) classQueue.removeLast();
        Converter converter = (Converter) sourceTargetConverters.get(currentClass);
        if (converter != null) {
          return converter;
        }
        if (currentClass.getSuperclass() != null) {
          classQueue.addFirst(currentClass.getSuperclass());
View Full Code Here

  public ConversionExecutor getConversionExecutor(String id, Class<?> sourceClass, Class<?> targetClass)
      throws ConversionExecutorNotFoundException {
    Assert.hasText(id, "The id of the custom converter is required");
    Assert.notNull(sourceClass, "The source class to convert from is required");
    Assert.notNull(targetClass, "The target class to convert to is required");
    Converter converter = customConverters.get(id);
    if (converter == null) {
      if (parent != null) {
        return parent.getConversionExecutor(id, sourceClass, targetClass);
      } else {
        throw new ConversionExecutorNotFoundException(sourceClass, targetClass,
            "No custom ConversionExecutor found with id '" + id + "' for converting from sourceClass ["
                + sourceClass.getName() + "] to targetClass [" + targetClass.getName() + "]");
      }
    }
    sourceClass = convertToWrapperClassIfNecessary(sourceClass);
    targetClass = convertToWrapperClassIfNecessary(targetClass);
    if (sourceClass.isArray()) {
      Class<?> sourceComponentType = sourceClass.getComponentType();
      if (targetClass.isArray()) {
        Class<?> targetComponentType = targetClass.getComponentType();
        if (converter.getSourceClass().isAssignableFrom(sourceComponentType)) {
          if (!converter.getTargetClass().isAssignableFrom(targetComponentType)) {
            throw new ConversionExecutorNotFoundException(sourceClass, targetClass,
                "Custom ConversionExecutor with id '" + id
                    + "' cannot convert from an array storing elements of type ["
                    + sourceComponentType.getName() + "] to an array of storing elements of type ["
                    + targetComponentType.getName() + "]");
          }
          ConversionExecutor elementConverter = new StaticConversionExecutor(sourceComponentType,
              targetComponentType, converter);
          return new StaticConversionExecutor(sourceClass, targetClass, new ArrayToArray(elementConverter));
        } else if (converter.getTargetClass().isAssignableFrom(sourceComponentType)
            && converter instanceof TwoWayConverter) {
          TwoWayConverter twoWay = (TwoWayConverter) converter;
          ConversionExecutor elementConverter = new StaticConversionExecutor(sourceComponentType,
              targetComponentType, new ReverseConverter(twoWay));
          return new StaticConversionExecutor(sourceClass, targetClass, new ArrayToArray(elementConverter));
        } else {
          throw new ConversionExecutorNotFoundException(sourceClass, targetClass,
              "Custom ConversionExecutor with id '" + id
                  + "' cannot convert from an array storing elements of type ["
                  + sourceComponentType.getName() + "] to an array storing elements of type ["
                  + targetComponentType.getName() + "]");
        }
      } else if (Collection.class.isAssignableFrom(targetClass)) {
        if (!targetClass.isInterface() && Modifier.isAbstract(targetClass.getModifiers())) {
          throw new IllegalArgumentException("Conversion target class [" + targetClass.getName()
              + "] is invalid; cannot convert to abstract collection types--"
              + "request an interface or concrete implementation instead");
        }
        if (converter.getSourceClass().isAssignableFrom(sourceComponentType)) {
          // type erasure has prevented us from getting the concrete type, this is best we can do for now
          ConversionExecutor elementConverter = new StaticConversionExecutor(sourceComponentType,
              converter.getTargetClass(), converter);
          return new StaticConversionExecutor(sourceClass, targetClass, new ArrayToCollection(
              elementConverter));
        } else if (converter.getTargetClass().isAssignableFrom(sourceComponentType)
            && converter instanceof TwoWayConverter) {
          TwoWayConverter twoWay = (TwoWayConverter) converter;
          ConversionExecutor elementConverter = new StaticConversionExecutor(sourceComponentType,
              converter.getSourceClass(), new ReverseConverter(twoWay));
          return new StaticConversionExecutor(sourceClass, targetClass, new ArrayToCollection(
              elementConverter));
        } else {
          throw new ConversionExecutorNotFoundException(sourceClass, targetClass,
              "Custom ConversionExecutor with id '" + id
                  + "' cannot convert from array an storing elements type ["
                  + sourceComponentType.getName() + "] to a collection of type ["
                  + targetClass.getName() + "]");
        }
      }
    }
    if (targetClass.isArray()) {
      Class<?> targetComponentType = targetClass.getComponentType();
      if (Collection.class.isAssignableFrom(sourceClass)) {
        // type erasure limits us here as well
        if (converter.getTargetClass().isAssignableFrom(targetComponentType)) {
          ConversionExecutor elementConverter = new StaticConversionExecutor(converter.getSourceClass(),
              targetComponentType, converter);
          Converter collectionToArray = new ReverseConverter(new ArrayToCollection(elementConverter));
          return new StaticConversionExecutor(sourceClass, targetClass, collectionToArray);
        } else if (converter.getSourceClass().isAssignableFrom(targetComponentType)
            && converter instanceof TwoWayConverter) {
          TwoWayConverter twoWay = (TwoWayConverter) converter;
          ConversionExecutor elementConverter = new StaticConversionExecutor(converter.getTargetClass(),
              targetComponentType, new ReverseConverter(twoWay));
          Converter collectionToArray = new ReverseConverter(new ArrayToCollection(elementConverter));
          return new StaticConversionExecutor(sourceClass, targetClass, collectionToArray);
        } else {
          throw new ConversionExecutorNotFoundException(sourceClass, targetClass,
              "Custom ConversionExecutor with id '" + id + "' cannot convert from collection of type ["
                  + sourceClass.getName() + "] to an array storing elements of type ["
View Full Code Here

    List<Object> lst = new ArrayList<Object>();
    assertSame(lst, service.getConversionExecutor(ArrayList.class, List.class).execute(lst));
  }

  public void testOverrideConverter() {
    Converter customConverter = new StringToBoolean("ja", "nee");
    DefaultConversionService service = new DefaultConversionService();
    StaticConversionExecutor executor = (StaticConversionExecutor) service.getConversionExecutor(String.class,
        Boolean.class);
    try {
      executor.execute("ja");
View Full Code Here

TOP

Related Classes of org.springframework.binding.convert.converters.Converter

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.