org.springframework.core.convert.TypeDescriptor.isAssignableTo()方法的使用及代码示例

x33g5p2x  于2022-01-29 转载在 其他  
字(10.7k)|赞(0)|评价(0)|浏览(79)

本文整理了Java中org.springframework.core.convert.TypeDescriptor.isAssignableTo()方法的一些代码示例,展示了TypeDescriptor.isAssignableTo()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。TypeDescriptor.isAssignableTo()方法的具体详情如下:
包路径:org.springframework.core.convert.TypeDescriptor
类名称:TypeDescriptor
方法名:isAssignableTo

TypeDescriptor.isAssignableTo介绍

[英]Returns true if an object of this type descriptor can be assigned to the location described by the given type descriptor.

For example, valueOf(String.class).isAssignableTo(valueOf(CharSequence.class))returns true because a String value can be assigned to a CharSequence variable. On the other hand, valueOf(Number.class).isAssignableTo(valueOf(Integer.class))returns false because, while all Integers are Numbers, not all Numbers are Integers.

For arrays, collections, and maps, element and key/value types are checked if declared. For example, a List<String> field value is assignable to a Collection<CharSequence> field, but List<Number> is not assignable to List<Integer>.
[中]如果此类型描述符的对象可以分配到给定类型描述符描述的位置,则返回true。
例如,valueOf(String.class)。isAssignableTo(valueOf(CharSequence.class))返回true,因为可以将字符串值分配给CharSequence变量。另一方面,valueOf(Number.class)。isAssignableTo(valueOf(Integer.class))返回false,因为虽然所有整数都是数字,但并非所有数字都是整数。
对于数组、集合和映射,如果已声明,则会检查元素和键/值类型。例如,列表<String>字段值可分配给集合<CharSequence>字段,但列表<Number>不可分配给列表<Integer>。

代码示例

代码示例来源:origin: spring-projects/spring-framework

