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

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

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

TypeDescriptor.getResolvableType介绍

[英]Return the underlying ResolvableType.
[中]返回基础ResolvableType。

代码示例

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

@Override
  public int compareTo(ConverterCacheKey other) {
    int result = this.sourceType.getResolvableType().toString().compareTo(
        other.sourceType.getResolvableType().toString());
    if (result == 0) {
      result = this.targetType.getResolvableType().toString().compareTo(
          other.targetType.getResolvableType().toString());
    }
    return result;
  }
}

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

@Override
public String toString() {
  StringBuilder builder = new StringBuilder();
  for (Annotation ann : getAnnotations()) {
    builder.append("@").append(ann.annotationType().getName()).append(' ');
  }
  builder.append(getResolvableType().toString());
  return builder.toString();
}

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

@Override
  public int compareTo(ConverterCacheKey other) {
    int result = this.sourceType.getResolvableType().toString().compareTo(
        other.sourceType.getResolvableType().toString());
    if (result == 0) {
      result = this.targetType.getResolvableType().toString().compareTo(
          other.targetType.getResolvableType().toString());
    }
    return result;
  }
}

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

@Override
public Object convert(@Nullable Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
  if (source == null) {
    return Optional.empty();
  }
  else if (source instanceof Optional) {
    return source;
  }
  else if (targetType.getResolvableType().hasGenerics()) {
    Object target = this.conversionService.convert(source, sourceType, new GenericTypeDescriptor(targetType));
    if (target == null || (target.getClass().isArray() && Array.getLength(target) == 0) ||
          (target instanceof Collection && ((Collection<?>) target).isEmpty())) {
      return Optional.empty();
    }
    return Optional.of(target);
  }
  else {
    return Optional.of(source);
  }
}

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

public GenericTypeDescriptor(TypeDescriptor typeDescriptor) {
    super(typeDescriptor.getResolvableType().getGeneric(), null, typeDescriptor.getAnnotations());
  }
}

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

@Override
public String toString() {
  StringBuilder builder = new StringBuilder();
  for (Annotation ann : getAnnotations()) {
    builder.append("@").append(ann.annotationType().getName()).append(' ');
  }
  builder.append(getResolvableType().toString());
  return builder.toString();
}

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

/**
 * Narrows this {@link TypeDescriptor} by setting its type to the class of the
 * provided value.
 * <p>If the value is {@code null}, no narrowing is performed and this TypeDescriptor
 * is returned unchanged.
 * <p>Designed to be called by binding frameworks when they read property, field,
 * or method return values. Allows such frameworks to narrow a TypeDescriptor built
 * from a declared property, field, or method return value type. For example, a field
 * declared as {@code java.lang.Object} would be narrowed to {@code java.util.HashMap}
 * if it was set to a {@code java.util.HashMap} value. The narrowed TypeDescriptor
 * can then be used to convert the HashMap to some other type. Annotation and nested
 * type context is preserved by the narrowed copy.
 * @param value the value to use for narrowing this type descriptor
 * @return this TypeDescriptor narrowed (returns a copy with its type updated to the
 * class of the provided value)
 */
public TypeDescriptor narrow(@Nullable Object value) {
  if (value == null) {
    return this;
  }
  ResolvableType narrowed = ResolvableType.forType(value.getClass(), getResolvableType());
  return new TypeDescriptor(narrowed, value.getClass(), getAnnotations());
}

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

/**
 * If this type is a {@link Map} and its key type is parameterized,
 * returns the map's key type. If the Map's key type is not parameterized,
 * returns {@code null} indicating the key type is not declared.
 * @return the Map key type, or {@code null} if this type is a Map
 * but its key type is not parameterized
 * @throws IllegalStateException if this type is not a {@code java.util.Map}
 */
@Nullable
public TypeDescriptor getMapKeyTypeDescriptor() {
  Assert.state(isMap(), "Not a [java.util.Map]");
  return getRelatedIfResolvable(this, getResolvableType().asMap().getGeneric(0));
}

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

/**
 * If this type is a {@link Map} and its value type is parameterized,
 * returns the map's value type.
 * <p>If the Map's value type is not parameterized, returns {@code null}
 * indicating the value type is not declared.
 * @return the Map value type, or {@code null} if this type is a Map
 * but its value type is not parameterized
 * @throws IllegalStateException if this type is not a {@code java.util.Map}
 */
@Nullable
public TypeDescriptor getMapValueTypeDescriptor() {
  Assert.state(isMap(), "Not a [java.util.Map]");
  return getRelatedIfResolvable(this, getResolvableType().asMap().getGeneric(1));
}

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

/**
 * If this type is an array, returns the array's component type.
 * If this type is a {@code Stream}, returns the stream's component type.
 * If this type is a {@link Collection} and it is parameterized, returns the Collection's element type.
 * If the Collection is not parameterized, returns {@code null} indicating the element type is not declared.
 * @return the array component type or Collection element type, or {@code null} if this type is a
 * Collection but its element type is not parameterized
 * @throws IllegalStateException if this type is not a {@code java.util.Collection} or array type
 */
@Nullable
public TypeDescriptor getElementTypeDescriptor() {
  if (getResolvableType().isArray()) {
    return new TypeDescriptor(getResolvableType().getComponentType(), null, getAnnotations());
  }
  if (Stream.class.isAssignableFrom(getType())) {
    return getRelatedIfResolvable(this, getResolvableType().as(Stream.class).getGeneric(0));
  }
  return getRelatedIfResolvable(this, getResolvableType().asCollection().getGeneric(0));
}

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

