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

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

@Override
@Nullable
public Object convert(@Nullable Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
	if (source == null) {
		return null;
	}
	Collection<?> sourceCollection = (Collection<?>) source;
	TypeDescriptor targetElementType = targetType.getElementTypeDescriptor();
	Assert.state(targetElementType != null, "No target element type");
	Object array = Array.newInstance(targetElementType.getType(), sourceCollection.size());
	int i = 0;
	for (Object sourceElement : sourceCollection) {
		Object targetElement = this.conversionService.convert(sourceElement,
				sourceType.elementTypeDescriptor(sourceElement), targetElementType);
		Array.set(array, i++, targetElement);
	}
	return array;
}
 
@Override
@Nullable
public Object convert(@Nullable Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
	if (source == null) {
		return null;
	}

	TypeDescriptor elementDesc = targetType.getElementTypeDescriptor();
	Collection<Object> target = CollectionFactory.createCollection(targetType.getType(),
			(elementDesc != null ? elementDesc.getType() : null), 1);

	if (elementDesc == null || elementDesc.isCollection()) {
		target.add(source);
	}
	else {
		Object singleElement = this.conversionService.convert(source, sourceType, elementDesc);
		target.add(singleElement);
	}
	return target;
}
 
@Override
@Nullable
public Object convert(@Nullable Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
	if (source == null) {
		return null;
	}
	String string = (String) source;
	String[] fields = StringUtils.commaDelimitedListToStringArray(string);
	TypeDescriptor targetElementType = targetType.getElementTypeDescriptor();
	Assert.state(targetElementType != null, "No target element type");
	Object target = Array.newInstance(targetElementType.getType(), fields.length);
	for (int i = 0; i < fields.length; i++) {
		String sourceElement = fields[i];
		Object targetElement = this.conversionService.convert(sourceElement.trim(), sourceType, targetElementType);
		Array.set(target, i, targetElement);
	}
	return target;
}
 
@Override
@Nullable
public Object convert(@Nullable Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
	if (source == null) {
		return null;
	}

	TypeDescriptor elementDesc = targetType.getElementTypeDescriptor();
	Collection<Object> target = CollectionFactory.createCollection(targetType.getType(),
			(elementDesc != null ? elementDesc.getType() : null), 1);

	if (elementDesc == null || elementDesc.isCollection()) {
		target.add(source);
	}
	else {
		Object singleElement = this.conversionService.convert(source, sourceType, elementDesc);
		target.add(singleElement);
	}
	return target;
}
 
源代码5 项目: lams   文件: ObjectToCollectionConverter.java
@Override
public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
	if (source == null) {
		return null;
	}

	TypeDescriptor elementDesc = targetType.getElementTypeDescriptor();
	Collection<Object> target = CollectionFactory.createCollection(targetType.getType(),
			(elementDesc != null ? elementDesc.getType() : null), 1);

	if (elementDesc == null || elementDesc.isCollection()) {
		target.add(source);
	}
	else {
		Object singleElement = this.conversionService.convert(source, sourceType, elementDesc);
		target.add(singleElement);
	}
	return target;
}
 
private Object convert(String source, TypeDescriptor sourceType,
                       TypeDescriptor targetType) {
    Delimiter delimiter = targetType.getAnnotation(Delimiter.class);
    String[] elements = getElements(source,
            (delimiter != null) ? delimiter.value() : ",");
    TypeDescriptor elementDescriptor = targetType.getElementTypeDescriptor();
    Collection<Object> target = createCollection(targetType, elementDescriptor,
            elements.length);
    Stream<Object> stream = Arrays.stream(elements).map(String::trim);
    if (elementDescriptor != null) {
        stream = stream.map((element) -> this.conversionService.convert(element,
                sourceType, elementDescriptor));
    }
    stream.forEach(target::add);
    return target;
}
 
@Override
@Nullable
public Object convert(@Nullable Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
	if (this.conversionService instanceof GenericConversionService) {
		TypeDescriptor targetElement = targetType.getElementTypeDescriptor();
		if (targetElement != null &&
				((GenericConversionService) this.conversionService).canBypassConvert(
						sourceType.getElementTypeDescriptor(), targetElement)) {
			return source;
		}
	}
	List<Object> sourceList = Arrays.asList(ObjectUtils.toObjectArray(source));
	return this.helperConverter.convert(sourceList, sourceType, targetType);
}
 
