下面列出了org.springframework.core.convert.TypeDescriptor#isAssignableTo ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@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);
}
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;
}
@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);
}
@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());
}
@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);
}
@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");
}
@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));
}
@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);
}
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);
}
/**
* 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);
}
@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));
}
@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);
}
@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");
}
/**
* 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);
}
@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");
}
@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");
}
@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));
}
private boolean matchesToByteBuffer(TypeDescriptor sourceType) {
return (sourceType.isAssignableTo(BYTE_ARRAY_TYPE) ||
this.conversionService.canConvert(sourceType, BYTE_ARRAY_TYPE));
}
private boolean matchesFromByteBuffer(TypeDescriptor targetType) {
return (targetType.isAssignableTo(BYTE_ARRAY_TYPE) ||
this.conversionService.canConvert(BYTE_ARRAY_TYPE, targetType));
}
/**
* 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);
}