@Override public boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType) { return ConversionUtils.canConvertElements( sourceType.getElementTypeDescriptor(), targetType.getElementTypeDescriptor(), this.conversionService); }
@Override public boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType) { return ConversionUtils.canConvertElements(sourceType.getElementTypeDescriptor(), targetType.getElementTypeDescriptor(), this.conversionService); }
@Override public boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType) { return (targetType.getElementTypeDescriptor() == null || this.conversionService.canConvert(sourceType, targetType.getElementTypeDescriptor())); }
@Override public boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType) { return ConversionUtils.canConvertElements(sourceType, targetType.getElementTypeDescriptor(), this.conversionService); }
@Override public void setValue(@Nullable Object newValue) { TypeDescriptor elementType = this.typeDescriptor.getElementTypeDescriptor(); Assert.state(elementType != null, "No element type"); setArrayElement(this.typeConverter, this.array, this.index, newValue, elementType.getType()); }
@Override public boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType) { return ConversionUtils.canConvertElements(sourceType, targetType.getElementTypeDescriptor(), this.conversionService); }
@Nullable private Object convertFromStream(@Nullable Stream<?> source, TypeDescriptor streamType, TypeDescriptor targetType) { List<Object> content = (source != null ? source.collect(Collectors.<Object>toList()) : Collections.emptyList()); TypeDescriptor listType = TypeDescriptor.collection(List.class, streamType.getElementTypeDescriptor()); return this.conversionService.convert(content, listType, targetType); }
private Object convertToStream(@Nullable Object source, TypeDescriptor sourceType, TypeDescriptor streamType) { TypeDescriptor targetCollection = TypeDescriptor.collection(List.class, streamType.getElementTypeDescriptor()); List<?> target = (List<?>) this.conversionService.convert(source, sourceType, targetCollection); if (target == null) { target = Collections.emptyList(); } return target.stream(); }
@Override public boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType) { if (sourceType.isAssignableTo(STREAM_TYPE)) { return matchesFromStream(sourceType.getElementTypeDescriptor(), targetType); } if (targetType.isAssignableTo(STREAM_TYPE)) { return matchesToStream(targetType.getElementTypeDescriptor(), sourceType); } return false; }
@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 createCollectionWithNullElement() throws Exception { TypeDescriptor typeDescriptor = TypeDescriptor.collection(List.class, null); assertThat(typeDescriptor.getElementTypeDescriptor(), nullValue()); }
@Test public void elementTypePreserveContext() throws Exception { TypeDescriptor desc = new TypeDescriptor(getClass().getField("listPreserveContext")); assertEquals(Integer.class, desc.getElementTypeDescriptor().getElementTypeDescriptor().getType()); List<Integer> value = new ArrayList<>(3); desc = desc.elementTypeDescriptor(value); assertEquals(Integer.class, desc.getElementTypeDescriptor().getType()); assertNotNull(desc.getAnnotation(FieldAnnotation.class)); }
@Test public void propertyComplex() throws Exception { Property property = new Property(getClass(), getClass().getMethod("getComplexProperty"), getClass().getMethod("setComplexProperty", Map.class)); TypeDescriptor desc = new TypeDescriptor(property); assertEquals(String.class, desc.getMapKeyTypeDescriptor().getType()); assertEquals(Integer.class, desc.getMapValueTypeDescriptor().getElementTypeDescriptor().getElementTypeDescriptor().getType()); }
@Test public void fieldComplexTypeDescriptor() throws Exception { TypeDescriptor typeDescriptor = new TypeDescriptor(TypeDescriptorTests.class.getDeclaredField("arrayOfListOfString")); assertTrue(typeDescriptor.isArray()); assertEquals(List.class,typeDescriptor.getElementTypeDescriptor().getType()); assertEquals(String.class, typeDescriptor.getElementTypeDescriptor().getElementTypeDescriptor().getType()); assertEquals("java.util.List<java.lang.String>[]",typeDescriptor.toString()); }
@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 elementTypeForCollectionSubclass() throws Exception { @SuppressWarnings("serial") class CustomSet extends HashSet<String> { } assertEquals(TypeDescriptor.valueOf(CustomSet.class).getElementTypeDescriptor(), TypeDescriptor.valueOf(String.class)); assertEquals(TypeDescriptor.forObject(new CustomSet()).getElementTypeDescriptor(), TypeDescriptor.valueOf(String.class)); }
@Test public void fieldMap() throws Exception { TypeDescriptor desc = new TypeDescriptor(TypeDescriptorTests.class.getField("fieldMap")); assertTrue(desc.isMap()); assertEquals(Integer.class, desc.getMapKeyTypeDescriptor().getElementTypeDescriptor().getType()); assertEquals(Long.class, desc.getMapValueTypeDescriptor().getElementTypeDescriptor().getType()); }
@Test public void property() throws Exception { Property property = new Property( getClass(), getClass().getMethod("getProperty"), getClass().getMethod("setProperty", Map.class)); TypeDescriptor desc = new TypeDescriptor(property); assertEquals(Map.class, desc.getType()); assertEquals(Integer.class, desc.getMapKeyTypeDescriptor().getElementTypeDescriptor().getType()); assertEquals(Long.class, desc.getMapValueTypeDescriptor().getElementTypeDescriptor().getType()); assertNotNull(desc.getAnnotation(MethodAnnotation1.class)); assertNotNull(desc.getAnnotation(MethodAnnotation2.class)); assertNotNull(desc.getAnnotation(MethodAnnotation3.class)); }
@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 valueOfArray() throws Exception { TypeDescriptor typeDescriptor = TypeDescriptor.valueOf(int[].class); assertTrue(typeDescriptor.isArray()); assertFalse(typeDescriptor.isCollection()); assertFalse(typeDescriptor.isMap()); assertEquals(Integer.TYPE, typeDescriptor.getElementTypeDescriptor().getType()); }