Java Code Examples for org.springframework.core.convert.TypeDescriptor#forObject()

The following examples show how to use org.springframework.core.convert.TypeDescriptor#forObject() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CollectionToCollectionConverterTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void scalarList() throws Exception {
	List<String> list = new ArrayList<>();
	list.add("9");
	list.add("37");
	TypeDescriptor sourceType = TypeDescriptor.forObject(list);
	TypeDescriptor targetType = new TypeDescriptor(getClass().getField("scalarListTarget"));
	assertTrue(conversionService.canConvert(sourceType, targetType));
	try {
		conversionService.convert(list, sourceType, targetType);
	}
	catch (ConversionFailedException ex) {
		assertTrue(ex.getCause() instanceof ConverterNotFoundException);
	}
	conversionService.addConverterFactory(new StringToNumberConverterFactory());
	assertTrue(conversionService.canConvert(sourceType, targetType));
	@SuppressWarnings("unchecked")
	List<Integer> result = (List<Integer>) conversionService.convert(list, sourceType, targetType);
	assertFalse(list.equals(result));
	assertEquals(9, result.get(0).intValue());
	assertEquals(37, result.get(1).intValue());
}
 
Example 2
Source File: CollectionToCollectionConverterTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
@SuppressWarnings("unchecked")
public void stringToCollection() throws Exception {
	List<List<String>> list = new ArrayList<List<String>>();
	list.add(Arrays.asList("9,12"));
	list.add(Arrays.asList("37,23"));
	conversionService.addConverterFactory(new StringToNumberConverterFactory());
	conversionService.addConverter(new StringToCollectionConverter(conversionService));
	conversionService.addConverter(new ObjectToCollectionConverter(conversionService));
	conversionService.addConverter(new CollectionToObjectConverter(conversionService));
	TypeDescriptor sourceType = TypeDescriptor.forObject(list);
	TypeDescriptor targetType = new TypeDescriptor(getClass().getField("objectToCollection"));
	assertTrue(conversionService.canConvert(sourceType, targetType));
	List<List<List<Integer>>> result = (List<List<List<Integer>>>) conversionService.convert(list, sourceType, targetType);
	assertEquals((Integer) 9, result.get(0).get(0).get(0));
	assertEquals((Integer) 12, result.get(0).get(0).get(1));
	assertEquals((Integer) 37, result.get(1).get(0).get(0));
	assertEquals((Integer) 23, result.get(1).get(0).get(1));
}
 
Example 3
Source File: CollectionToCollectionConverterTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void scalarList() throws Exception {
	List<String> list = new ArrayList<String>();
	list.add("9");
	list.add("37");
	TypeDescriptor sourceType = TypeDescriptor.forObject(list);
	TypeDescriptor targetType = new TypeDescriptor(getClass().getField("scalarListTarget"));
	assertTrue(conversionService.canConvert(sourceType, targetType));
	try {
		conversionService.convert(list, sourceType, targetType);
	}
	catch (ConversionFailedException ex) {
		assertTrue(ex.getCause() instanceof ConverterNotFoundException);
	}
	conversionService.addConverterFactory(new StringToNumberConverterFactory());
	assertTrue(conversionService.canConvert(sourceType, targetType));
	@SuppressWarnings("unchecked")
	List<String> result = (List<String>) conversionService.convert(list, sourceType, targetType);
	assertFalse(list.equals(result));
	assertEquals(9, result.get(0));
	assertEquals(37, result.get(1));
}
 
Example 4
Source File: MapToMapConverterTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void scalarMap() throws Exception {
	Map<String, String> map = new HashMap<>();
	map.put("1", "9");
	map.put("2", "37");
	TypeDescriptor sourceType = TypeDescriptor.forObject(map);
	TypeDescriptor targetType = new TypeDescriptor(getClass().getField("scalarMapTarget"));

	assertTrue(conversionService.canConvert(sourceType, targetType));
	try {
		conversionService.convert(map, sourceType, targetType);
	}
	catch (ConversionFailedException ex) {
		assertTrue(ex.getCause() instanceof ConverterNotFoundException);
	}

	conversionService.addConverterFactory(new StringToNumberConverterFactory());
	assertTrue(conversionService.canConvert(sourceType, targetType));
	@SuppressWarnings("unchecked")
	Map<Integer, Integer> result = (Map<Integer, Integer>) conversionService.convert(map, sourceType, targetType);
	assertFalse(map.equals(result));
	assertEquals((Integer) 9, result.get(1));
	assertEquals((Integer) 37, result.get(2));
}
 
