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

The following examples show how to use org.springframework.core.convert.TypeDescriptor#isAssignableTo() . 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: CollectionToObjectConverter.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;
	}
	if (sourceType.isAssignableTo(targetType)) {
		return source;
	}
	Collection<?> sourceCollection = (Collection<?>) source;
	if (sourceCollection.isEmpty()) {
		return null;
	}
	Object firstElement = sourceCollection.iterator().next();
	return this.conversionService.convert(firstElement, sourceType.elementTypeDescriptor(firstElement), targetType);
}
 
Example 2
Source File: CollectionToObjectConverter.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;
	}
	if (sourceType.isAssignableTo(targetType)) {
		return source;
	}
	Collection<?> sourceCollection = (Collection<?>) source;
	if (sourceCollection.isEmpty()) {
		return null;
	}
	Object firstElement = sourceCollection.iterator().next();
	return this.conversionService.convert(firstElement, sourceType.elementTypeDescriptor(firstElement), targetType);
}
 
Example 3
Source File: SpringMvcContract.java    From spring-cloud-openfeign with Apache License 2.0 6 votes vote down vote up
private static TypeDescriptor createTypeDescriptor(Method method, int paramIndex) {
	Parameter parameter = method.getParameters()[paramIndex];
	MethodParameter methodParameter = MethodParameter.forParameter(parameter);
	TypeDescriptor typeDescriptor = new TypeDescriptor(methodParameter);

	// Feign applies the Param.Expander to each element of an Iterable, so in those
	// cases we need to provide a TypeDescriptor of the element.
	if (typeDescriptor.isAssignableTo(ITERABLE_TYPE_DESCRIPTOR)) {
		TypeDescriptor elementTypeDescriptor = getElementTypeDescriptor(
				typeDescriptor);

		checkState(elementTypeDescriptor != null,
				"Could not resolve element type of Iterable type %s. Not declared?",
				typeDescriptor);

		typeDescriptor = elementTypeDescriptor;
	}
	return typeDescriptor;
}
 
Example 4
Source File: ByteBufferConverter.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
public boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType) {
	boolean byteBufferTarget = targetType.isAssignableTo(BYTE_BUFFER_TYPE);
	if (sourceType.isAssignableTo(BYTE_BUFFER_TYPE)) {
		return (byteBufferTarget || matchesFromByteBuffer(targetType));
	}
	return (byteBufferTarget && matchesToByteBuffer(sourceType));
}
 
Example 5
Source File: ByteBufferConverter.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Override
public boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType) {
	boolean byteBufferTarget = targetType.isAssignableTo(BYTE_BUFFER_TYPE);
	if (sourceType.isAssignableTo(BYTE_BUFFER_TYPE)) {
		return (byteBufferTarget || matchesFromByteBuffer(targetType));
	}
	return (byteBufferTarget && matchesToByteBuffer(sourceType));
}
 
Example 6
Source File: ByteBufferConverter.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) {
	boolean byteBufferTarget = targetType.isAssignableTo(BYTE_BUFFER_TYPE);
	if (source instanceof ByteBuffer) {
		ByteBuffer buffer = (ByteBuffer) source;
		return (byteBufferTarget ? buffer.duplicate() : convertFromByteBuffer(buffer, targetType));
	}
	if (byteBufferTarget) {
		return convertToByteBuffer(source, sourceType);
	}
	// Should not happen
	throw new IllegalStateException("Unexpected source/target types");
}
 
Example 7
Source File: StreamConverter.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 (sourceType.isAssignableTo(STREAM_TYPE)) {
		return convertFromStream((Stream<?>) source, sourceType, targetType);
	}
	if (targetType.isAssignableTo(STREAM_TYPE)) {
		return convertToStream(source, sourceType, targetType);
	}
	// Should not happen
	throw new IllegalStateException("Unexpected source/target types");
}
 
Example 8
Source File: ReflectionHelper.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * Compare argument arrays and return information about whether they match.
 * A supplied type converter and conversionAllowed flag allow for matches to take
 * into account that a type may be transformed into a different type by the converter.
 * @param expectedArgTypes the types the method/constructor is expecting
 * @param suppliedArgTypes the types that are being supplied at the point of invocation
 * @param typeConverter a registered type converter
 * @return a MatchInfo object indicating what kind of match it was,
 * or {@code null} if it was not a match
 */
