Search in sources :

Example 16 with TypeDescriptor

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

the class Spr7538Tests method repro.

@Test
void repro() throws Exception {
    AlwaysTrueReleaseStrategy target = new AlwaysTrueReleaseStrategy();
    BeanFactoryTypeConverter converter = new BeanFactoryTypeConverter();
    StandardEvaluationContext context = new StandardEvaluationContext();
    context.setTypeConverter(converter);
    List<Foo> arguments = Collections.emptyList();
    List<TypeDescriptor> paramDescriptors = new ArrayList<>();
    Method method = AlwaysTrueReleaseStrategy.class.getMethod("checkCompleteness", List.class);
    paramDescriptors.add(new TypeDescriptor(new MethodParameter(method, 0)));
    List<TypeDescriptor> argumentTypes = new ArrayList<>();
    argumentTypes.add(TypeDescriptor.fromObject(arguments));
    ReflectiveMethodResolver resolver = new ReflectiveMethodResolver();
    MethodExecutor executor = resolver.resolve(context, target, "checkCompleteness", argumentTypes);
    Object result = executor.execute(context, target, arguments);
    System.out.println("Result: " + result);
}
Also used : ArrayList(java.util.ArrayList) Method(java.lang.reflect.Method) TypeDescriptor(cn.taketoday.core.TypeDescriptor) MethodExecutor(cn.taketoday.expression.MethodExecutor) MethodParameter(cn.taketoday.core.MethodParameter) Test(org.junit.jupiter.api.Test)

Example 17 with TypeDescriptor

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

the class GenericConversionServiceTests method adaptedCollectionTypesFromSameSourceType.

@Test
void adaptedCollectionTypesFromSameSourceType() throws Exception {
    conversionService.addConverter(new MyStringToStringCollectionConverter());
    assertThat(conversionService.convert("test", TypeDescriptor.valueOf(String.class), new TypeDescriptor(getClass().getField("stringCollection")))).isEqualTo(Collections.singleton("testX"));
    assertThat(conversionService.convert("test", TypeDescriptor.valueOf(String.class), new TypeDescriptor(getClass().getField("genericCollection")))).isEqualTo(Collections.singleton("testX"));
    assertThat(conversionService.convert("test", TypeDescriptor.valueOf(String.class), new TypeDescriptor(getClass().getField("rawCollection")))).isEqualTo(Collections.singleton("testX"));
    assertThat(conversionService.convert("test", TypeDescriptor.valueOf(String.class), new TypeDescriptor(getClass().getField("genericCollection")))).isEqualTo(Collections.singleton("testX"));
    assertThat(conversionService.convert("test", TypeDescriptor.valueOf(String.class), new TypeDescriptor(getClass().getField("stringCollection")))).isEqualTo(Collections.singleton("testX"));
    assertThat(conversionService.convert("test", TypeDescriptor.valueOf(String.class), new TypeDescriptor(getClass().getField("rawCollection")))).isEqualTo(Collections.singleton("testX"));
    assertThatExceptionOfType(ConverterNotFoundException.class).isThrownBy(() -> conversionService.convert("test", TypeDescriptor.valueOf(String.class), new TypeDescriptor(getClass().getField("integerCollection"))));
}
Also used : TypeDescriptor(cn.taketoday.core.TypeDescriptor) ConverterNotFoundException(cn.taketoday.core.conversion.ConverterNotFoundException) Test(org.junit.jupiter.api.Test)

Example 18 with TypeDescriptor

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

the class GenericConversionServiceTests method wildcardMap.

@Test
void wildcardMap() throws Exception {
    Map<String, String> input = new LinkedHashMap<>();
    input.put("key", "value");
    Object converted = conversionService.convert(input, TypeDescriptor.fromObject(input), new TypeDescriptor(getClass().getField("wildcardMap")));
    assertThat(converted).isEqualTo(input);
}
Also used : TypeDescriptor(cn.taketoday.core.TypeDescriptor) LinkedHashMap(java.util.LinkedHashMap) Test(org.junit.jupiter.api.Test)

