private List<TypeDescriptor> getArgumentTypes(Object... arguments) { List<TypeDescriptor> descriptors = new ArrayList<>(arguments.length); for (Object argument : arguments) { descriptors.add(TypeDescriptor.forObject(argument)); } return Collections.unmodifiableList(descriptors); }
@Override @SuppressWarnings("unchecked") @Nullable public <T> T convert(@Nullable Object source, Class<T> targetType) { Assert.notNull(targetType, "Target type to convert to cannot be null"); return (T) convert(source, TypeDescriptor.forObject(source), TypeDescriptor.valueOf(targetType)); }
@SuppressWarnings("unchecked") private <T> T convertValue(TypeConverter converter, @Nullable Object value, Class<T> targetType) { T result = (T) converter.convertValue( value, TypeDescriptor.forObject(value), TypeDescriptor.valueOf(targetType)); if (result == null) { throw new IllegalStateException("Null conversion result for index [" + value + "]"); } return result; }
@Override public void setValue(@Nullable Object newValue) { if (this.mapEntryDescriptor.getMapValueTypeDescriptor() != null) { newValue = this.typeConverter.convertValue(newValue, TypeDescriptor.forObject(newValue), this.mapEntryDescriptor.getMapValueTypeDescriptor()); } this.map.put(this.key, newValue); }
@Test public void serializable() throws Exception { TypeDescriptor typeDescriptor = TypeDescriptor.forObject(""); ByteArrayOutputStream out = new ByteArrayOutputStream(); ObjectOutputStream outputStream = new ObjectOutputStream(out); outputStream.writeObject(typeDescriptor); ObjectInputStream inputStream = new ObjectInputStream(new ByteArrayInputStream( out.toByteArray())); TypeDescriptor readObject = (TypeDescriptor) inputStream.readObject(); assertThat(readObject, equalTo(typeDescriptor)); }
private void populateReferenceTypeArray(ExpressionState state, Object newArray, TypeConverter typeConverter, InlineList initializer, Class<?> componentType) { TypeDescriptor toTypeDescriptor = TypeDescriptor.valueOf(componentType); Object[] newObjectArray = (Object[]) newArray; for (int i = 0; i < newObjectArray.length; i++) { SpelNode elementNode = initializer.getChild(i); Object arrayEntry = elementNode.getValue(state); newObjectArray[i] = typeConverter.convertValue(arrayEntry, TypeDescriptor.forObject(arrayEntry), toTypeDescriptor); } }
@Test public void mixedInNulls() throws Exception { List<Resource> resources = new ArrayList<>(); resources.add(new ClassPathResource("test")); resources.add(null); resources.add(new FileSystemResource("test")); resources.add(new TestResource()); TypeDescriptor sourceType = TypeDescriptor.forObject(resources); assertSame(resources, conversionService.convert(resources, sourceType, new TypeDescriptor(getClass().getField("resources")))); }
@Test(expected = ConversionFailedException.class) public void nothingInCommon() throws Exception { List<Object> resources = new ArrayList<>(); resources.add(new ClassPathResource("test")); resources.add(3); TypeDescriptor sourceType = TypeDescriptor.forObject(resources); assertEquals(resources, conversionService.convert(resources, sourceType, new TypeDescriptor(getClass().getField("resources")))); }
@Test public void convertMapToMap() throws Exception { Map<String, String> foo = new HashMap<>(); foo.put("1", "BAR"); foo.put("2", "BAZ"); @SuppressWarnings("unchecked") Map<Integer, FooEnum> map = (Map<Integer, FooEnum>) conversionService.convert(foo, TypeDescriptor.forObject(foo), new TypeDescriptor(getClass().getField("genericMap"))); assertEquals(FooEnum.BAR, map.get(1)); assertEquals(FooEnum.BAZ, map.get(2)); }
@Test public void testWildcardMap() throws Exception { Map<String, String> input = new LinkedHashMap<>(); input.put("key", "value"); Object converted = conversionService.convert(input, TypeDescriptor.forObject(input), new TypeDescriptor(getClass().getField("wildcardMap"))); assertEquals(input, converted); }
@Test public void listToCollectionNoCopyRequired() throws NoSuchFieldException { List<?> input = new ArrayList<>(Arrays.asList("foo", "bar")); assertSame(input, conversionService.convert(input, TypeDescriptor.forObject(input), new TypeDescriptor(getClass().getField("wildcardCollection")))); }
@SuppressWarnings("rawtypes") private void testCollectionConversionToArrayList(Collection<String> aSource) { Object myConverted = (new CollectionToCollectionConverter(new GenericConversionService())).convert( aSource, TypeDescriptor.forObject(aSource), TypeDescriptor.forObject(new ArrayList())); assertTrue(myConverted instanceof ArrayList<?>); assertEquals(aSource.size(), ((ArrayList<?>) myConverted).size()); }
@Test public void testStringToEnumSet() throws Exception { conversionService.addConverterFactory(new StringToEnumConverterFactory()); List<String> list = new ArrayList<>(); list.add("A"); list.add("C"); assertEquals(EnumSet.of(MyEnum.A, MyEnum.C), conversionService.convert(list, TypeDescriptor.forObject(list), new TypeDescriptor(getClass().getField("enumSet")))); }
@Test public void testStandardTypeConverter() throws EvaluationException { TypeConverter tc = new StandardTypeConverter(); tc.convertValue(3, TypeDescriptor.forObject(3), TypeDescriptor.valueOf(Double.class)); }
@Test public void emptyMapDifferentTargetImplType() throws Exception { Map<String, String> map = new HashMap<>(); TypeDescriptor sourceType = TypeDescriptor.forObject(map); TypeDescriptor targetType = new TypeDescriptor(getClass().getField("emptyMapDifferentTarget")); assertTrue(conversionService.canConvert(sourceType, targetType)); @SuppressWarnings("unchecked") LinkedHashMap<String, String> result = (LinkedHashMap<String, String>) conversionService.convert(map, sourceType, targetType); assertEquals(map, result); assertEquals(LinkedHashMap.class, result.getClass()); }
@Test public void emptyListToList() throws Exception { conversionService.addConverter(new CollectionToCollectionConverter(conversionService)); conversionService.addConverterFactory(new StringToNumberConverterFactory()); List<String> list = new ArrayList<>(); TypeDescriptor sourceType = TypeDescriptor.forObject(list); TypeDescriptor targetType = new TypeDescriptor(getClass().getField("emptyListTarget")); assertTrue(conversionService.canConvert(sourceType, targetType)); assertEquals(list, conversionService.convert(list, sourceType, targetType)); }
@Test public void emptyListToArray() { conversionService.addConverter(new CollectionToArrayConverter(conversionService)); conversionService.addConverterFactory(new StringToNumberConverterFactory()); List<String> list = new ArrayList<>(); TypeDescriptor sourceType = TypeDescriptor.forObject(list); TypeDescriptor targetType = TypeDescriptor.valueOf(String[].class); assertTrue(conversionService.canConvert(sourceType, targetType)); assertEquals(0, ((String[]) conversionService.convert(list, sourceType, targetType)).length); }
@Test public void elementTypeForMapSubclass() throws Exception { @SuppressWarnings("serial") class CustomMap extends HashMap<String, Integer> { } assertEquals(TypeDescriptor.valueOf(CustomMap.class).getMapKeyTypeDescriptor(), TypeDescriptor.valueOf(String.class)); assertEquals(TypeDescriptor.valueOf(CustomMap.class).getMapValueTypeDescriptor(), TypeDescriptor.valueOf(Integer.class)); assertEquals(TypeDescriptor.forObject(new CustomMap()).getMapKeyTypeDescriptor(), TypeDescriptor.valueOf(String.class)); assertEquals(TypeDescriptor.forObject(new CustomMap()).getMapValueTypeDescriptor(), TypeDescriptor.valueOf(Integer.class)); }
@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)); }