Java Code Examples for org.springframework.core.convert.ConversionService#canConvert()

The following examples show how to use org.springframework.core.convert.ConversionService#canConvert() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ConversionUtils.java    From java-technology-stack with MIT License 6 votes vote down vote up
public static boolean canConvertElements(@Nullable TypeDescriptor sourceElementType,
		@Nullable TypeDescriptor targetElementType, ConversionService conversionService) {

	if (targetElementType == null) {
		// yes
		return true;
	}
	if (sourceElementType == null) {
		// maybe
		return true;
	}
	if (conversionService.canConvert(sourceElementType, targetElementType)) {
		// yes
		return true;
	}
	if (sourceElementType.getType().isAssignableFrom(targetElementType.getType())) {
		// maybe
		return true;
	}
	// no
	return false;
}
 
Example 2
Source File: ConversionUtils.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
public static boolean canConvertElements(TypeDescriptor sourceElementType, TypeDescriptor targetElementType, ConversionService conversionService) {
	if (targetElementType == null) {
		// yes
		return true;
	}
	if (sourceElementType == null) {
		// maybe
		return true;
	}
	if (conversionService.canConvert(sourceElementType, targetElementType)) {
		// yes
		return true;
	}
	else if (sourceElementType.getType().isAssignableFrom(targetElementType.getType())) {
		// maybe;
		return true;
	}
	else {
		// no;
		return false;
	}
}
 
Example 3
Source File: ConversionUtils.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
public static boolean canConvertElements(TypeDescriptor sourceElementType, TypeDescriptor targetElementType,
		ConversionService conversionService) {

	if (targetElementType == null) {
		// yes
		return true;
	}
	if (sourceElementType == null) {
		// maybe
		return true;
	}
	if (conversionService.canConvert(sourceElementType, targetElementType)) {
		// yes
		return true;
	}
	else if (sourceElementType.getType().isAssignableFrom(targetElementType.getType())) {
		// maybe
		return true;
	}
	else {
		// no
		return false;
	}
}
 
Example 4
Source File: ServletModelAttributeMethodProcessor.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Create a model attribute from a String request value (e.g. URI template
 * variable, request parameter) using type conversion.
 * <p>The default implementation converts only if there a registered
 * {@link Converter} that can perform the conversion.
 * @param sourceValue the source value to create the model attribute from
 * @param attributeName the name of the attribute (never {@code null})
 * @param methodParam the method parameter
 * @param binderFactory for creating WebDataBinder instance
 * @param request the current request
 * @return the created model attribute, or {@code null} if no suitable
 * conversion found
 * @throws Exception
 */
protected Object createAttributeFromRequestValue(String sourceValue, String attributeName,
		MethodParameter methodParam, WebDataBinderFactory binderFactory, NativeWebRequest request)
		throws Exception {

	DataBinder binder = binderFactory.createBinder(request, null, attributeName);
	ConversionService conversionService = binder.getConversionService();
	if (conversionService != null) {
		TypeDescriptor source = TypeDescriptor.valueOf(String.class);
		TypeDescriptor target = new TypeDescriptor(methodParam);
		if (conversionService.canConvert(source, target)) {
			return binder.convertIfNecessary(sourceValue, methodParam.getParameterType(), methodParam);
		}
	}
	return null;
}
 
Example 5
Source File: FormModelMethodArgumentResolver.java    From distributed-transaction-process with MIT License 6 votes vote down vote up
/**
 * Create a model attribute from a String request value (e.g. URI template
 * variable, request parameter) using type conversion.
 * <p>The default implementation converts only if there a registered
 * {@link org.springframework.core.convert.converter.Converter} that can perform the conversion.
 *
 * @param sourceValue   the source value to create the model attribute from
 * @param attributeName the name of the attribute, never {@code null}
 * @param parameter     the method parameter
 * @param binderFactory for creating WebDataBinder instance
 * @param request       the current request
 * @return the created model attribute, or {@code null}
 * @throws Exception
 */