Example 5
Source File: MapToMapConverterTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void collectionMap() throws Exception {
	Map<String, List<String>> map = new HashMap<String, List<String>>();
	map.put("1", Arrays.asList("9", "12"));
	map.put("2", Arrays.asList("37", "23"));
	TypeDescriptor sourceType = TypeDescriptor.forObject(map);
	TypeDescriptor targetType = new TypeDescriptor(getClass().getField("collectionMapTarget"));
	assertTrue(conversionService.canConvert(sourceType, targetType));
	try {
		conversionService.convert(map, sourceType, targetType);
	} catch (ConversionFailedException e) {
		assertTrue(e.getCause() instanceof ConverterNotFoundException);
	}
	conversionService.addConverter(new CollectionToCollectionConverter(conversionService));
	conversionService.addConverterFactory(new StringToNumberConverterFactory());
	assertTrue(conversionService.canConvert(sourceType, targetType));
	@SuppressWarnings("unchecked")
	Map<Integer, List<Integer>> result = (Map<Integer, List<Integer>>) conversionService.convert(map, sourceType, targetType);
	assertFalse(map.equals(result));
	assertEquals(Arrays.asList(9, 12), result.get(1));
	assertEquals(Arrays.asList(37, 23), result.get(2));
}
 
Example 6
Source File: CollectionToCollectionConverterTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
@SuppressWarnings("unchecked")
public void stringToCollection() throws Exception {
	List<List<String>> list = new ArrayList<>();
	list.add(Arrays.asList("9,12"));
	list.add(Arrays.asList("37,23"));
	conversionService.addConverterFactory(new StringToNumberConverterFactory());
	conversionService.addConverter(new StringToCollectionConverter(conversionService));
	conversionService.addConverter(new ObjectToCollectionConverter(conversionService));
	conversionService.addConverter(new CollectionToObjectConverter(conversionService));
	TypeDescriptor sourceType = TypeDescriptor.forObject(list);
	TypeDescriptor targetType = new TypeDescriptor(getClass().getField("objectToCollection"));
	assertTrue(conversionService.canConvert(sourceType, targetType));
	List<List<List<Integer>>> result = (List<List<List<Integer>>>) conversionService.convert(list, sourceType, targetType);
	assertEquals((Integer) 9, result.get(0).get(0).get(0));
	assertEquals((Integer) 12, result.get(0).get(0).get(1));
	assertEquals((Integer) 37, result.get(1).get(0).get(0));
	assertEquals((Integer) 23, result.get(1).get(0).get(1));
}
 
Example 7
Source File: CollectionToCollectionConverterTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
@SuppressWarnings("unchecked")
public void stringToCollection() throws Exception {
	List<List<String>> list = new ArrayList<>();
	list.add(Arrays.asList("9,12"));
	list.add(Arrays.asList("37,23"));
	conversionService.addConverterFactory(new StringToNumberConverterFactory());
	conversionService.addConverter(new StringToCollectionConverter(conversionService));
	conversionService.addConverter(new ObjectToCollectionConverter(conversionService));
	conversionService.addConverter(new CollectionToObjectConverter(conversionService));
	TypeDescriptor sourceType = TypeDescriptor.forObject(list);
	TypeDescriptor targetType = new TypeDescriptor(getClass().getField("objectToCollection"));
	assertTrue(conversionService.canConvert(sourceType, targetType));
	List<List<List<Integer>>> result = (List<List<List<Integer>>>) conversionService.convert(list, sourceType, targetType);
	assertEquals((Integer) 9, result.get(0).get(0).get(0));
	assertEquals((Integer) 12, result.get(0).get(0).get(1));
	assertEquals((Integer) 37, result.get(1).get(0).get(0));
	assertEquals((Integer) 23, result.get(1).get(0).get(1));
}
 
Example 8
Source File: CollectionToCollectionConverterTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
@SuppressWarnings("unchecked")
public void objectToCollection() throws Exception {
	List<List<String>> list = new ArrayList<>();
	list.add(Arrays.asList("9", "12"));
	list.add(Arrays.asList("37", "23"));
	conversionService.addConverterFactory(new StringToNumberConverterFactory());
	conversionService.addConverter(new ObjectToCollectionConverter(conversionService));
	conversionService.addConverter(new CollectionToObjectConverter(conversionService));
	TypeDescriptor sourceType = TypeDescriptor.forObject(list);
	TypeDescriptor targetType = new TypeDescriptor(getClass().getField("objectToCollection"));
	assertTrue(conversionService.canConvert(sourceType, targetType));
	List<List<List<Integer>>> result = (List<List<List<Integer>>>) conversionService.convert(list, sourceType, targetType);
	assertEquals((Integer) 9, result.get(0).get(0).get(0));
	assertEquals((Integer) 12, result.get(0).get(1).get(0));
	assertEquals((Integer) 37, result.get(1).get(0).get(0));
	assertEquals((Integer) 23, result.get(1).get(1).get(0));
}
 