@Nullable
static ArgumentsMatchInfo compareArguments(
		List<TypeDescriptor> expectedArgTypes, List<TypeDescriptor> suppliedArgTypes, TypeConverter typeConverter) {

	Assert.isTrue(expectedArgTypes.size() == suppliedArgTypes.size(),
			"Expected argument types and supplied argument types should be arrays of same length");

	ArgumentsMatchKind match = ArgumentsMatchKind.EXACT;
	for (int i = 0; i < expectedArgTypes.size() && match != null; i++) {
		TypeDescriptor suppliedArg = suppliedArgTypes.get(i);
		TypeDescriptor expectedArg = expectedArgTypes.get(i);
		// The user may supply null - and that will be ok unless a primitive is expected
		if (suppliedArg == null) {
			if (expectedArg.isPrimitive()) {
				match = null;
			}
		}
		else if (!expectedArg.equals(suppliedArg))  {
			if (suppliedArg.isAssignableTo(expectedArg)) {
				if (match != ArgumentsMatchKind.REQUIRES_CONVERSION) {
					match = ArgumentsMatchKind.CLOSE;
				}
			}
			else if (typeConverter.canConvert(suppliedArg, expectedArg)) {
				match = ArgumentsMatchKind.REQUIRES_CONVERSION;
			}
			else {
				match = null;
			}
		}
	}
	return (match != null ? new ArgumentsMatchInfo(match) : null);
}
 
Example 9
Source File: StreamConverter.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 (sourceType.isAssignableTo(STREAM_TYPE)) {
		return convertFromStream((Stream<?>) source, sourceType, targetType);
	}
	if (targetType.isAssignableTo(STREAM_TYPE)) {
		return convertToStream(source, sourceType, targetType);
	}
	// Should not happen
	throw new IllegalStateException("Unexpected source/target types");
}
 
Example 10
Source File: ArrayToObjectConverter.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;
	}
	if (sourceType.isAssignableTo(targetType)) {
		return source;
	}
	if (Array.getLength(source) == 0) {
		return null;
	}
	Object firstElement = Array.get(source, 0);
	return this.conversionService.convert(firstElement, sourceType.elementTypeDescriptor(firstElement), targetType);
}
 
Example 11
Source File: FormattingConversionService.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
public Object convert(@Nullable Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
	if (!sourceType.isAssignableTo(this.printerObjectType)) {
		source = this.conversionService.convert(source, sourceType, this.printerObjectType);
	}
	if (source == null) {
		return "";
	}
	return this.printer.print(source, LocaleContextHolder.getLocale());
}
 
Example 12
Source File: ReflectionHelper.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Compare argument arrays and return information about whether they match.
 * A supplied type converter and conversionAllowed flag allow for matches to take
 * into account that a type may be transformed into a different type by the converter.
 * @param expectedArgTypes the types the method/constructor is expecting
 * @param suppliedArgTypes the types that are being supplied at the point of invocation
 * @param typeConverter a registered type converter
 * @return a MatchInfo object indicating what kind of match it was,
 * or {@code null} if it was not a match
 */
static ArgumentsMatchInfo compareArguments(
		List<TypeDescriptor> expectedArgTypes, List<TypeDescriptor> suppliedArgTypes, TypeConverter typeConverter) {

	Assert.isTrue(expectedArgTypes.size() == suppliedArgTypes.size(),
			"Expected argument types and supplied argument types should be arrays of same length");

	ArgumentsMatchKind match = ArgumentsMatchKind.EXACT;
	for (int i = 0; i < expectedArgTypes.size() && match != null; i++) {
		TypeDescriptor suppliedArg = suppliedArgTypes.get(i);
		TypeDescriptor expectedArg = expectedArgTypes.get(i);
		if (!expectedArg.equals(suppliedArg)) {
			// The user may supply null - and that will be ok unless a primitive is expected
			if (suppliedArg == null) {
				if (expectedArg.isPrimitive()) {
					match = null;
				}
			}
			else {
				if (suppliedArg.isAssignableTo(expectedArg)) {
					if (match != ArgumentsMatchKind.REQUIRES_CONVERSION) {
						match = ArgumentsMatchKind.CLOSE;
					}
				}
				else if (typeConverter.canConvert(suppliedArg, expectedArg)) {
					match = ArgumentsMatchKind.REQUIRES_CONVERSION;
				}
				else {
					match = null;
				}
			}
		}
	}
	return (match != null ? new ArgumentsMatchInfo(match) : null);
}
 
