Search in sources :

Example 11 with TypeDescriptor

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

the class DelimitedStringToArrayConverterTests method matchesWhenHasAnnotationAndNonConvertibleElementTypeShouldReturnFalse.

@ConversionServiceTest
void matchesWhenHasAnnotationAndNonConvertibleElementTypeShouldReturnFalse(ConversionService conversionService) {
    TypeDescriptor sourceType = TypeDescriptor.valueOf(String.class);
    TypeDescriptor targetType = TypeDescriptor.nested(ReflectionUtils.findField(Values.class, "nonConvertibleElementType"), 0);
    assertThat(new DelimitedStringToArrayConverter(conversionService).matches(sourceType, targetType)).isFalse();
}
Also used : TypeDescriptor(cn.taketoday.core.TypeDescriptor)

Example 12 with TypeDescriptor

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

the class DelimitedStringToArrayConverterTests method matchesWhenTargetIsNotAnnotatedShouldReturnTrue.

@ConversionServiceTest
void matchesWhenTargetIsNotAnnotatedShouldReturnTrue(ConversionService conversionService) {
    TypeDescriptor sourceType = TypeDescriptor.valueOf(String.class);
    TypeDescriptor targetType = TypeDescriptor.nested(ReflectionUtils.findField(Values.class, "noAnnotation"), 0);
    assertThat(new DelimitedStringToArrayConverter(conversionService).matches(sourceType, targetType)).isTrue();
}
Also used : TypeDescriptor(cn.taketoday.core.TypeDescriptor)

Example 13 with TypeDescriptor

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

the class SpelReproTests method varargsAndPrimitives_SPR8174.

@Test
void varargsAndPrimitives_SPR8174() throws Exception {
    EvaluationContext emptyEvalContext = new StandardEvaluationContext();
    List<TypeDescriptor> args = new ArrayList<>();
    args.add(TypeDescriptor.fromObject(34L));
    ReflectionUtil<Integer> ru = new ReflectionUtil<>();
    MethodExecutor me = new ReflectiveMethodResolver().resolve(emptyEvalContext, ru, "methodToCall", args);
    args.set(0, TypeDescriptor.fromObject(23));
    me = new ReflectiveMethodResolver().resolve(emptyEvalContext, ru, "foo", args);
    me.execute(emptyEvalContext, ru, 45);
    args.set(0, TypeDescriptor.fromObject(23f));
    me = new ReflectiveMethodResolver().resolve(emptyEvalContext, ru, "foo", args);
    me.execute(emptyEvalContext, ru, 45f);
    args.set(0, TypeDescriptor.fromObject(23d));
    me = new ReflectiveMethodResolver().resolve(emptyEvalContext, ru, "foo", args);
    me.execute(emptyEvalContext, ru, 23d);
    args.set(0, TypeDescriptor.fromObject((short) 23));
    me = new ReflectiveMethodResolver().resolve(emptyEvalContext, ru, "foo", args);
    me.execute(emptyEvalContext, ru, (short) 23);
    args.set(0, TypeDescriptor.fromObject(23L));
    me = new ReflectiveMethodResolver().resolve(emptyEvalContext, ru, "foo", args);
    me.execute(emptyEvalContext, ru, 23L);
    args.set(0, TypeDescriptor.fromObject((char) 65));
    me = new ReflectiveMethodResolver().resolve(emptyEvalContext, ru, "foo", args);
    me.execute(emptyEvalContext, ru, (char) 65);
    args.set(0, TypeDescriptor.fromObject((byte) 23));
    me = new ReflectiveMethodResolver().resolve(emptyEvalContext, ru, "foo", args);
    me.execute(emptyEvalContext, ru, (byte) 23);
    args.set(0, TypeDescriptor.fromObject(true));
    me = new ReflectiveMethodResolver().resolve(emptyEvalContext, ru, "foo", args);
    me.execute(emptyEvalContext, ru, true);
    // trickier:
    args.set(0, TypeDescriptor.fromObject(12));
    args.add(TypeDescriptor.fromObject(23f));
    me = new ReflectiveMethodResolver().resolve(emptyEvalContext, ru, "bar", args);
    me.execute(emptyEvalContext, ru, 12, 23f);
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) StandardEvaluationContext(cn.taketoday.expression.spel.support.StandardEvaluationContext) TypeDescriptor(cn.taketoday.core.TypeDescriptor) ReflectiveMethodResolver(cn.taketoday.expression.spel.support.ReflectiveMethodResolver) MethodExecutor(cn.taketoday.expression.MethodExecutor) ArrayList(java.util.ArrayList) EvaluationContext(cn.taketoday.expression.EvaluationContext) StandardEvaluationContext(cn.taketoday.expression.spel.support.StandardEvaluationContext) Test(org.junit.jupiter.api.Test)

Example 14 with TypeDescriptor

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

the class SpelReproTests method wideningPrimitiveConversion_SPR8224.

/**
 * Test whether {@link ReflectiveMethodResolver} handles Widening Primitive Conversion. That's passing an 'int' to a
 * method accepting 'long' is ok.
 */
@Test
void wideningPrimitiveConversion_SPR8224() throws Exception {
    class WideningPrimitiveConversion {

        public int getX(long i) {
            return 10;
        }
    }
    final Integer INTEGER_VALUE = 7;
    WideningPrimitiveConversion target = new WideningPrimitiveConversion();
    EvaluationContext emptyEvalContext = new StandardEvaluationContext();
    List<TypeDescriptor> args = new ArrayList<>();
    args.add(TypeDescriptor.fromObject(INTEGER_VALUE));
    MethodExecutor me = new ReflectiveMethodResolver(true).resolve(emptyEvalContext, target, "getX", args);
    final int actual = (Integer) me.execute(emptyEvalContext, target, INTEGER_VALUE).getValue();
    final int compiler = target.getX(INTEGER_VALUE);
    assertThat(actual).isEqualTo(compiler);
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) StandardEvaluationContext(cn.taketoday.expression.spel.support.StandardEvaluationContext) TypeDescriptor(cn.taketoday.core.TypeDescriptor) ReflectiveMethodResolver(cn.taketoday.expression.spel.support.ReflectiveMethodResolver) MethodExecutor(cn.taketoday.expression.MethodExecutor) ArrayList(java.util.ArrayList) EvaluationContext(cn.taketoday.expression.EvaluationContext) StandardEvaluationContext(cn.taketoday.expression.spel.support.StandardEvaluationContext) Test(org.junit.jupiter.api.Test)

Example 15 with TypeDescriptor

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

the class SpelReproTests method projectionTypeDescriptors_2.

@Test
void projectionTypeDescriptors_2() {
    StandardEvaluationContext context = new StandardEvaluationContext(new C());
    SpelExpressionParser parser = new SpelExpressionParser();
    String el1 = "as.![#this.equals('abc')]";
    SpelExpression exp = parser.parseRaw(el1);
    Object[] value = (Object[]) exp.getValue(context);
    // value is array containing [true,false]
    assertThat(value[0].getClass()).isEqualTo(Boolean.class);
    TypeDescriptor evaluated = exp.getValueTypeDescriptor(context);
    assertThat(evaluated.getElementDescriptor().getType()).isEqualTo(Boolean.class);
}
Also used : StandardEvaluationContext(cn.taketoday.expression.spel.support.StandardEvaluationContext) SpelExpressionParser(cn.taketoday.expression.spel.standard.SpelExpressionParser) TypeDescriptor(cn.taketoday.core.TypeDescriptor) SpelExpression(cn.taketoday.expression.spel.standard.SpelExpression) 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