Example 9
Source File: MapToMapConverterTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void scalarMap() throws Exception {
	Map<String, String> map = new HashMap<String, String>();
	map.put("1", "9");
	map.put("2", "37");
	TypeDescriptor sourceType = TypeDescriptor.forObject(map);
	TypeDescriptor targetType = new TypeDescriptor(getClass().getField("scalarMapTarget"));
	assertTrue(conversionService.canConvert(sourceType, targetType));
	try {
		conversionService.convert(map, sourceType, targetType);
	} catch (ConversionFailedException e) {
		assertTrue(e.getCause() instanceof ConverterNotFoundException);
	}
	conversionService.addConverterFactory(new StringToNumberConverterFactory());
	assertTrue(conversionService.canConvert(sourceType, targetType));
	@SuppressWarnings("unchecked")
	Map<Integer, Integer> result = (Map<Integer, Integer>) conversionService.convert(map, sourceType, targetType);
	assertFalse(map.equals(result));
	assertEquals((Integer) 9, result.get(1));
	assertEquals((Integer) 37, result.get(2));
}
 
Example 10
Source File: CollectionToCollectionConverterTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void allNulls() throws Exception {
	List<Resource> resources = new ArrayList<>();
	resources.add(null);
	resources.add(null);
	TypeDescriptor sourceType = TypeDescriptor.forObject(resources);
	assertSame(resources, conversionService.convert(resources, sourceType, new TypeDescriptor(getClass().getField("resources"))));
}
 
Example 11
Source File: CollectionToCollectionConverterTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@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"))));
}
 
Example 12
Source File: CollectionToCollectionConverterTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void emptyListToListDifferentTargetType() 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("emptyListDifferentTarget"));
	assertTrue(conversionService.canConvert(sourceType, targetType));
	@SuppressWarnings("unchecked")
	LinkedList<Integer> result = (LinkedList<Integer>) conversionService.convert(list, sourceType, targetType);
	assertEquals(LinkedList.class, result.getClass());
	assertTrue(result.isEmpty());
}
 
Example 13
Source File: MapToMapConverterTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@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());
}
 
Example 14
Source File: GenericConversionServiceTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@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);
}
 
Example 15
Source File: CollectionToCollectionConverterTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@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"))));
}
 
Example 16
Source File: CollectionToCollectionConverterTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void mixedInNulls() throws Exception {
	List<Resource> resources = new ArrayList<Resource>();
	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"))));
}
 
Example 17
Source File: CollectionToCollectionConverterTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void emptyListToListDifferentTargetType() throws Exception {
	conversionService.addConverter(new CollectionToCollectionConverter(conversionService));
	conversionService.addConverterFactory(new StringToNumberConverterFactory());
	List<String> list = new ArrayList<String>();
	TypeDescriptor sourceType = TypeDescriptor.forObject(list);
	TypeDescriptor targetType = new TypeDescriptor(getClass().getField("emptyListDifferentTarget"));
	assertTrue(conversionService.canConvert(sourceType, targetType));
	@SuppressWarnings("unchecked")
	LinkedList<Integer> result = (LinkedList<Integer>) conversionService.convert(list, sourceType, targetType);
	assertEquals(LinkedList.class, result.getClass());
	assertTrue(result.isEmpty());
}
 
Example 18
Source File: CollectionToCollectionConverterTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@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));
}
 
Example 19
Source File: SalespointIdentifierConverter.java    From salespoint with Apache License 2.0 5 votes vote down vote up
@Override
public SalespointIdentifier convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {

	Class<?> targetClass = targetType.getType();

	try {

		Constructor<?> constructor = targetClass.getDeclaredConstructor(String.class);
		return (SalespointIdentifier) BeanUtils.instantiateClass(constructor, source);

	} catch (NoSuchMethodException | SecurityException o_O) {
		throw new ConversionFailedException(TypeDescriptor.forObject(source), TypeDescriptor.valueOf(targetClass), source,
				o_O);
	}
}
 
Example 20
Source File: TypedValue.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
public TypeDescriptor getTypeDescriptor() {
	if (this.typeDescriptor == null && this.value != null) {
		this.typeDescriptor = TypeDescriptor.forObject(this.value);
	}
	return this.typeDescriptor;
}