Search in sources :

Example 1 with GenericConverter

use of org.springframework.core.convert.converter.GenericConverter in project spring-framework by spring-projects.

the class GenericConversionService method canBypassConvert.

/**
	 * Return whether conversion between the source type and the target type can be bypassed.
	 * <p>More precisely, this method will return true if objects of sourceType can be
	 * converted to the target type by returning the source object unchanged.
	 * @param sourceType context about the source type to convert from
	 * (may be {@code null} if source is {@code null})
	 * @param targetType context about the target type to convert to (required)
	 * @return {@code true} if conversion can be bypassed; {@code false} otherwise
	 * @throws IllegalArgumentException if targetType is {@code null}
	 * @since 3.2
	 */
public boolean canBypassConvert(TypeDescriptor sourceType, TypeDescriptor targetType) {
    Assert.notNull(targetType, "Target type to convert to cannot be null");
    if (sourceType == null) {
        return true;
    }
    GenericConverter converter = getConverter(sourceType, targetType);
    return (converter == NO_OP_CONVERTER);
}
Also used : GenericConverter(org.springframework.core.convert.converter.GenericConverter) ConditionalGenericConverter(org.springframework.core.convert.converter.ConditionalGenericConverter)

Example 2 with GenericConverter

use of org.springframework.core.convert.converter.GenericConverter in project spring-boot by spring-projects.

the class ApplicationConversionService method isConvertViaObjectSourceType.

/**
 * Return {@code true} if objects of {@code sourceType} can be converted to the
 * {@code targetType} and the converter has {@code Object.class} as a supported source
 * type.
 * @param sourceType the source type to test
 * @param targetType the target type to test
 * @return if conversion happens via an {@code ObjectTo...} converter
 * @since 2.4.3
 */
public boolean isConvertViaObjectSourceType(TypeDescriptor sourceType, TypeDescriptor targetType) {
    GenericConverter converter = getConverter(sourceType, targetType);
    Set<ConvertiblePair> pairs = (converter != null) ? converter.getConvertibleTypes() : null;
    if (pairs != null) {
        for (ConvertiblePair pair : pairs) {
            if (Object.class.equals(pair.getSourceType())) {
                return true;
            }
        }
    }
    return false;
}
Also used : GenericConverter(org.springframework.core.convert.converter.GenericConverter) ConvertiblePair(org.springframework.core.convert.converter.GenericConverter.ConvertiblePair)

Example 3 with GenericConverter

use of org.springframework.core.convert.converter.GenericConverter in project spring-data-jdbc by spring-projects.

the class BasicRelationalConverterUnitTests method before.

@BeforeEach
public void before() throws Exception {
    Set<GenericConverter> converters = ConverterBuilder.writing(MyValue.class, String.class, MyValue::getFoo).andReading(MyValue::new).getConverters();
    CustomConversions conversions = new CustomConversions(CustomConversions.StoreConversions.NONE, converters);
    context.setSimpleTypeHolder(conversions.getSimpleTypeHolder());
    converter = new BasicRelationalConverter(context, conversions);
}
Also used : GenericConverter(org.springframework.core.convert.converter.GenericConverter) CustomConversions(org.springframework.data.convert.CustomConversions) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 4 with GenericConverter

use of org.springframework.core.convert.converter.GenericConverter in project spring-boot by spring-projects.

the class ConfigurationPropertiesBindingPostProcessor method getDefaultConversionService.

private ConversionService getDefaultConversionService() {
    if (this.defaultConversionService == null) {
        DefaultConversionService conversionService = new DefaultConversionService();
        this.applicationContext.getAutowireCapableBeanFactory().autowireBean(this);
        for (Converter<?, ?> converter : this.converters) {
            conversionService.addConverter(converter);
        }
        for (GenericConverter genericConverter : this.genericConverters) {
            conversionService.addConverter(genericConverter);
        }
        this.defaultConversionService = conversionService;
    }
    return this.defaultConversionService;
}
Also used : GenericConverter(org.springframework.core.convert.converter.GenericConverter) DefaultConversionService(org.springframework.core.convert.support.DefaultConversionService)

Example 5 with GenericConverter

use of org.springframework.core.convert.converter.GenericConverter in project spring-framework by spring-projects.

the class GenericConversionService method getConverter.

/**
	 * Hook method to lookup the converter for a given sourceType/targetType pair.
	 * First queries this ConversionService's converter cache.
	 * On a cache miss, then performs an exhaustive search for a matching converter.
	 * If no converter matches, returns the default converter.
	 * @param sourceType the source type to convert from
	 * @param targetType the target type to convert to
	 * @return the generic converter that will perform the conversion,
	 * or {@code null} 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);
    }
    converter = this.converters.find(sourceType, targetType);
    if (converter == null) {
        converter = getDefaultConverter(sourceType, targetType);
    }
    if (converter != null) {
        this.converterCache.put(key, converter);
        return converter;
    }
    this.converterCache.put(key, NO_MATCH);
    return null;
}
Also used : GenericConverter(org.springframework.core.convert.converter.GenericConverter) ConditionalGenericConverter(org.springframework.core.convert.converter.ConditionalGenericConverter)

Aggregations

GenericConverter (org.springframework.core.convert.converter.GenericConverter)10 ConditionalGenericConverter (org.springframework.core.convert.converter.ConditionalGenericConverter)4 Test (org.junit.jupiter.api.Test)2 TypeDescriptor (org.springframework.core.convert.TypeDescriptor)2 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1 Set (java.util.Set)1 BeforeEach (org.junit.jupiter.api.BeforeEach)1 ConversionService (org.springframework.core.convert.ConversionService)1 Converter (org.springframework.core.convert.converter.Converter)1 ConvertiblePair (org.springframework.core.convert.converter.GenericConverter.ConvertiblePair)1 DefaultConversionService (org.springframework.core.convert.support.DefaultConversionService)1 CustomConversions (org.springframework.data.convert.CustomConversions)1 Nullable (org.springframework.lang.Nullable)1