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);
}
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;
}
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);
}
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;
}
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;
}
Aggregations