/**
 * 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);
}

代码示例来源:origin: spring-projects/spring-framework

private boolean isNestedAssignable(@Nullable TypeDescriptor nestedTypeDescriptor,
    @Nullable TypeDescriptor otherNestedTypeDescriptor) {
  return (nestedTypeDescriptor == null || otherNestedTypeDescriptor == null ||
      nestedTypeDescriptor.isAssignableTo(otherNestedTypeDescriptor));
}

代码示例来源:origin: org.springframework/spring-core

/**
 * 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);
}

代码示例来源:origin: spring-projects/spring-framework

@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");
}

代码示例来源:origin: spring-projects/spring-framework

@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));
}

代码示例来源:origin: spring-projects/spring-framework

@Nullable
private Object convertFromByteBuffer(ByteBuffer source, TypeDescriptor targetType) {
  byte[] bytes = new byte[source.remaining()];
  source.get(bytes);
  if (targetType.isAssignableTo(BYTE_ARRAY_TYPE)) {
    return bytes;
  }
  return this.conversionService.convert(bytes, BYTE_ARRAY_TYPE, targetType);
}

代码示例来源:origin: spring-projects/spring-framework

private boolean matchesFromByteBuffer(TypeDescriptor targetType) {
  return (targetType.isAssignableTo(BYTE_ARRAY_TYPE) ||
      this.conversionService.canConvert(BYTE_ARRAY_TYPE, targetType));
}

代码示例来源:origin: spring-projects/spring-framework

private boolean matchesToByteBuffer(TypeDescriptor sourceType) {
  return (sourceType.isAssignableTo(BYTE_ARRAY_TYPE) ||
      this.conversionService.canConvert(sourceType, BYTE_ARRAY_TYPE));
}

代码示例来源:origin: org.springframework/spring-core

private boolean isNestedAssignable(@Nullable TypeDescriptor nestedTypeDescriptor,
    @Nullable TypeDescriptor otherNestedTypeDescriptor) {
  return (nestedTypeDescriptor == null || otherNestedTypeDescriptor == null ||
      nestedTypeDescriptor.isAssignableTo(otherNestedTypeDescriptor));
}

代码示例来源:origin: spring-projects/spring-framework

@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());
}

代码示例来源:origin: spring-projects/spring-framework

@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");
}

代码示例来源:origin: spring-projects/spring-framework

@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);
}

代码示例来源:origin: spring-projects/spring-framework

@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);
}

代码示例来源:origin: spring-projects/spring-framework

@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);
}

代码示例来源:origin: spring-projects/spring-framework

@Override
public boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType) {
  if (sourceType.isAssignableTo(STREAM_TYPE)) {
    return matchesFromStream(sourceType.getElementTypeDescriptor(), targetType);
  }
  if (targetType.isAssignableTo(STREAM_TYPE)) {
    return matchesToStream(targetType.getElementTypeDescriptor(), sourceType);
  }
  return false;
}

代码示例来源:origin: spring-projects/spring-framework

@Override
@Nullable
public Object convert(@Nullable Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
  String text = (String) source;
  if (!StringUtils.hasText(text)) {
    return null;
  }
  Object result;
  try {
    result = this.parser.parse(text, LocaleContextHolder.getLocale());
  }
  catch (IllegalArgumentException ex) {
    throw ex;
  }
  catch (Throwable ex) {
    throw new IllegalArgumentException("Parse attempt failed for value [" + text + "]", ex);
  }
  TypeDescriptor resultType = TypeDescriptor.valueOf(result.getClass());
  if (!resultType.isAssignableTo(targetType)) {
    result = this.conversionService.convert(result, resultType, targetType);
  }
  return result;
}

代码示例来源:origin: org.springframework/spring-context

@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());
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void isAssignableElementTypes() throws Exception {
  assertTrue(new TypeDescriptor(getClass().getField("listField")).isAssignableTo(new TypeDescriptor(getClass().getField("listField"))));
  assertTrue(new TypeDescriptor(getClass().getField("notGenericList")).isAssignableTo(new TypeDescriptor(getClass().getField("listField"))));
  assertTrue(new TypeDescriptor(getClass().getField("listField")).isAssignableTo(new TypeDescriptor(getClass().getField("notGenericList"))));
  assertFalse(new TypeDescriptor(getClass().getField("isAssignableElementTypes")).isAssignableTo(new TypeDescriptor(getClass().getField("listField"))));
  assertTrue(TypeDescriptor.valueOf(List.class).isAssignableTo(new TypeDescriptor(getClass().getField("listField"))));
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void isAssignableMapKeyValueTypes() throws Exception {
  assertTrue(new TypeDescriptor(getClass().getField("mapField")).isAssignableTo(new TypeDescriptor(getClass().getField("mapField"))));
  assertTrue(new TypeDescriptor(getClass().getField("notGenericMap")).isAssignableTo(new TypeDescriptor(getClass().getField("mapField"))));
  assertTrue(new TypeDescriptor(getClass().getField("mapField")).isAssignableTo(new TypeDescriptor(getClass().getField("notGenericMap"))));
  assertFalse(new TypeDescriptor(getClass().getField("isAssignableMapKeyValueTypes")).isAssignableTo(new TypeDescriptor(getClass().getField("mapField"))));
  assertTrue(TypeDescriptor.valueOf(Map.class).isAssignableTo(new TypeDescriptor(getClass().getField("mapField"))));
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void isAssignableTypes() {
  assertTrue(TypeDescriptor.valueOf(Integer.class).isAssignableTo(TypeDescriptor.valueOf(Number.class)));
  assertFalse(TypeDescriptor.valueOf(Number.class).isAssignableTo(TypeDescriptor.valueOf(Integer.class)));
  assertFalse(TypeDescriptor.valueOf(String.class).isAssignableTo(TypeDescriptor.valueOf(String[].class)));
}

相关文章

微信公众号

最新文章

更多