org.springframework.core.convert.TypeDescriptor#forObject ( )源码实例Demo

下面列出了org.springframework.core.convert.TypeDescriptor#forObject ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

@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());
}
 
@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));
}
 
@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));
}
 
@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));
}
 
@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));
}
 
@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));
}
 
@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));
}
 
@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));
}
 
@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));
}
 
@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
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());
}
 
@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"))));
}
 
@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 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 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 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"))));
}
 
@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());
}
 
@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));
}
 
源代码19 项目: salespoint   文件: SalespointIdentifierConverter.java
@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);
	}
}
 
源代码20 项目: lams   文件: TypedValue.java
public TypeDescriptor getTypeDescriptor() {
	if (this.typeDescriptor == null && this.value != null) {
		this.typeDescriptor = TypeDescriptor.forObject(this.value);
	}
	return this.typeDescriptor;
}