protected Object createAttributeFromRequestValue(String sourceValue,
                                                 String attributeName,
                                                 MethodParameter parameter,
                                                 WebDataBinderFactory binderFactory,
                                                 NativeWebRequest request) throws Exception {
    DataBinder binder = binderFactory.createBinder(request, null, attributeName);
    ConversionService conversionService = binder.getConversionService();
    if (conversionService != null) {
        TypeDescriptor source = TypeDescriptor.valueOf(String.class);
        TypeDescriptor target = new TypeDescriptor(parameter);
        if (conversionService.canConvert(source, target)) {
            return binder.convertIfNecessary(sourceValue, parameter.getParameterType(), parameter);
        }
    }
    return null;
}
 
Example 6
Source File: ServletModelAttributeMethodProcessor.java    From java-technology-stack with MIT License 6 votes vote down vote up
/**
 * Create a model attribute from a String request value (e.g. URI template
 * variable, request parameter) using type conversion.
 * <p>The default implementation converts only if there a registered
 * {@link Converter} that can perform the conversion.
 * @param sourceValue the source value to create the model attribute from
 * @param attributeName the name of the attribute (never {@code null})
 * @param parameter the method parameter
 * @param binderFactory for creating WebDataBinder instance
 * @param request the current request
 * @return the created model attribute, or {@code null} if no suitable
 * conversion found
 */
@Nullable
protected Object createAttributeFromRequestValue(String sourceValue, String attributeName,
		MethodParameter parameter, WebDataBinderFactory binderFactory, NativeWebRequest request)
		throws Exception {

	DataBinder binder = binderFactory.createBinder(request, null, attributeName);
	ConversionService conversionService = binder.getConversionService();
	if (conversionService != null) {
		TypeDescriptor source = TypeDescriptor.valueOf(String.class);
		TypeDescriptor target = new TypeDescriptor(parameter);
		if (conversionService.canConvert(source, target)) {
			return binder.convertIfNecessary(sourceValue, parameter.getParameterType(), parameter);
		}
	}
	return null;
}
 
Example 7
Source File: ConvertedDatatablesData.java    From springlets with Apache License 2.0 6 votes vote down vote up
private static Map<String, Object> convert(Object value, ConversionService conversionService) {

    BeanWrapper bean = new BeanWrapperImpl(value);
    PropertyDescriptor[] properties = bean.getPropertyDescriptors();
    Map<String, Object> convertedValue = new HashMap<>(properties.length);

    for (int i = 0; i < properties.length; i++) {
      String name = properties[i].getName();
      Object propertyValue = bean.getPropertyValue(name);
      if (propertyValue != null
          && conversionService.canConvert(propertyValue.getClass(), String.class)) {
        TypeDescriptor source = bean.getPropertyTypeDescriptor(name);
        String convertedPropertyValue =
            (String) conversionService.convert(propertyValue, source, TYPE_STRING);
        convertedValue.put(name, convertedPropertyValue);
      }
    }

    return convertedValue;
  }
 
Example 8
Source File: ConversionUtils.java    From spring-analysis-note with MIT License 6 votes vote down vote up
public static boolean canConvertElements(@Nullable TypeDescriptor sourceElementType,
		@Nullable TypeDescriptor targetElementType, ConversionService conversionService) {

	if (targetElementType == null) {
		// yes
		return true;
	}
	if (sourceElementType == null) {
		// maybe
		return true;
	}
	if (conversionService.canConvert(sourceElementType, targetElementType)) {
		// yes
		return true;
	}
	if (ClassUtils.isAssignable(sourceElementType.getType(), targetElementType.getType())) {
		// maybe
		return true;
	}
	// no
	return false;
}
 
Example 9
Source File: CompositeConversionService.java    From camel-spring-boot with Apache License 2.0 5 votes vote down vote up
@Override
public boolean canConvert(TypeDescriptor sourceType, TypeDescriptor targetType) {
    for (ConversionService service : this.delegates) {
        if (service.canConvert(sourceType, targetType)) {
            return true;
        }
    }
    return false;
}
 
