Search in sources :

Example 1 with TypeDescriptor

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);
}
Also used : TypeDescriptor(cn.taketoday.core.TypeDescriptor) Nullable(cn.taketoday.lang.Nullable)

Example 2 with 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);
}
Also used : StandardTypeConverter(cn.taketoday.expression.spel.support.StandardTypeConverter) TypeDescriptor(cn.taketoday.core.TypeDescriptor) GenericConversionService(cn.taketoday.core.conversion.support.GenericConversionService) Test(org.junit.jupiter.api.Test)

Example 3 with TypeDescriptor

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();
}
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 4 with TypeDescriptor

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");
}
Also used : TypeDescriptor(cn.taketoday.core.TypeDescriptor) Collection(java.util.Collection)

Example 5 with TypeDescriptor

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");
}
Also used : TypeDescriptor(cn.taketoday.core.TypeDescriptor)

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