Example 19 with TypeDescriptor

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

the class GenericConversionServiceTests method multipleCollectionTypesFromSameSourceType.

@Test
void multipleCollectionTypesFromSameSourceType() throws Exception {
    conversionService.addConverter(new MyStringToRawCollectionConverter());
    conversionService.addConverter(new MyStringToGenericCollectionConverter());
    conversionService.addConverter(new MyStringToStringCollectionConverter());
    conversionService.addConverter(new MyStringToIntegerCollectionConverter());
    assertThat(conversionService.convert("test", TypeDescriptor.valueOf(String.class), new TypeDescriptor(getClass().getField("stringCollection")))).isEqualTo(Collections.singleton("testX"));
    assertThat(conversionService.convert("test", TypeDescriptor.valueOf(String.class), new TypeDescriptor(getClass().getField("integerCollection")))).isEqualTo(Collections.singleton(4));
    assertThat(conversionService.convert("test", TypeDescriptor.valueOf(String.class), new TypeDescriptor(getClass().getField("rawCollection")))).isEqualTo(Collections.singleton(4));
    assertThat(conversionService.convert("test", TypeDescriptor.valueOf(String.class), new TypeDescriptor(getClass().getField("genericCollection")))).isEqualTo(Collections.singleton(4));
    assertThat(conversionService.convert("test", TypeDescriptor.valueOf(String.class), new TypeDescriptor(getClass().getField("rawCollection")))).isEqualTo(Collections.singleton(4));
    assertThat(conversionService.convert("test", TypeDescriptor.valueOf(String.class), new TypeDescriptor(getClass().getField("stringCollection")))).isEqualTo(Collections.singleton("testX"));
}
Also used : TypeDescriptor(cn.taketoday.core.TypeDescriptor) Test(org.junit.jupiter.api.Test)

Example 20 with TypeDescriptor

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

the class GenericConversionServiceTests method emptyListToArray.

@Test
void emptyListToArray() {
    conversionService.addConverter(new CollectionToArrayConverter(conversionService));
    conversionService.addConverterFactory(new StringToNumberConverterFactory());
    List<String> list = new ArrayList<>();
    TypeDescriptor sourceType = TypeDescriptor.fromObject(list);
    TypeDescriptor targetType = TypeDescriptor.valueOf(String[].class);
    assertThat(conversionService.canConvert(sourceType, targetType)).isTrue();
    assertThat(((String[]) conversionService.convert(list, sourceType, targetType)).length).isEqualTo(0);
}
Also used : TypeDescriptor(cn.taketoday.core.TypeDescriptor) ArrayList(java.util.ArrayList) Test(org.junit.jupiter.api.Test)

Aggregations

TypeDescriptor (cn.taketoday.core.TypeDescriptor)221 Test (org.junit.jupiter.api.Test)128 ArrayList (java.util.ArrayList)44 Nullable (cn.taketoday.lang.Nullable)36 List (java.util.List)28 LinkedHashMap (java.util.LinkedHashMap)20 MethodParameter (cn.taketoday.core.MethodParameter)18 ConverterNotFoundException (cn.taketoday.core.conversion.ConverterNotFoundException)18 HashMap (java.util.HashMap)18 ConversionFailedException (cn.taketoday.core.conversion.ConversionFailedException)16 StandardEvaluationContext (cn.taketoday.expression.spel.support.StandardEvaluationContext)16 MultiValueMap (cn.taketoday.core.MultiValueMap)14 EnumMap (java.util.EnumMap)12 MethodExecutor (cn.taketoday.expression.MethodExecutor)10 Collection (java.util.Collection)10 Map (java.util.Map)10 ConversionService (cn.taketoday.core.conversion.ConversionService)8 ClassPathResource (cn.taketoday.core.io.ClassPathResource)8 AccessException (cn.taketoday.expression.AccessException)8 EvaluationException (cn.taketoday.expression.EvaluationException)8