Example 10
Source File: CompositeConversionService.java    From camel-spring-boot with Apache License 2.0 5 votes vote down vote up
@Override
public <T> T convert(Object source, Class<T> targetType) {
    for (int i = 0; i < this.delegates.size() - 1; i++) {
        try {
            ConversionService delegate = this.delegates.get(i);
            if (delegate.canConvert(source.getClass(), targetType)) {
                return delegate.convert(source, targetType);
            }
        } catch (ConversionException e) {
            // ignored
        }
    }

    return this.delegates.get(this.delegates.size() - 1).convert(source, targetType);
}
 
Example 11
Source File: ConvertingPropertyEditorAdapter.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * Create a new ConvertingPropertyEditorAdapter for a given
 * {@link org.springframework.core.convert.ConversionService}
 * and the given target type.
 * @param conversionService the ConversionService to delegate to
 * @param targetDescriptor the target type to convert to
 */
public ConvertingPropertyEditorAdapter(ConversionService conversionService, TypeDescriptor targetDescriptor) {
	Assert.notNull(conversionService, "ConversionService must not be null");
	Assert.notNull(targetDescriptor, "TypeDescriptor must not be null");
	this.conversionService = conversionService;
	this.targetDescriptor = targetDescriptor;
	this.canConvertToString = conversionService.canConvert(this.targetDescriptor, TypeDescriptor.valueOf(String.class));
}
 
Example 12
Source File: ConvertingPropertyEditorAdapter.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * Create a new ConvertingPropertyEditorAdapter for a given
 * {@link org.springframework.core.convert.ConversionService}
 * and the given target type.
 * @param conversionService the ConversionService to delegate to
 * @param targetDescriptor the target type to convert to
 */
public ConvertingPropertyEditorAdapter(ConversionService conversionService, TypeDescriptor targetDescriptor) {
	Assert.notNull(conversionService, "ConversionService must not be null");
	Assert.notNull(targetDescriptor, "TypeDescriptor must not be null");
	this.conversionService = conversionService;
	this.targetDescriptor = targetDescriptor;
	this.canConvertToString = conversionService.canConvert(this.targetDescriptor, TypeDescriptor.valueOf(String.class));
}
 
Example 13
Source File: CompositeConversionService.java    From camel-spring-boot with Apache License 2.0 5 votes vote down vote up
@Override
public boolean canConvert(Class<?> sourceType, Class<?> targetType) {
    for (ConversionService service : this.delegates) {
        if (service.canConvert(sourceType, targetType)) {
            return true;
        }
    }
    return false;
}
 
Example 14
Source File: BindConverter.java    From spring-cloud-gray with Apache License 2.0 5 votes vote down vote up
@Override
public boolean canConvert(TypeDescriptor sourceType, TypeDescriptor targetType) {
    for (ConversionService service : this.delegates) {
        if (service.canConvert(sourceType, targetType)) {
            return true;
        }
    }
    return false;
}
 
Example 15
Source File: BindConverter.java    From spring-cloud-gray with Apache License 2.0 5 votes vote down vote up
@Override
public Object convert(Object source, TypeDescriptor sourceType,
                      TypeDescriptor targetType) {
    for (int i = 0; i < this.delegates.size() - 1; i++) {
        try {
            ConversionService delegate = this.delegates.get(i);
            if (delegate.canConvert(sourceType, targetType)) {
                return delegate.convert(source, sourceType, targetType);
            }
        } catch (ConversionException ex) {
        }
    }
    return this.delegates.get(this.delegates.size() - 1).convert(source,
            sourceType, targetType);
}
 
Example 16
Source File: SpringTypeConverter.java    From camel-spring-boot with Apache License 2.0 5 votes vote down vote up
@Override
public <T> T convertTo(Class<T> type, Exchange exchange, Object value) throws TypeConversionException {
    // do not attempt to convert Camel types
    if (type.getCanonicalName().startsWith("org.apache")) {
        return null;
    }
    
    // do not attempt to convert List -> Map. Ognl expression may use this converter as a fallback expecting null
    if (type.isAssignableFrom(Map.class) && isArrayOrCollection(value)) {
        return null;
    }

    TypeDescriptor sourceType = types.computeIfAbsent(value.getClass(), TypeDescriptor::valueOf);
    TypeDescriptor targetType = types.computeIfAbsent(type, TypeDescriptor::valueOf);

    for (ConversionService conversionService : conversionServices) {
        if (conversionService.canConvert(sourceType, targetType)) {
            try {
                return (T)conversionService.convert(value, sourceType, targetType);
            } catch (ConversionFailedException e) {
                // if value is a collection or an array the check ConversionService::canConvert
                // may return true but then the conversion of specific objects may fail
                //
                // https://issues.apache.org/jira/browse/CAMEL-10548
                // https://jira.spring.io/browse/SPR-14971
                //
                if (e.getCause() instanceof ConverterNotFoundException && isArrayOrCollection(value)) {
                    return null;
                } else {
                    throw new TypeConversionException(value, type, e);
                }
            }
        }
    }

    return null;
}
 
