类 TypeDescriptor
- java.lang.Object
-
- cc.shacocloud.mirage.utils.converter.TypeDescriptor
-
- 所有已实现的接口:
Serializable
public class TypeDescriptor extends Object implements Serializable
关于要转换的类型描述符- 另请参阅:
- 序列化表格
-
-
构造器概要
构造器 构造器 说明 TypeDescriptor(MethodParameter methodParameter)从MethodParameter创建一个新的类型描述符TypeDescriptor(ResolvableType resolvableType, @Nullable Class<?> type, @Nullable Annotation[] annotations)从一个ResolvableType创建一个新的类型描述符TypeDescriptor(Field field)从一个Field创建一个新的类型描述符
-
方法概要
所有方法 静态方法 实例方法 具体方法 修饰符和类型 方法 说明 static @Nullable TypeDescriptorarray(@Nullable TypeDescriptor elementTypeDescriptor)创建一个新的类型描述符,作为指定类型的一个数组。static @NotNull TypeDescriptorcollection(@NotNull Class<?> collectionType, @Nullable TypeDescriptor elementTypeDescriptor)从一个Collection类型创建一个新的类型描述符。@Nullable TypeDescriptorelementTypeDescriptor(Object element)如果这个类型是一个Collection或一个数组,从提供的集合或数组元素中创建一个元素TypeDescriptorbooleanequals(@Nullable Object other)static @Nullable TypeDescriptorforObject(@Nullable Object source)为一个对象创建一个新的类型描述符。<T extends Annotation>
TgetAnnotation(Class<T> annotationType)获取此类型描述符上的指定annotationType的注释Annotation[]getAnnotations()返回与该类型描述符相关的注释,如果有的话。@Nullable TypeDescriptorgetElementTypeDescriptor()如果该类型是一个数组,返回数组的组件类型。@Nullable TypeDescriptorgetMapKeyTypeDescriptor()如果这个类型是Map,并且它的键类型是参数化的,返回地图的键类型。@Nullable TypeDescriptorgetMapKeyTypeDescriptor(Object mapKey)如果这个类型是一个Map,则从提供的地图键中创建一个mapKeyTypeDescriptor@Nullable TypeDescriptorgetMapValueTypeDescriptor()如果这个类型是一个Map,并且它的值类型是参数化的,返回map的值类型。@Nullable TypeDescriptorgetMapValueTypeDescriptor(Object mapValue)如果这个类型是一个Map,则从提供的地图值中创建一个mapValueTypeDescriptorStringgetName()返回该类型的名称:完全合格的类名称Class<?>getObjectType()getType()的变体,通过返回其对象包装类型来说明原始类型ResolvableTypegetResolvableType()返回底层的ResolvableTypeObjectgetSource()返回描述符的基本来源Class<?>getType()这个 TypeDescriptor 所描述的支持类、方法参数、字段或属性的类型booleanhasAnnotation(Class<? extends Annotation> annotationType)确定这个类型描述符是否有指定的注释inthashCode()booleanisArray()这个类型是一个数组类型吗?booleanisAssignableTo(@NotNull TypeDescriptor typeDescriptor)如果这个类型描述符的对象可以被分配到给定类型描述符所描述的位置,则返回true。booleanisCollection()这个类型是一个Collection类型吗?booleanisMap()这个类型是一个Map类型吗?booleanisPrimitive()这个类型是一个原始类型吗?static @NotNull TypeDescriptormap(@NotNull Class<?> mapType, @Nullable TypeDescriptor keyTypeDescriptor, @Nullable TypeDescriptor valueTypeDescriptor)从一个Map类型创建一个新的类型描述符。TypeDescriptornarrow(@Nullable Object value)缩小这个TypeDescriptor,将其类型设置为提供的值的类别。static @Nullable TypeDescriptornested(MethodParameter methodParameter, int nestingLevel)为方法参数中声明的嵌套类型创建一个类型描述符。static @Nullable TypeDescriptornested(Field field, int nestingLevel)为字段内声明的嵌套类型创建一个类型描述符。StringtoString()@Nullable TypeDescriptorupcast(@Nullable Class<?> superType)将这个TypeDescriptor投给一个超类或实现的接口,保留注释和嵌套的类型上下文。static @NotNull TypeDescriptorvalueOf(@Nullable Class<?> type)从给定的类型创建一个新的类型描述符
-
-
-
构造器详细资料
-
TypeDescriptor
public TypeDescriptor(MethodParameter methodParameter)
从MethodParameter创建一个新的类型描述符当源或目标转换点是构造函数参数、方法参数或方法返回值时,请使用此构造函数。
-
TypeDescriptor
public TypeDescriptor(ResolvableType resolvableType, @Nullable @Nullable Class<?> type, @Nullable @Nullable Annotation[] annotations)
从一个ResolvableType创建一个新的类型描述符这个构造函数在内部使用,也可能被支持具有扩展类型系统的非Java语言的子类使用
- 参数:
resolvableType- 可解决的类型type- 支持的类型annotations- 类型注释
-
-
方法详细资料
-
getObjectType
public Class<?> getObjectType()
getType()的变体,通过返回其对象包装类型来说明原始类型这对于希望规范化为基于对象的类型而不直接处理原始类型的转换服务实现是很有用的。
-
getType
public Class<?> getType()
这个 TypeDescriptor 所描述的支持类、方法参数、字段或属性的类型按原样返回原始类型。请参阅
getObjectType()以了解此操作的变体,如果有必要,它可以将原始类型解析为其对应的对象类型。- 另请参阅:
getObjectType()
-
getResolvableType
public ResolvableType getResolvableType()
返回底层的ResolvableType
-
getSource
public Object getSource()
返回描述符的基本来源将返回一个
Field、MethodParameter或Type,这取决于TypeDescriptor的构造方式
-
narrow
public TypeDescriptor narrow(@Nullable @Nullable Object value)
缩小这个TypeDescriptor,将其类型设置为提供的值的类别。如果值是
null,则不进行缩小,这个 TypeDescriptor 将被原样返回。设计成由绑定框架在读取属性、字段或方法返回值时调用。允许这些框架缩小TypeDescriptor的范围,从声明的属性、字段或方法的返回值类型中建立。 例如,一个声明为
java.lang.Object的字段将被缩小为java.util.HashMap如果它被设置为java.util.HashMap值。 然后,缩小的TypeDescriptor 可以被用来将HashMap转换为其他类型。注释和嵌套的类型上下文被缩小的副本所保留。- 参数:
value- 用于缩小该类型描述符范围的值- 返回:
- 这个 TypeDescriptor 缩小了(返回一个副本,其类型更新为所提供值的类)。
-
upcast
@Nullable public @Nullable TypeDescriptor upcast(@Nullable @Nullable Class<?> superType)
将这个TypeDescriptor投给一个超类或实现的接口,保留注释和嵌套的类型上下文。- 参数:
superType- 要转换的超类型(可以是null)- 返回:
- 一个新的 TypeDescriptor ,用于向上转换的类型
- 抛出:
IllegalArgumentException- 如果该类型不能被分配给超类
-
getName
public String getName()
返回该类型的名称:完全合格的类名称
-
isPrimitive
public boolean isPrimitive()
这个类型是一个原始类型吗?
-
getAnnotations
public Annotation[] getAnnotations()
返回与该类型描述符相关的注释,如果有的话。- 返回:
- 注释,如果没有,则为空数组
-
hasAnnotation
public boolean hasAnnotation(Class<? extends Annotation> annotationType)
确定这个类型描述符是否有指定的注释- 参数:
annotationType- 注释类型- 返回:
- 如果存在则返回 true
-
getAnnotation
@Nullable public <T extends Annotation> T getAnnotation(Class<T> annotationType)
获取此类型描述符上的指定annotationType的注释- 参数:
annotationType- 注释类型- 返回:
- 注释,如果该类型描述符上不存在这样的注释,则
null
-
isAssignableTo
public boolean isAssignableTo(@NotNull @NotNull TypeDescriptor typeDescriptor)如果这个类型描述符的对象可以被分配到给定类型描述符所描述的位置,则返回true。例如,
valueOf(String.class).isAssignableTo(valueOf(CharSequence.class))返回true, 因为一个字符串值可以被分配给CharSequence变量。 另一方面,valueOf(Number.class).isAssignableTo(valueOf(Integer.class))返回false, 因为虽然所有的整数是数字,但不是所有数字是整数。 *对于数组、集合和地图,如果声明了元素和键/值类型,就会被检查。 例如,一个List<String>字段的值可以分配给一个Collection<CharSequence>字段,但是List<Number>不能分配给List<Integer>。
- 返回:
true如果这个类型可以分配给所提供的*类型描述符所代表的类型- 另请参阅:
getObjectType()
-
isCollection
public boolean isCollection()
这个类型是一个Collection类型吗?
-
isArray
public boolean isArray()
这个类型是一个数组类型吗?
-
getElementTypeDescriptor
@Nullable public @Nullable TypeDescriptor getElementTypeDescriptor()
如果该类型是一个数组,返回数组的组件类型。 如果该类型是一个Stream,则返回该流的组件类型。 如果该类型是一个Collection并且是参数化的,则返回该集合的元素类型 如果集合没有被参数化,则返回null,表明元素类型没有被声明- 返回:
- 数组组件类型或集合元素类型,如果该类型不是数组类型或
java.util.Collection或其元素类型未被参数化,则null。 - 另请参阅:
elementTypeDescriptor(Object)
-
elementTypeDescriptor
@Nullable public @Nullable TypeDescriptor elementTypeDescriptor(Object element)
如果这个类型是一个Collection或一个数组,从提供的集合或数组元素中创建一个元素TypeDescriptor将
elementType属性缩小到所提供的集合或数组元素的类别。 例如,如果这描述了一个java.util.List<java.lang.Number>并且元素参数是一个java.lang.Integer, 返回的TypeDescriptor将是java.lang.Integer. 如果这描述了一个java.util.List<?>并且元素参数是一个java.lang.Integer, 返回的 TypeDescriptor 也是java.lang.Integer注释和嵌套的类型上下文将被保留在被返回的缩小的 TypeDescriptor中。
- 参数:
element- 集合或数组元素- 返回:
- 一个元素类型描述符,缩小到所提供元素的类型。
- 另请参阅:
getElementTypeDescriptor(),narrow(Object)
-
isMap
public boolean isMap()
这个类型是一个Map类型吗?
-
getMapKeyTypeDescriptor
@Nullable public @Nullable TypeDescriptor getMapKeyTypeDescriptor()
如果这个类型是Map,并且它的键类型是参数化的,返回地图的键类型。 如果Map的键类型没有被参数化,返回null,表示该键类型没有被声明。- 返回:
- Map的关键类型,或者
null如果这个类型是一个Map 但是它的关键类型没有被参数化 - 抛出:
IllegalStateException- 如果该类型不是java.util.Map,则抛出该例外
-
getMapKeyTypeDescriptor
@Nullable public @Nullable TypeDescriptor getMapKeyTypeDescriptor(Object mapKey)
如果这个类型是一个Map,则从提供的地图键中创建一个mapKeyTypeDescriptor将
mapKeyType属性缩小到所提供的地图键的类别。 例如,如果这描述了一个java.util.Map<java.lang.Number, java.lang.String>并且键参数是一个java.lang.Integer, 返回的TypeDescriptor将是java.lang.Integer。 如果这描述了一个java.util.Map<?, ?>并且关键参数是一个java.lang.Integer, 返回的 TypeDescriptor也将是java.lang.Integer。注释和嵌套的类型上下文将被保留在被返回的缩小的 TypeDescriptor中。
- 参数:
mapKey- 地图键- 返回:
- 地图键类型描述符
- 抛出:
IllegalStateException- 如果该类型不是java.util.Map,则抛出该例外- 另请参阅:
narrow(Object)
-
getMapValueTypeDescriptor
@Nullable public @Nullable TypeDescriptor getMapValueTypeDescriptor()
如果这个类型是一个Map,并且它的值类型是参数化的,返回map的值类型。如果Map的值类型没有被参数化,则返回
null表示该值类型没有被声明。- 返回:
- Map值类型, 如果这个类型是一个Map 但是它的值类型没有被参数化,返回
null - 抛出:
IllegalStateException- 如果该类型不是java.util.Map,则抛出该例外
-
getMapValueTypeDescriptor
@Nullable public @Nullable TypeDescriptor getMapValueTypeDescriptor(Object mapValue)
如果这个类型是一个Map,则从提供的地图值中创建一个mapValueTypeDescriptor将
mapValueType属性缩小到所提供的地图值的类别。 例如,如果这描述了一个java.util.Map<java.lang.String, java.lang.Number>并且值参数是一个java.lang.Integer, 返回的TypeDescriptor将是 *java.lang.Integer。 如果这描述了一个java.util.Map<?, ?>并且值参数是一个java.lang.Integer, 返回的 TypeDescriptor 也将是java.lang.Integer。注释和嵌套的类型上下文将被保留在被返回的缩小的 TypeDescriptor中。
- 参数:
mapValue- the map value- 返回:
- the map value type descriptor
- 抛出:
IllegalStateException- if this type is not ajava.util.Map- 另请参阅:
narrow(Object)
-
forObject
@Nullable public static @Nullable TypeDescriptor forObject(@Nullable @Nullable Object source)
为一个对象创建一个新的类型描述符。在要求转换系统将一个源对象转换为另一种类型之前,使用这个工厂方法对其进行反省,转换系统将其转换为另一种类型。
如果提供的对象是
null,返回null,否则调用valueOf(Class)来从该对象的类中建立一个TypeDescriptor。- 参数:
source- 源对象- 返回:
- 类型描述符
-
valueOf
@NotNull public static @NotNull TypeDescriptor valueOf(@Nullable @Nullable Class<?> type)
从给定的类型创建一个新的类型描述符使用这个指示转换系统将一个对象转换为一个 特定的目标类型,当没有类型的位置,如方法参数或字段来提供额外的转换环境。
一般来说,更倾向于使用
forObject(Object)来构建类型描述符,因为它可以处理null对象的情况。- 参数:
type- 类(可以用null来表示Object.class)- 返回:
- 相应的类型描述符
-
collection
@Contract("_, _ -> new") @NotNull public static @NotNull TypeDescriptor collection(@NotNull @NotNull Class<?> collectionType, @Nullable @Nullable TypeDescriptor elementTypeDescriptor)从一个Collection类型创建一个新的类型描述符。对于转换为类型化的集合是有用的。
例如,一个
List<String>可以通过转换为一个List<EmailAddress>通过转换到一个用这个方法构建的targetType。 构建这样一个TypeDescriptor的方法调用将看起来像像这样collection(List.class, TypeDescriptor.valueOf(EmailAddress.class));。- 参数:
collectionType- 集合类型,它必须实现CollectionelementTypeDescriptor- 一个关于集合元素类型的描述符- 返回:
- 类型描述符
-
map
@Contract("_, _, _ -> new") @NotNull public static @NotNull TypeDescriptor map(@NotNull @NotNull Class<?> mapType, @Nullable @Nullable TypeDescriptor keyTypeDescriptor, @Nullable @Nullable TypeDescriptor valueTypeDescriptor)从一个Map类型创建一个新的类型描述符。对于转换为类型化的Maps来说是有用的。
例如,一个Map<String, String>可以转换为一个Map<Id, EmailAddress> 通过转换到一个用这个方法构建的targetType。 构建这样一个TypeDescriptor的方法调用将看起来像。
map(Map.class, TypeDescriptor.valueOf(Id.class), TypeDescriptor.valueOf(EmailAddress.class))。- 参数:
mapType- 地图类型,它必须实现Map。keyTypeDescriptor- 地图键类型的描述符,用于转换地图键。valueTypeDescriptor- 地图的值类型,用于转换地图值。- 返回:
- 地图类型描述符
-
array
@Nullable public static @Nullable TypeDescriptor array(@Nullable @Nullable TypeDescriptor elementTypeDescriptor)
创建一个新的类型描述符,作为指定类型的一个数组。例如,创建一个
Map<String,String>[]使用。TypeDescriptor.array(TypeDescriptor.map(Map.class, TypeDescriptor.value(String.class), TypeDescriptor.value(String.class))。- 参数:
elementTypeDescriptor-TypeDescriptor或null- 返回:
- 一个数组
TypeDescriptor,如果elementTypeDescriptor是null的话返回null
-
nested
@Nullable public static @Nullable TypeDescriptor nested(MethodParameter methodParameter, int nestingLevel)
为方法参数中声明的嵌套类型创建一个类型描述符。例如,如果methodParameter是一个
List<String>,并且嵌套级别是1,嵌套类型描述符将是String.class。如果methodParameter是一个
List<List<String>,并且嵌套级别是2,那么嵌套的类型描述符就是String.class。 级别是2,嵌套的类型描述符也将是String.class。如果methodParameter是一个
Map<Integer, String>并且嵌套的级别是1,嵌套的类型描述符将是String,由map值派生。如果methodParameter是一个
List<Map<Integer, String>并且嵌套级别是2,那么嵌套的类型描述符将是String,由map值派生。 嵌套级别是2,嵌套类型描述符将是String,由map值派生。如果一个嵌套类型因为没有被声明而无法获得,则返回
null。 例如,如果方法参数是List<?>,返回的嵌套类型将是List<?>返回的描述符将是null。- 参数:
methodParameter- 方法参数,其嵌套级别为 1nestingLevel- 集合/数组元素的嵌套级别或方法参数内的映射键/值声明- 返回:
- 在指定的嵌套层次上的嵌套类型描述符。如果无法获得,则
null。 - 抛出:
IllegalArgumentException- 如果输入的嵌套级别MethodParameter参数不是1,或者到指定嵌套级别的类型不是集合、 数组或地图类型, 指定的嵌套级别不属于集合、数组或地图类型。
-
nested
@Nullable public static @Nullable TypeDescriptor nested(Field field, int nestingLevel)
为字段内声明的嵌套类型创建一个类型描述符。例如,如果field是一个
List<String>,并且嵌套级别是1,嵌套类型描述符将是String.class。如果field是一个
List<List<String>,并且嵌套级别是2,那么嵌套的类型描述符就是String.class。 级别是2,嵌套的类型描述符也将是String.class。如果field是一个
Map<Integer, String>并且嵌套的级别是1,嵌套的类型描述符将是String,由map值派生。如果field是一个
List<Map<Integer, String>并且嵌套级别是2,那么嵌套的类型描述符将是String,由map值派生。 嵌套级别是2,嵌套类型描述符将是String,由map值派生。如果一个嵌套类型因为没有被声明而无法获得,则返回
null。 例如,如果方法参数是List<?>,返回的嵌套类型将是List<?>返回的描述符将是null。- 参数:
field-FieldnestingLevel- 集合/数组元素的嵌套级别或方法参数内的映射键/值声明- 返回:
- 在指定的嵌套层次上的嵌套类型描述符。如果无法获得,则
null。 - 抛出:
IllegalArgumentException- 如果到指定嵌套级别的类型不属于集合、数组或地图类型
-
-