@Override
@Nullable
public Object convert(@Nullable Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
	if (source == null) {
		return null;
	}
	TypeDescriptor targetElementType = targetType.getElementTypeDescriptor();
	Assert.state(targetElementType != null, "No target element type");
	Object target = Array.newInstance(targetElementType.getType(), 1);
	Object targetElement = this.conversionService.convert(source, sourceType, targetElementType);
	Array.set(target, 0, targetElement);
	return target;
}
 
@Override
@Nullable
public Object convert(@Nullable Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
	if (source == null) {
		return null;
	}
	Collection<?> sourceCollection = (Collection<?>) source;

	// Shortcut if possible...
	boolean copyRequired = !targetType.getType().isInstance(source);
	if (!copyRequired && sourceCollection.isEmpty()) {
		return source;
	}
	TypeDescriptor elementDesc = targetType.getElementTypeDescriptor();
	if (elementDesc == null && !copyRequired) {
		return source;
	}

	// At this point, we need a collection copy in any case, even if just for finding out about element copies...
	Collection<Object> target = CollectionFactory.createCollection(targetType.getType(),
			(elementDesc != null ? elementDesc.getType() : null), sourceCollection.size());

	if (elementDesc == null) {
		target.addAll(sourceCollection);
	}
	else {
		for (Object sourceElement : sourceCollection) {
			Object targetElement = this.conversionService.convert(sourceElement,
					sourceType.elementTypeDescriptor(sourceElement), elementDesc);
			target.add(targetElement);
			if (sourceElement != targetElement) {
				copyRequired = true;
			}
		}
	}

	return (copyRequired ? target : source);
}
 
private Object newValue(Class<?> type, TypeDescriptor desc, String name) {
	try {
		if (type.isArray()) {
			Class<?> componentType = type.getComponentType();
			// TODO - only handles 2-dimensional arrays
			if (componentType.isArray()) {
				Object array = Array.newInstance(componentType, 1);
				Array.set(array, 0, Array.newInstance(componentType.getComponentType(), 0));
				return array;
			}
			else {
				return Array.newInstance(componentType, 0);
			}
		}
		else if (Collection.class.isAssignableFrom(type)) {
			TypeDescriptor elementDesc = (desc != null ? desc.getElementTypeDescriptor() : null);
			return CollectionFactory.createCollection(type, (elementDesc != null ? elementDesc.getType() : null), 16);
		}
		else if (Map.class.isAssignableFrom(type)) {
			TypeDescriptor keyDesc = (desc != null ? desc.getMapKeyTypeDescriptor() : null);
			return CollectionFactory.createMap(type, (keyDesc != null ? keyDesc.getType() : null), 16);
		}
		else {
			return BeanUtils.instantiate(type);
		}
	}
	catch (Exception ex) {
		// TODO: Root cause exception context is lost here; just exception message preserved.
		// Should we throw another exception type that preserves context instead?
		throw new NullValueInNestedPathException(getRootClass(), this.nestedPath + name,
				"Could not instantiate property type [" + type.getName() + "] to auto-grow nested property path: " + ex);
	}
}
 
private Object newValue(Class<?> type, @Nullable TypeDescriptor desc, String name) {
	try {
		if (type.isArray()) {
			Class<?> componentType = type.getComponentType();
			// TODO - only handles 2-dimensional arrays
			if (componentType.isArray()) {
				Object array = Array.newInstance(componentType, 1);
				Array.set(array, 0, Array.newInstance(componentType.getComponentType(), 0));
				return array;
			}
			else {
				return Array.newInstance(componentType, 0);
			}
		}
		else if (Collection.class.isAssignableFrom(type)) {
			TypeDescriptor elementDesc = (desc != null ? desc.getElementTypeDescriptor() : null);
			return CollectionFactory.createCollection(type, (elementDesc != null ? elementDesc.getType() : null), 16);
		}
		else if (Map.class.isAssignableFrom(type)) {
			TypeDescriptor keyDesc = (desc != null ? desc.getMapKeyTypeDescriptor() : null);
			return CollectionFactory.createMap(type, (keyDesc != null ? keyDesc.getType() : null), 16);
		}
		else {
			Constructor<?> ctor = type.getDeclaredConstructor();
			if (Modifier.isPrivate(ctor.getModifiers())) {
				throw new IllegalAccessException("Auto-growing not allowed with private constructor: " + ctor);
			}
			return BeanUtils.instantiateClass(ctor);
		}
	}
	catch (Throwable ex) {
		throw new NullValueInNestedPathException(getRootClass(), this.nestedPath + name,
				"Could not instantiate property type [" + type.getName() + "] to auto-grow nested property path", ex);
	}
}
 