Example 17
Source File: CamelSpringCloudServiceRegistry.java    From camel-spring-boot with Apache License 2.0 5 votes vote down vote up
private Registration convertServiceDefinition(ServiceDefinition definition) {
    for (int i = 0; i < conversionServices.size(); i++) {
        ConversionService cs = conversionServices.get(i);

        if (cs.canConvert(ServiceDefinition.class, registrationType)) {
            return cs.convert(definition, registrationType);
        }
    }

    throw new IllegalStateException("Unable to convert service definition to native registration of type:" + registrationType);
}
 
Example 18
Source File: ConvertingPropertyEditorAdapter.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Create a new ConvertingPropertyEditorAdapter for a given
 * {@link org.springframework.core.convert.ConversionService}
 * and the given target type.
 * @param conversionService the ConversionService to delegate to
 * @param targetDescriptor the target type to convert to
 */
public ConvertingPropertyEditorAdapter(ConversionService conversionService, TypeDescriptor targetDescriptor) {
	Assert.notNull(conversionService, "ConversionService must not be null");
	Assert.notNull(targetDescriptor, "TypeDescriptor must not be null");
	this.conversionService = conversionService;
	this.targetDescriptor = targetDescriptor;
	this.canConvertToString = conversionService.canConvert(this.targetDescriptor, TypeDescriptor.valueOf(String.class));
}
 
Example 19
Source File: DefaultJpaStoreImpl.java    From spring-content with Apache License 2.0 5 votes vote down vote up
protected Object convertToExternalContentIdType(S property, Object contentId) {
	ConversionService converter = new DefaultConversionService();
	if (converter.canConvert(TypeDescriptor.forObject(contentId),
			TypeDescriptor.valueOf(BeanUtils.getFieldWithAnnotationType(property,
					ContentId.class)))) {
		contentId = converter.convert(contentId, TypeDescriptor.forObject(contentId),
				TypeDescriptor.valueOf(BeanUtils.getFieldWithAnnotationType(property,
						ContentId.class)));
		return contentId;
	}
	return contentId.toString();
}
 
Example 20
Source File: EntityModelAttributeMethodProcessor.java    From java-platform with Apache License 2.0 4 votes vote down vote up
/**
 * Create a model attribute from a String request value (e.g. URI template
 * variable, request parameter) using type conversion.
 * <p>
 * The default implementation converts only if there a registered
 * {@link Converter} that can perform the conversion.
 * 
 * @param sourceValue
 *            the source value to create the model attribute from
 * @param attributeName
 *            the name of the attribute, never {@code null}
 * @param methodParam
 *            the method parameter
 * @param binderFactory
 *            for creating WebDataBinder instance
 * @param request
 *            the current request
 * @return the created model attribute, or {@code null}
 * @throws Exception
 */
protected Object createAttributeFromRequestValue(String sourceValue, String attributeName,
		MethodParameter methodParam, WebDataBinderFactory binderFactory, NativeWebRequest request)
				throws Exception {

	DataBinder binder = binderFactory.createBinder(request, null, attributeName);
	ConversionService conversionService = binder.getConversionService();
	if (conversionService != null) {
		TypeDescriptor source = TypeDescriptor.valueOf(String.class);
		TypeDescriptor target = new TypeDescriptor(methodParam);
		if (conversionService.canConvert(source, target)) {
			return binder.convertIfNecessary(sourceValue, methodParam.getParameterType(), methodParam);
		}
	}
	return null;
}