Search in sources :

Example 1 with ConversionService

use of cn.taketoday.core.conversion.ConversionService in project today-infrastructure 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();
}
Also used : ConfigurableApplicationContext(cn.taketoday.context.ConfigurableApplicationContext) AnnotationConfigApplicationContext(cn.taketoday.context.annotation.AnnotationConfigApplicationContext) ApplicationContext(cn.taketoday.context.ApplicationContext) AnnotationConfigApplicationContext(cn.taketoday.context.annotation.AnnotationConfigApplicationContext) ConversionService(cn.taketoday.core.conversion.ConversionService) ApplicationConversionService(cn.taketoday.format.support.ApplicationConversionService) Test(org.junit.jupiter.api.Test)

Example 2 with ConversionService

use of cn.taketoday.core.conversion.ConversionService in project today-infrastructure 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();
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) GenericConverter(cn.taketoday.core.conversion.GenericConverter) TypeDescriptor(cn.taketoday.core.TypeDescriptor) ConversionService(cn.taketoday.core.conversion.ConversionService) Converter(cn.taketoday.core.conversion.Converter) GenericConverter(cn.taketoday.core.conversion.GenericConverter) Nullable(cn.taketoday.lang.Nullable) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test)

Example 3 with ConversionService

use of cn.taketoday.core.conversion.ConversionService in project today-infrastructure by TAKETODAY.

the class StandardBeanExpressionResolver method evaluate.

@Override
@Nullable
public Object evaluate(@Nullable String value, BeanExpressionContext evalContext) throws BeansException {
    if (StringUtils.isEmpty(value)) {
        return value;
    }
    try {
        Expression expr = expressionCache.get(value);
        if (expr == null) {
            expr = expressionParser.parseExpression(value, this.beanExpressionParserContext);
            expressionCache.put(value, expr);
        }
        StandardEvaluationContext sec = evaluationCache.get(evalContext);
        if (sec == null) {
            sec = new StandardEvaluationContext(evalContext);
            sec.addPropertyAccessor(new BeanExpressionContextAccessor());
            sec.addPropertyAccessor(new BeanFactoryAccessor());
            sec.addPropertyAccessor(new MapAccessor());
            sec.addPropertyAccessor(new EnvironmentAccessor());
            sec.setBeanResolver(new BeanFactoryResolver(evalContext.getBeanFactory()));
            sec.setTypeLocator(new StandardTypeLocator(evalContext.getBeanFactory().getBeanClassLoader()));
            sec.setTypeConverter(new StandardTypeConverter(() -> {
                ConversionService cs = evalContext.getBeanFactory().getConversionService();
                return cs != null ? cs : ApplicationConversionService.getSharedInstance();
            }));
            customizeEvaluationContext(sec);
            evaluationCache.put(evalContext, sec);
        }
        return expr.getValue(sec);
    } catch (Throwable ex) {
        throw new BeanExpressionException("Expression parsing failed", ex);
    }
}
Also used : StandardTypeConverter(cn.taketoday.expression.spel.support.StandardTypeConverter) StandardEvaluationContext(cn.taketoday.expression.spel.support.StandardEvaluationContext) BeanExpressionException(cn.taketoday.beans.factory.BeanExpressionException) StandardTypeLocator(cn.taketoday.expression.spel.support.StandardTypeLocator) Expression(cn.taketoday.expression.Expression) ConversionService(cn.taketoday.core.conversion.ConversionService) ApplicationConversionService(cn.taketoday.format.support.ApplicationConversionService) Nullable(cn.taketoday.lang.Nullable)

Example 4 with ConversionService

use of cn.taketoday.core.conversion.ConversionService in project today-infrastructure 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));
}
Also used : ConversionService(cn.taketoday.core.conversion.ConversionService) DefaultConversionService(cn.taketoday.core.conversion.support.DefaultConversionService) ConfigurableConversionService(cn.taketoday.core.conversion.support.ConfigurableConversionService)

Example 5 with ConversionService

use of cn.taketoday.core.conversion.ConversionService in project today-infrastructure by TAKETODAY.

the class ObjectToStringHttpMessageConverterTests method defaultCharsetModified.

@Test
public void defaultCharsetModified() throws IOException {
    ConversionService cs = new DefaultConversionService();
    ObjectToStringHttpMessageConverter converter = new ObjectToStringHttpMessageConverter(cs, StandardCharsets.UTF_16);
    converter.write((byte) 31, null, this.response);
    assertThat(this.servletResponse.getCharacterEncoding()).isEqualTo("UTF-16");
}
Also used : ConversionService(cn.taketoday.core.conversion.ConversionService) DefaultConversionService(cn.taketoday.core.conversion.support.DefaultConversionService) DefaultConversionService(cn.taketoday.core.conversion.support.DefaultConversionService) Test(org.junit.jupiter.api.Test)

Aggregations

ConversionService (cn.taketoday.core.conversion.ConversionService)26 Test (org.junit.jupiter.api.Test)13 ApplicationConversionService (cn.taketoday.format.support.ApplicationConversionService)8 TypeDescriptor (cn.taketoday.core.TypeDescriptor)6 DefaultConversionService (cn.taketoday.core.conversion.support.DefaultConversionService)6 Nullable (cn.taketoday.lang.Nullable)5 ConfigurableApplicationContext (cn.taketoday.context.ConfigurableApplicationContext)4 AnnotationConfigApplicationContext (cn.taketoday.context.annotation.AnnotationConfigApplicationContext)4 Converter (cn.taketoday.core.conversion.Converter)4 BeanExpressionException (cn.taketoday.beans.factory.BeanExpressionException)2 ApplicationContext (cn.taketoday.context.ApplicationContext)2 GenericConverter (cn.taketoday.core.conversion.GenericConverter)2 ConfigurableConversionService (cn.taketoday.core.conversion.support.ConfigurableConversionService)2 Expression (cn.taketoday.expression.Expression)2 StandardEvaluationContext (cn.taketoday.expression.spel.support.StandardEvaluationContext)2 StandardTypeConverter (cn.taketoday.expression.spel.support.StandardTypeConverter)2 StandardTypeLocator (cn.taketoday.expression.spel.support.StandardTypeLocator)2 ServletServerHttpResponse (cn.taketoday.http.server.ServletServerHttpResponse)2 ArrayList (java.util.ArrayList)2 HashSet (java.util.HashSet)2