Example 13
Source File: GenericConversionService.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
private Object handleConverterNotFound(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
	if (source == null) {
		assertNotPrimitiveTargetType(sourceType, targetType);
		return null;
	}
	if (sourceType.isAssignableTo(targetType) && targetType.getObjectType().isInstance(source)) {
		return source;
	}
	throw new ConverterNotFoundException(sourceType, targetType);
}
 
Example 14
Source File: CollectionToObjectConverter.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;
	}
	if (sourceType.isAssignableTo(targetType)) {
		return source;
	}
	Collection<?> sourceCollection = (Collection<?>) source;
	if (sourceCollection.size() == 0) {
		return null;
	}
	Object firstElement = sourceCollection.iterator().next();
	return this.conversionService.convert(firstElement, sourceType.elementTypeDescriptor(firstElement), targetType);
}
 
Example 15
Source File: ByteBufferConverter.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Override
public boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType) {
	boolean byteBufferTarget = targetType.isAssignableTo(BYTE_BUFFER_TYPE);
	if (sourceType.isAssignableTo(BYTE_BUFFER_TYPE)) {
		return (byteBufferTarget || matchesFromByteBuffer(targetType));
	}
	return (byteBufferTarget && matchesToByteBuffer(sourceType));
}
 
Example 16
Source File: StreamConverter.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 (sourceType.isAssignableTo(STREAM_TYPE)) {
		return convertFromStream((Stream<?>) source, sourceType, targetType);
	}
	if (targetType.isAssignableTo(STREAM_TYPE)) {
		return convertToStream(source, sourceType, targetType);
	}
	// Should not happen
	throw new IllegalStateException("Unexpected source/target types");
}
 
Example 17
Source File: GenericConversionService.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Nullable
private Object handleConverterNotFound(
		@Nullable Object source, @Nullable TypeDescriptor sourceType, TypeDescriptor targetType) {

	if (source == null) {
		assertNotPrimitiveTargetType(sourceType, targetType);
		return null;
	}
	if ((sourceType == null || sourceType.isAssignableTo(targetType)) &&
			targetType.getObjectType().isInstance(source)) {
		return source;
	}
	throw new ConverterNotFoundException(sourceType, targetType);
}
 
Example 18
Source File: ByteBufferConverter.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
private boolean matchesToByteBuffer(TypeDescriptor sourceType) {
	return (sourceType.isAssignableTo(BYTE_ARRAY_TYPE) ||
			this.conversionService.canConvert(sourceType, BYTE_ARRAY_TYPE));
}
 
Example 19
Source File: ByteBufferConverter.java    From java-technology-stack with MIT License 4 votes vote down vote up
private boolean matchesFromByteBuffer(TypeDescriptor targetType) {
	return (targetType.isAssignableTo(BYTE_ARRAY_TYPE) ||
			this.conversionService.canConvert(BYTE_ARRAY_TYPE, targetType));
}
 
Example 20
Source File: GenericConversionService.java    From spring-analysis-note with MIT License 2 votes vote down vote up
/**
 * Return the default converter if no converter is found for the given sourceType/targetType pair.
 * <p>Returns a NO_OP Converter if the source type is assignable to the target type.
 * Returns {@code null} otherwise, indicating no suitable converter could be found.
 * @param sourceType the source type to convert from
 * @param targetType the target type to convert to
 * @return the default generic converter that will perform the conversion
 */
@Nullable
protected GenericConverter getDefaultConverter(TypeDescriptor sourceType, TypeDescriptor targetType) {
	return (sourceType.isAssignableTo(targetType) ? NO_OP_CONVERTER : null);
}