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

The following examples show how to use org.springframework.core.convert.TypeDescriptor#getElementTypeDescriptor() . 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: CollectionToArrayConverter.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@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;
}
 
Example 2
Source File: DelimitedStringToCollectionConverter.java    From spring-cloud-gray with Apache License 2.0 6 votes vote down vote up
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;
}
 
Example 3
Source File: ObjectToCollectionConverter.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
@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;
}
 
Example 4
Source File: ObjectToCollectionConverter.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@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;
}
 
Example 5
Source File: ObjectToCollectionConverter.java    From java-technology-stack with MIT License 6 votes vote down vote up
@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;
}
 
Example 6
Source File: StringToArrayConverter.java    From java-technology-stack with MIT License 6 votes vote down vote up
@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;
}
 
Example 7
Source File: ArrayToArrayConverter.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@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);
}
 
Example 8
Source File: CollectionToCollectionConverter.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example 9
Source File: DelimitedStringToArrayConverter.java    From spring-cloud-gray with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example 10
Source File: ObjectToArrayConverter.java    From java-technology-stack with MIT License 5 votes vote down vote up
@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;
}
 
Example 11
Source File: ArrayToArrayConverter.java    From java-technology-stack with MIT License 5 votes vote down vote up
@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);
}
 
Example 12
Source File: CollectionToDelimitedStringConverter.java    From spring-cloud-gray with Apache License 2.0 5 votes vote down vote up
@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;
}
 
Example 13
Source File: AbstractNestablePropertyAccessor.java    From spring-analysis-note with MIT License 5 votes vote down vote up
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);
	}
}
 
Example 14
Source File: AbstractNestablePropertyAccessor.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
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);
	}
}
 
Example 15
Source File: CollectionToCollectionConverter.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@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);
}
 
Example 16
Source File: ObjectToArrayConverter.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@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;
}
 
Example 17
Source File: StringToCollectionConverter.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
@Override
public boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType) {
	return (targetType.getElementTypeDescriptor() == null ||
			this.conversionService.canConvert(sourceType, targetType.getElementTypeDescriptor()));
}
 
Example 18
Source File: StringToCollectionConverter.java    From java-technology-stack with MIT License 4 votes vote down vote up
@Override
public boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType) {
	return (targetType.getElementTypeDescriptor() == null ||
			this.conversionService.canConvert(sourceType, targetType.getElementTypeDescriptor()));
}
 
Example 19
Source File: DelimitedStringToCollectionConverter.java    From spring-cloud-gray with Apache License 2.0 4 votes vote down vote up
@Override
public boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType) {
    return targetType.getElementTypeDescriptor() == null || this.conversionService
            .canConvert(sourceType, targetType.getElementTypeDescriptor());
}
 
Example 20
Source File: DelimitedStringToArrayConverter.java    From spring-cloud-gray with Apache License 2.0 4 votes vote down vote up
@Override
public boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType) {
    return targetType.getElementTypeDescriptor() == null || this.conversionService
            .canConvert(sourceType, targetType.getElementTypeDescriptor());
}