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