use of cn.taketoday.core.conversion.ConversionService in project today-framework by TAKETODAY.
the class ConversionServiceFactoryBeanTests method createDefaultConversionServiceWithSupplements.
@Test
public void createDefaultConversionServiceWithSupplements() {
ConversionServiceFactoryBean factory = new ConversionServiceFactoryBean();
Set<Object> converters = new HashSet<>();
converters.add(new Converter<String, Foo>() {
@Override
public Foo convert(String source) {
return new Foo();
}
});
converters.add(new ConverterFactory<String, Bar>() {
@Override
public <T extends Bar> Converter<String, T> getConverter(Class<T> targetType) {
return new Converter<>() {
@SuppressWarnings("unchecked")
@Override
public T convert(String source) {
return (T) new Bar();
}
};
}
});
converters.add(new GenericConverter() {
@Override
public Set<ConvertiblePair> getConvertibleTypes() {
return Collections.singleton(new ConvertiblePair(String.class, Baz.class));
}
@Override
@Nullable
public Object convert(@Nullable Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
return new Baz();
}
});
factory.setConverters(converters);
factory.afterPropertiesSet();
ConversionService service = factory.getObject();
assertThat(service.canConvert(String.class, Integer.class)).isTrue();
assertThat(service.canConvert(String.class, Foo.class)).isTrue();
assertThat(service.canConvert(String.class, Bar.class)).isTrue();
assertThat(service.canConvert(String.class, Baz.class)).isTrue();
}
use of cn.taketoday.core.conversion.ConversionService in project today-framework by TAKETODAY.
the class MessageEndpointParameterResolver method resolve.
@Override
public Object resolve(WebSocketSession session, cn.taketoday.web.socket.Message<?> message, ResolvableMethodParameter parameter) {
Object payload = message.getPayload();
if (supportParameterType.isInstance(payload)) {
return payload;
}
Converter converter = getConverter();
if (converter != null) {
return converter.convert(payload);
}
ConversionService conversionService = getConversionService();
Assert.state(conversionService != null, "No ConversionService");
TypeDescriptor targetType = parameter.getTypeDescriptor();
return conversionService.convert(payload, targetType);
}
use of cn.taketoday.core.conversion.ConversionService in project today-framework by TAKETODAY.
the class ApplicationConversionService method addDelimitedStringConverters.
/**
* Add converters to support delimited strings.
*
* @param registry the registry of converters to add to (must also be castable to
* ConversionService, e.g. being a {@link ConfigurableConversionService})
* @throws ClassCastException if the given ConverterRegistry could not be cast to a
* ConversionService
*/
public static void addDelimitedStringConverters(ConverterRegistry registry) {
ConversionService service = (ConversionService) registry;
registry.addConverter(new ArrayToDelimitedStringConverter(service));
registry.addConverter(new CollectionToDelimitedStringConverter(service));
registry.addConverter(new DelimitedStringToArrayConverter(service));
registry.addConverter(new DelimitedStringToCollectionConverter(service));
}
use of cn.taketoday.core.conversion.ConversionService in project today-framework by TAKETODAY.
the class ConversionServiceDeducerTests method getConversionServiceWhenHasQualifiedConverterBeansContainsCustomizedApplicationService.
@Test
void getConversionServiceWhenHasQualifiedConverterBeansContainsCustomizedApplicationService() {
ApplicationContext applicationContext = new AnnotationConfigApplicationContext(CustomConverterConfiguration.class);
ConversionServiceDeducer deducer = new ConversionServiceDeducer(applicationContext);
List<ConversionService> conversionServices = deducer.getConversionServices();
assertThat(conversionServices).hasSize(1);
assertThat(conversionServices.get(0)).isNotSameAs(ApplicationConversionService.getSharedInstance());
assertThat(conversionServices.get(0).canConvert(InputStream.class, OutputStream.class)).isTrue();
}
use of cn.taketoday.core.conversion.ConversionService in project today-framework by TAKETODAY.
the class ConversionServiceDeducer method getConversionServices.
private List<ConversionService> getConversionServices(ConfigurableApplicationContext applicationContext) {
List<ConversionService> conversionServices = new ArrayList<>();
if (applicationContext.getBeanFactory().getConversionService() != null) {
conversionServices.add(applicationContext.getBeanFactory().getConversionService());
}
ConverterBeans converterBeans = new ConverterBeans(applicationContext);
if (!converterBeans.isEmpty()) {
ApplicationConversionService beansConverterService = new ApplicationConversionService();
converterBeans.addTo(beansConverterService);
conversionServices.add(beansConverterService);
}
return conversionServices;
}
Aggregations