@Override
public boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType) {
    TypeDescriptor sourceElementType = sourceType.getElementTypeDescriptor();
    if (targetType == null || sourceElementType == null) {
        return true;
    }
    if (this.conversionService.canConvert(sourceElementType, targetType)
            || sourceElementType.getType().isAssignableFrom(targetType.getType())) {
        return true;
    }
    return false;
}
 
@Override
@Nullable
public Object convert(@Nullable Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
	if (this.conversionService instanceof GenericConversionService) {
		TypeDescriptor targetElement = targetType.getElementTypeDescriptor();
		if (targetElement != null &&
				((GenericConversionService) this.conversionService).canBypassConvert(
						sourceType.getElementTypeDescriptor(), targetElement)) {
			return source;
		}
	}
	List<Object> sourceList = Arrays.asList(ObjectUtils.toObjectArray(source));
	return this.helperConverter.convert(sourceList, sourceType, targetType);
}
 
@Override
@Nullable
public Object convert(@Nullable Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
	if (source == null) {
		return null;
	}
	TypeDescriptor targetElementType = targetType.getElementTypeDescriptor();
	Assert.state(targetElementType != null, "No target element type");
	Object target = Array.newInstance(targetElementType.getType(), 1);
	Object targetElement = this.conversionService.convert(source, sourceType, targetElementType);
	Array.set(target, 0, targetElement);
	return target;
}
 
private Object convert(String source, TypeDescriptor sourceType,
                       TypeDescriptor targetType) {
    Delimiter delimiter = targetType.getAnnotation(Delimiter.class);
    String[] elements = getElements(source,
            (delimiter != null) ? delimiter.value() : ",");
    TypeDescriptor elementDescriptor = targetType.getElementTypeDescriptor();
    Object target = Array.newInstance(elementDescriptor.getType(), elements.length);
    for (int i = 0; i < elements.length; i++) {
        String sourceElement = elements[i];
        Object targetElement = this.conversionService.convert(sourceElement.trim(),
                sourceType, elementDescriptor);
        Array.set(target, i, targetElement);
    }
    return target;
}
 
@Override
public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
	if (source == null) {
		return null;
	}
	Collection<?> sourceCollection = (Collection<?>) source;

	// Shortcut if possible...
	boolean copyRequired = !targetType.getType().isInstance(source);
	if (!copyRequired && sourceCollection.isEmpty()) {
		return source;
	}
	TypeDescriptor elementDesc = targetType.getElementTypeDescriptor();
	if (elementDesc == null && !copyRequired) {
		return source;
	}

	// At this point, we need a collection copy in any case, even if just for finding out about element copies...
	Collection<Object> target = CollectionFactory.createCollection(targetType.getType(),
			(elementDesc != null ? elementDesc.getType() : null), sourceCollection.size());

	if (elementDesc == null) {
		target.addAll(sourceCollection);
	}
	else {
		for (Object sourceElement : sourceCollection) {
			Object targetElement = this.conversionService.convert(sourceElement,
					sourceType.elementTypeDescriptor(sourceElement), elementDesc);
			target.add(targetElement);
			if (sourceElement != targetElement) {
				copyRequired = true;
			}
		}
	}

	return (copyRequired ? target : source);
}
 
源代码17 项目: lams   文件: StringToCollectionConverter.java
@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 (targetType.getElementTypeDescriptor() == null ||
			this.conversionService.canConvert(sourceType, targetType.getElementTypeDescriptor()));
}
 
@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 targetType.getElementTypeDescriptor() == null || this.conversionService
            .canConvert(sourceType, targetType.getElementTypeDescriptor());
}