Package org.springframework.binding.convert

Examples of org.springframework.binding.convert.ConversionExecutor


  protected void setUp() throws Exception {
    this.service = new FacesConversionService();
  }

  public void testGetAbstractType() {
    ConversionExecutor executor = this.service.getConversionExecutor(List.class, DataModel.class);
    ArrayList<Object> list = new ArrayList<Object>();
    list.add("foo");
    executor.execute(list);
  }
View Full Code Here


                "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 ["
                  + targetComponentType.getName() + "]");
        }
      } else {
        if (converter.getSourceClass().isAssignableFrom(sourceClass)) {
          if (!converter.getTargetClass().isAssignableFrom(targetComponentType)) {
            throw new ConversionExecutorNotFoundException(sourceClass, targetClass,
                "Custom ConversionExecutor with id '" + id + "' cannot convert from sourceClass ["
                    + sourceClass.getName() + "] to array holding elements of type ["
                    + targetComponentType.getName() + "]");
          }
          ConversionExecutor elementConverter = new StaticConversionExecutor(sourceClass,
              targetComponentType, converter);
          return new StaticConversionExecutor(sourceClass, targetClass, new ObjectToArray(elementConverter));
        } else if (converter.getTargetClass().isAssignableFrom(sourceClass)
            && converter instanceof TwoWayConverter) {
          if (!converter.getSourceClass().isAssignableFrom(targetComponentType)) {
            throw new ConversionExecutorNotFoundException(sourceClass, targetClass,
                "Custom ConversionExecutor with id '" + id + "' cannot convert from sourceClass ["
                    + sourceClass.getName() + "] to array holding elements of type ["
                    + targetComponentType.getName() + "]");
          }
          TwoWayConverter twoWay = (TwoWayConverter) converter;
          ConversionExecutor elementConverter = new StaticConversionExecutor(sourceClass,
              targetComponentType, new ReverseConverter(twoWay));
          return new StaticConversionExecutor(sourceClass, targetClass, new ObjectToArray(elementConverter));
        }
      }
    }
    if (Collection.class.isAssignableFrom(targetClass)) {
      if (Collection.class.isAssignableFrom(sourceClass)) {
        ConversionExecutor elementConverter;
        // type erasure forces us to do runtime checks of list elements
        if (converter instanceof TwoWayConverter) {
          elementConverter = new TwoWayCapableConversionExecutor(converter.getSourceClass(),
              converter.getTargetClass(), (TwoWayConverter) converter);
        } else {
          elementConverter = new StaticConversionExecutor(converter.getSourceClass(),
              converter.getTargetClass(), converter);
        }
        return new StaticConversionExecutor(sourceClass, targetClass, new CollectionToCollection(
            elementConverter));
      } else {
        ConversionExecutor elementConverter;
        // type erasure forces us to do runtime checks of list elements
        if (converter instanceof TwoWayConverter) {
          elementConverter = new TwoWayCapableConversionExecutor(sourceClass, converter.getTargetClass(),
              (TwoWayConverter) converter);
        } else {
View Full Code Here

    }
  }

  public Object executeConversion(Object source, Class<?> targetClass) throws ConversionException {
    if (source != null) {
      ConversionExecutor conversionExecutor = getConversionExecutor(source.getClass(), targetClass);
      return conversionExecutor.execute(source);
    } else {
      return null;
    }
  }
View Full Code Here

    }
  }

  public Object executeConversion(String converterId, Object source, Class<?> targetClass) throws ConversionException {
    if (source != null) {
      ConversionExecutor conversionExecutor = getConversionExecutor(converterId, source.getClass(), targetClass);
      return conversionExecutor.execute(source);
    } else {
      return null;
    }
  }
View Full Code Here

  }

  private Object getConvertedValue(FlowElementAttribute attribute) {
    if (attribute.needsTypeConversion()) {
      Class<?> targetType = fromStringToClass(attribute.getType());
      ConversionExecutor converter = flowBuilderServices.getConversionService().getConversionExecutor(
          String.class, targetType);
      return converter.execute(attribute.getValue());
    } else {
      return attribute.getValue();
    }
  }
View Full Code Here

  // utility methods

  private Object getConvertedValue(FlowElementAttribute attribute) {
    if (attribute.needsTypeConversion()) {
      Class<?> targetType = fromStringToClass(attribute.getType());
      ConversionExecutor converter = conversionService.getConversionExecutor(String.class, targetType);
      return converter.execute(attribute.getValue());
    } else {
      return attribute.getValue();
    }
  }
View Full Code Here

    DefaultMapping mapping = new DefaultMapping(source, target);
    mapping.setRequired(binding.getRequired());
    if (binding.getConverter() != null) {
      Assert.notNull(conversionService,
          "A ConversionService must be configured to use resolve custom converters to use during binding");
      ConversionExecutor conversionExecutor = conversionService.getConversionExecutor(binding.getConverter(),
          String.class, target.getValueType(model));
      mapping.setTypeConverter(conversionExecutor);
    }
    if (logger.isDebugEnabled()) {
      logger.debug("Adding mapping for parameter '" + binding.getProperty() + "'");
View Full Code Here

  }

  private void parseAndSetMappingConversionExecutor(AbstractMappingModel mappingModel, DefaultMapping mapping) {
    if (StringUtils.hasText(mappingModel.getType())) {
      Class<?> type = toClass(mappingModel.getType());
      ConversionExecutor typeConverter = new RuntimeBindingConversionExecutor(type, getLocalContext()
          .getConversionService());
      mapping.setTypeConverter(typeConverter);
    }
  }
View Full Code Here

   */
  @SuppressWarnings("unchecked")
  private <T> T[] convert(String[] parameters, Class<? extends T> targetElementType)
      throws ConversionExecutionException {
    List<T> list = new ArrayList<T>(parameters.length);
    ConversionExecutor converter = conversionService.getConversionExecutor(String.class, targetElementType);
    for (String parameter : parameters) {
      list.add((T) converter.execute(parameter));
    }
    return list.toArray((T[]) Array.newInstance(targetElementType, parameters.length));
  }
View Full Code Here

  @SuppressWarnings("unchecked")
  public void testRegisterCustomConverterListToList() {
    DefaultConversionService service = new DefaultConversionService();
    service.addConverter("princy", new CustomTwoWayConverter());
    ConversionExecutor executor = service.getConversionExecutor("princy", List.class, List.class);
    List<String> princyList = new ArrayList<String>();
    princyList.add("princy1");
    princyList.add("princy2");
    List<Principal> list = (List<Principal>) executor.execute(princyList);
    assertEquals("princy1", list.get(0).getName());
    assertEquals("princy2", list.get(1).getName());
  }
View Full Code Here

TOP

Related Classes of org.springframework.binding.convert.ConversionExecutor

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.