/** * Is this type a primitive type? */ public boolean isPrimitive() { return getType().isPrimitive(); }
/** * Is this type a {@link Collection} type? */ public boolean isCollection() { return Collection.class.isAssignableFrom(getType()); }
/** * Is this type an array type? */ public boolean isArray() { return getType().isArray(); }
/** * Return the name of this type: the fully qualified class name. */ public String getName() { return ClassUtils.getQualifiedName(getType()); }
@Override @Nullable public Class<?> getValueType(EvaluationContext context) throws EvaluationException { Assert.notNull(context, "EvaluationContext is required"); ExpressionState expressionState = new ExpressionState(context, this.configuration); TypeDescriptor typeDescriptor = this.ast.getValueInternal(expressionState).getTypeDescriptor(); return (typeDescriptor != null ? typeDescriptor.getType() : null); }
@Override public boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType) { Method finder = getFinder(targetType.getType()); return (finder != null && this.conversionService.canConvert(sourceType, TypeDescriptor.valueOf(finder.getParameterTypes()[0]))); }
@Override @Nullable public Class<?> getValueType(EvaluationContext context, Object rootObject) throws EvaluationException { ExpressionState expressionState = new ExpressionState(context, toTypedValue(rootObject), this.configuration); TypeDescriptor typeDescriptor = this.ast.getValueInternal(expressionState).getTypeDescriptor(); return (typeDescriptor != null ? typeDescriptor.getType() : null); }
@Test public void propertyGenericTypeList() throws Exception { GenericType<Integer> genericBean = new IntegerType(); Property property = new Property(getClass(), genericBean.getClass().getMethod("getListProperty"), genericBean.getClass().getMethod("setListProperty", List.class)); TypeDescriptor desc = new TypeDescriptor(property); assertEquals(List.class, desc.getType()); assertEquals(Integer.class, desc.getElementTypeDescriptor().getType()); }
@Test public void forObject() { TypeDescriptor desc = TypeDescriptor.forObject("3"); assertEquals(String.class, desc.getType()); }
@Test public void narrow() { TypeDescriptor desc = TypeDescriptor.valueOf(Number.class); Integer value = Integer.valueOf(3); desc = desc.narrow(value); assertEquals(Integer.class, desc.getType()); }
@Test public void passDownGeneric() throws Exception { TypeDescriptor td = new TypeDescriptor(getClass().getField("passDownGeneric")); assertEquals(List.class, td.getElementTypeDescriptor().getType()); assertEquals(Set.class, td.getElementTypeDescriptor().getElementTypeDescriptor().getType()); assertEquals(Integer.class, td.getElementTypeDescriptor().getElementTypeDescriptor().getElementTypeDescriptor().getType()); }
@Test public void mapValueType() { TypeDescriptor desc = TypeDescriptor.valueOf(Map.class); Integer value = Integer.valueOf(3); desc = desc.getMapValueTypeDescriptor(value); assertEquals(Integer.class, desc.getType()); }
@Test public void conditionalConversionForAllTypes() { MyConditionalGenericConverter converter = new MyConditionalGenericConverter(); conversionService.addConverter(converter); assertEquals((Integer) 3, conversionService.convert(3, Integer.class)); assertThat(converter.getSourceTypes().size(), greaterThan(2)); assertTrue(converter.getSourceTypes().stream().allMatch(td -> Integer.class.equals(td.getType()))); }
@Test public void mapKeyTypePreserveContext() throws Exception { TypeDescriptor desc = new TypeDescriptor(getClass().getField("mapPreserveContext")); assertEquals(Integer.class, desc.getMapKeyTypeDescriptor().getElementTypeDescriptor().getType()); List<Integer> value = new ArrayList<>(3); desc = desc.getMapKeyTypeDescriptor(value); assertEquals(Integer.class, desc.getElementTypeDescriptor().getType()); assertNotNull(desc.getAnnotation(FieldAnnotation.class)); }
@Test public void mapValueTypePreserveContext() throws Exception { TypeDescriptor desc = new TypeDescriptor(getClass().getField("mapPreserveContext")); assertEquals(Integer.class, desc.getMapValueTypeDescriptor().getElementTypeDescriptor().getType()); List<Integer> value = new ArrayList<>(3); desc = desc.getMapValueTypeDescriptor(value); assertEquals(Integer.class, desc.getElementTypeDescriptor().getType()); assertNotNull(desc.getAnnotation(FieldAnnotation.class)); }
@Test public void testGetValueType() throws Exception { LiteralExpression lEx = new LiteralExpression("somevalue"); assertEquals(String.class, lEx.getValueType()); assertEquals(String.class, lEx.getValueType(new StandardEvaluationContext())); assertEquals(String.class, lEx.getValueType(new Rooty())); assertEquals(String.class, lEx.getValueType(new StandardEvaluationContext(), new Rooty())); assertEquals(String.class, lEx.getValueTypeDescriptor().getType()); assertEquals(String.class, lEx.getValueTypeDescriptor(new StandardEvaluationContext()).getType()); assertEquals(String.class, lEx.getValueTypeDescriptor(new Rooty()).getType()); assertEquals(String.class, lEx.getValueTypeDescriptor(new StandardEvaluationContext(), new Rooty()).getType()); }
@Test public 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] assertEquals(Boolean.class, value[0].getClass()); TypeDescriptor evaluated = exp.getValueTypeDescriptor(context); assertEquals(Boolean.class, evaluated.getElementTypeDescriptor().getType()); }
@Test public void createMapArray() throws Exception { TypeDescriptor mapType = TypeDescriptor.map( LinkedHashMap.class, TypeDescriptor.valueOf(String.class), TypeDescriptor.valueOf(Integer.class)); TypeDescriptor arrayType = TypeDescriptor.array(mapType); assertEquals(arrayType.getType(), LinkedHashMap[].class); assertEquals(arrayType.getElementTypeDescriptor(), mapType); }
@Test public void parameterPrimitive() throws Exception { TypeDescriptor desc = new TypeDescriptor(new MethodParameter(getClass().getMethod("testParameterPrimitive", int.class), 0)); assertEquals(int.class, desc.getType()); assertEquals(Integer.class, desc.getObjectType()); assertEquals("int", desc.getName()); assertEquals("int", desc.toString()); assertTrue(desc.isPrimitive()); assertEquals(0, desc.getAnnotations().length); assertFalse(desc.isCollection()); assertFalse(desc.isMap()); }