use of cn.taketoday.core.TypeDescriptor in project today-infrastructure by TAKETODAY.
the class BeanWrapperImpl method convertForProperty.
/**
* Convert the given value for the specified property to the latter's type.
* <p>This method is only intended for optimizations in a BeanFactory.
* Use the {@code convertIfNecessary} methods for programmatic conversion.
*
* @param value the value to convert
* @param propertyName the target property
* (note that nested or indexed properties are not supported here)
* @return the new value, possibly the result of type conversion
* @throws TypeMismatchException if type conversion failed
*/
@Nullable
public Object convertForProperty(@Nullable Object value, String propertyName) throws TypeMismatchException {
BeanProperty beanProperty = getMetadata().getBeanProperty(propertyName);
if (beanProperty == null) {
throw new InvalidPropertyException(getRootClass(), getNestedPath() + propertyName, "No property '" + propertyName + "' found");
}
TypeDescriptor typeDescriptor = beanProperty.getTypeDescriptor();
return convertForProperty(propertyName, null, value, typeDescriptor);
}
use of cn.taketoday.core.TypeDescriptor in project today-infrastructure by TAKETODAY.
the class BooleanExpressionTests method testConvertAndHandleNull.
@Test
public void testConvertAndHandleNull() {
// SPR-9445
// without null conversion
evaluateAndCheckError("null or true", SpelMessage.TYPE_CONVERSION_ERROR, 0, "null", "boolean");
evaluateAndCheckError("null and true", SpelMessage.TYPE_CONVERSION_ERROR, 0, "null", "boolean");
evaluateAndCheckError("!null", SpelMessage.TYPE_CONVERSION_ERROR, 1, "null", "boolean");
evaluateAndCheckError("null ? 'foo' : 'bar'", SpelMessage.TYPE_CONVERSION_ERROR, 0, "null", "boolean");
// with null conversion (null -> false)
GenericConversionService conversionService = new GenericConversionService() {
@Override
protected Object convertNullSource(TypeDescriptor sourceType, TypeDescriptor targetType) {
return targetType.getType() == Boolean.class ? false : null;
}
};
context.setTypeConverter(new StandardTypeConverter(conversionService));
evaluate("null or true", Boolean.TRUE, Boolean.class, false);
evaluate("null and true", Boolean.FALSE, Boolean.class, false);
evaluate("!null", Boolean.TRUE, Boolean.class, false);
evaluate("null ? 'foo' : 'bar'", "bar", String.class, false);
}
use of cn.taketoday.core.TypeDescriptor 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();
}
use of cn.taketoday.core.TypeDescriptor in project today-infrastructure by TAKETODAY.
the class DelimitedStringToCollectionConverterTests method convertWhenHasNoElementTypeShouldReturnTrimmedString.
@ConversionServiceTest
@SuppressWarnings("unchecked")
void convertWhenHasNoElementTypeShouldReturnTrimmedString(ConversionService conversionService) {
TypeDescriptor sourceType = TypeDescriptor.valueOf(String.class);
TypeDescriptor targetType = TypeDescriptor.nested(ReflectionUtils.findField(Values.class, "noElementType"), 0);
Collection<String> converted = (Collection<String>) conversionService.convert(" a | b| c ", sourceType, targetType);
assertThat(converted).containsExactly("a", "b", "c");
}
use of cn.taketoday.core.TypeDescriptor in project today-infrastructure by TAKETODAY.
the class DelimitedStringToCollectionConverterTests method convertWhenHasCollectionObjectTypeShouldUseCollectionObjectType.
@SuppressWarnings("unchecked")
@ConversionServiceTest
void convertWhenHasCollectionObjectTypeShouldUseCollectionObjectType(ConversionService conversionService) {
TypeDescriptor sourceType = TypeDescriptor.valueOf(String.class);
TypeDescriptor targetType = TypeDescriptor.nested(ReflectionUtils.findField(Values.class, "specificType"), 0);
MyCustomList<String> converted = (MyCustomList<String>) conversionService.convert("a*b", sourceType, targetType);
assertThat(converted).containsExactly("a", "b");
}
Aggregations