@Override
public boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType) {
  if (targetType.getResolvableType().hasGenerics()) {
    return this.conversionService.canConvert(sourceType, new GenericTypeDescriptor(targetType));
  }
  else {
    return true;
  }
}

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

public GenericTypeDescriptor(TypeDescriptor typeDescriptor) {
    super(typeDescriptor.getResolvableType().getGeneric(), null, typeDescriptor.getAnnotations());
  }
}

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

/**
 * Cast this {@link TypeDescriptor} to a superclass or implemented interface
 * preserving annotations and nested type context.
 * @param superType the super type to cast to (can be {@code null})
 * @return a new TypeDescriptor for the up-cast type
 * @throws IllegalArgumentException if this type is not assignable to the super-type
 * @since 3.2
 */
@Nullable
public TypeDescriptor upcast(@Nullable Class<?> superType) {
  if (superType == null) {
    return null;
  }
  Assert.isAssignable(superType, getType());
  return new TypeDescriptor(getResolvableType().as(superType), superType, getAnnotations());
}

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

/**
 * If this type is a {@link Map} and its key type is parameterized,
 * returns the map's key type. If the Map's key type is not parameterized,
 * returns {@code null} indicating the key type is not declared.
 * @return the Map key type, or {@code null} if this type is a Map
 * but its key type is not parameterized
 * @throws IllegalStateException if this type is not a {@code java.util.Map}
 */
@Nullable
public TypeDescriptor getMapKeyTypeDescriptor() {
  Assert.state(isMap(), "Not a [java.util.Map]");
  return getRelatedIfResolvable(this, getResolvableType().asMap().getGeneric(0));
}

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

@Override
public boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType) {
  if (targetType.getResolvableType().hasGenerics()) {
    return this.conversionService.canConvert(sourceType, new GenericTypeDescriptor(targetType));
  }
  else {
    return true;
  }
}

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

/**
 * If this type is a {@link Map} and its value type is parameterized,
 * returns the map's value type.
 * <p>If the Map's value type is not parameterized, returns {@code null}
 * indicating the value type is not declared.
 * @return the Map value type, or {@code null} if this type is a Map
 * but its value type is not parameterized
 * @throws IllegalStateException if this type is not a {@code java.util.Map}
 */
@Nullable
public TypeDescriptor getMapValueTypeDescriptor() {
  Assert.state(isMap(), "Not a [java.util.Map]");
  return getRelatedIfResolvable(this, getResolvableType().asMap().getGeneric(1));
}

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

/**
 * If this type is an array, returns the array's component type.
 * If this type is a {@code Stream}, returns the stream's component type.
 * If this type is a {@link Collection} and it is parameterized, returns the Collection's element type.
 * If the Collection is not parameterized, returns {@code null} indicating the element type is not declared.
 * @return the array component type or Collection element type, or {@code null} if this type is a
 * Collection but its element type is not parameterized
 * @throws IllegalStateException if this type is not a {@code java.util.Collection} or array type
 */
@Nullable
public TypeDescriptor getElementTypeDescriptor() {
  if (getResolvableType().isArray()) {
    return new TypeDescriptor(getResolvableType().getComponentType(), null, getAnnotations());
  }
  if (Stream.class.isAssignableFrom(getType())) {
    return getRelatedIfResolvable(this, getResolvableType().as(Stream.class).getGeneric(0));
  }
  return getRelatedIfResolvable(this, getResolvableType().asCollection().getGeneric(0));
}

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

/**
 * Cast this {@link TypeDescriptor} to a superclass or implemented interface
 * preserving annotations and nested type context.
 * @param superType the super type to cast to (can be {@code null})
 * @return a new TypeDescriptor for the up-cast type
 * @throws IllegalArgumentException if this type is not assignable to the super-type
 * @since 3.2
 */
@Nullable
public TypeDescriptor upcast(@Nullable Class<?> superType) {
  if (superType == null) {
    return null;
  }
  Assert.isAssignable(superType, getType());
  return new TypeDescriptor(getResolvableType().as(superType), superType, getAnnotations());
}

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

@Override
public boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType) {
  // Check raw type first...
  if (this.typeInfo.getTargetType() != targetType.getObjectType()) {
    return false;
  }
  // Full check for complex generic type match required?
  ResolvableType rt = targetType.getResolvableType();
  if (!(rt.getType() instanceof Class) && !rt.isAssignableFrom(this.targetType) &&
      !this.targetType.hasUnresolvableGenerics()) {
    return false;
  }
  return !(this.converter instanceof ConditionalConverter) ||
      ((ConditionalConverter) this.converter).matches(sourceType, targetType);
}

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

@Override
public boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType) {
  // Check raw type first...
  if (this.typeInfo.getTargetType() != targetType.getObjectType()) {
    return false;
  }
  // Full check for complex generic type match required?
  ResolvableType rt = targetType.getResolvableType();
  if (!(rt.getType() instanceof Class) && !rt.isAssignableFrom(this.targetType) &&
      !this.targetType.hasUnresolvableGenerics()) {
    return false;
  }
  return !(this.converter instanceof ConditionalConverter) ||
      ((ConditionalConverter) this.converter).matches(sourceType, targetType);
}

相关文章

微信公众号

最新文章

更多