类 TypeDescriptor
- 所有已实现的接口:
Serializable
Capable of representing arrays and generic collection types.
- 从以下版本开始:
- 3.0 2021/3/22 20:37
- 作者:
- Keith Donald, Andy Clement, Juergen Hoeller, Phillip Webb, Sam Brannen, Stephane Nicoll, Harry Yang
- 另请参阅:
-
嵌套类概要
嵌套类修饰符和类型类说明private static classAdapter class for exposing aTypeDescriptor's annotations as anAnnotatedElement, in particular toAnnotatedElementUtils. -
字段概要
字段修饰符和类型字段说明private final TypeDescriptor.AnnotatedElementAdapterprivate static final Class<?>[]private static final HashMap<Class<?>,TypeDescriptor> private Integerprivate final ResolvableTypeprivate static final longprivate final Class<?> -
构造器概要
构造器构造器说明TypeDescriptor(MethodParameter methodParameter) Create a new type descriptor from aMethodParameter.TypeDescriptor(ResolvableType resolvableType, Class<?> type, Annotation[] annotations) Create a new type descriptor from aResolvableType.TypeDescriptor(ResolvableType resolvableType, Class<?> type, AnnotatedElement annotated) Create a new type descriptor from aResolvableType.TypeDescriptor(Property property) Create a new type descriptor from aProperty.TypeDescriptor(Field field) Create a new type descriptor from aField. -
方法概要
修饰符和类型方法说明private booleanannotationEquals(Annotation ann, Annotation otherAnn) private booleanannotationsMatch(TypeDescriptor otherDesc) static TypeDescriptorarray(TypeDescriptor elementDescriptor) Create a new type descriptor as an array of the specified type.static TypeDescriptorcollection(Class<?> collectionType, TypeDescriptor elementDesc) Create a new type descriptor from aCollectiontype.static TypeDescriptorcollection(Class<?> collectionType, Class<?> element) elementDescriptor(Object element) If this type is aCollectionor an array, creates a element TypeDescriptor from the provided collection or array element.booleanstatic TypeDescriptorCreate a new type descriptor for an object.static TypeDescriptorforParameter(Executable executable, int parameterIndex) static TypeDescriptorfromParameter(Parameter parameter) for internal using<T extends Annotation>
TgetAnnotation(Class<T> annotationType) Obtain the annotation of the specifiedannotationTypethat is on this type descriptor.Return the annotations associated with this type descriptor, if any.Class<?>If this type is an array, returns the array's component type.If this type is aMapand its key type is parameterized, returns the map's key type.getMapKeyDescriptor(Object mapKey) If this type is aMap, creates a mapKeyTypeDescriptorfrom the provided map key.If this type is aMapand its value type is parameterized, returns the map's value type.getMapValueDescriptor(Object mapValue) If this type is aMap, creates a mapValueTypeDescriptorfrom the provided map value.getName()Return the name of this type: the fully qualified class name.private static StringClass<?>Variation ofgetType()that accounts for a primitive type by returning its object wrapper type.private TypeDescriptorReturn the underlyingResolvableType.Return the underlying source of the descriptor.Class<?>getType()The type of the backing class, method parameter, field, or property described by this TypeDescriptor.booleanhasAnnotation(Class<? extends Annotation> annotationType) Determine if this type descriptor has the specified annotation.inthashCode()booleanbooleanisArray()booleanisAssignableFrom(Class<?> subType) booleanisAssignableTo(TypeDescriptor typeDescriptor) Returns true if an object of this type descriptor can be assigned to the location described by the given type descriptor.booleanisAssignableTo(Class<?> superType) booleanbooleanisEnum()booleanisInstance(Object source) booleanisMap()Is this type aMaptype?private booleanisNestedAssignable(TypeDescriptor nestedTypeDescriptor, TypeDescriptor otherNestedTypeDescriptor) booleanIs this type a primitive type?static TypeDescriptormap(Class<?> mapType, TypeDescriptor keyDesc, TypeDescriptor valueDesc) Create a new type descriptor from aMaptype.static TypeDescriptorNarrows thisTypeDescriptorby setting its type to the class of the provided value.private TypeDescriptornarrow(Object value, TypeDescriptor typeDescriptor) nested(int nestingLevel) Create a type descriptor for a nested type declared within this descriptor.static TypeDescriptornested(MethodParameter methodParameter, int nestingLevel) Create a type descriptor for a nested type declared within the method parameter.static TypeDescriptorCreate a type descriptor for a nested type declared within the field.toString()Cast thisTypeDescriptorto a superclass or implemented interface preserving annotations and nested type context.static TypeDescriptorCreate a new type descriptor from the given type.
-
字段详细资料
-
serialVersionUID
private static final long serialVersionUID- 另请参阅:
-
commonTypesCache
-
CACHED_COMMON_TYPES
-
type
-
resolvableType
-
annotatedElement
-
hash
-
-
构造器详细资料
-
TypeDescriptor
Create a new type descriptor from aField.Use this constructor when a source or target conversion point is a field.
- 参数:
field- the field
-
TypeDescriptor
public TypeDescriptor(ResolvableType resolvableType, @Nullable Class<?> type, @Nullable Annotation[] annotations) Create a new type descriptor from aResolvableType.This constructor is used internally and may also be used by subclasses that support non-Java languages with extended type systems.
- 参数:
resolvableType- the resolvable typetype- the backing type (ornullif it should get resolved)annotations- the type annotations
-
TypeDescriptor
public TypeDescriptor(ResolvableType resolvableType, @Nullable Class<?> type, @Nullable AnnotatedElement annotated) Create a new type descriptor from aResolvableType.This constructor is used internally and may also be used by subclasses that support non-Java languages with extended type systems.
- 参数:
resolvableType- the resolvable typetype- the backing type (ornullif it should get resolved)annotated- annotated-element- 从以下版本开始:
- 4.0
-
TypeDescriptor
Create a new type descriptor from aMethodParameter.Use this constructor when a source or target conversion point is a constructor parameter, method parameter, or method return value.
- 参数:
methodParameter- the method parameter- 从以下版本开始:
- 4.0
-
TypeDescriptor
Create a new type descriptor from aProperty.Use this constructor when a source or target conversion point is a property on a Java class.
- 参数:
property- the property
-
-
方法详细资料
-
getObjectType
Variation ofgetType()that accounts for a primitive type by returning its object wrapper type.This is useful for conversion service implementations that wish to normalize to object-based types and not work with primitive types directly.
-
getType
The type of the backing class, method parameter, field, or property described by this TypeDescriptor.Returns primitive types as-is. See
getObjectType()for a variation of this operation that resolves primitive types to their corresponding Object types if necessary.- 另请参阅:
-
isArray
public boolean isArray() -
isCollection
public boolean isCollection() -
getComponentType
-
isInstance
-
is
-
isAssignableFrom
-
isAssignableTo
-
isEnum
public boolean isEnum() -
getName
Return the name of this type: the fully qualified class name. -
getSimpleName
-
getResolvableType
Return the underlyingResolvableType. -
getSource
Return the underlying source of the descriptor. Will return aField,ParameterorTypedepending on how theTypeDescriptorwas constructed. This method is primarily to provide access to additional type information or meta-data that alternative JVM languages may provide. -
nested
Create a type descriptor for a nested type declared within this descriptor.- 参数:
nestingLevel- the nesting level of the collection/array element or map key/value declaration within the property- 返回:
- the nested type descriptor at the specified nesting level, or
nullif it could not be obtained
-
narrow
Narrows thisTypeDescriptorby setting its type to the class of the provided value.If the value is
null, no narrowing is performed and this TypeDescriptor is returned unchanged.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
java.lang.Objectwould be narrowed tojava.util.HashMapif it was set to ajava.util.HashMapvalue. 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.- 参数:
value- the value to use for narrowing this type descriptor- 返回:
- this TypeDescriptor narrowed (returns a copy with its type updated to the class of the provided value)
-
upcast
Cast thisTypeDescriptorto a superclass or implemented interface preserving annotations and nested type context.- 参数:
superType- the super type to cast to (can benull)- 返回:
- a new TypeDescriptor for the up-cast type
- 抛出:
IllegalArgumentException- if this type is not assignable to the super-type
-
isPrimitive
public boolean isPrimitive()Is this type a primitive type? -
getAnnotations
Return the annotations associated with this type descriptor, if any.- 返回:
- the annotations, or an empty array if none
-
getAnnotatedElement
for internal using -
hasAnnotation
Determine if this type descriptor has the specified annotation.this method supports arbitrary levels of meta-annotations.
- 参数:
annotationType- the annotation type- 返回:
trueif the annotation is present
-
getAnnotation
Obtain the annotation of the specifiedannotationTypethat is on this type descriptor.This method supports arbitrary levels of meta-annotations.
- 参数:
annotationType- the annotation type- 返回:
- the annotation, or
nullif no such annotation exists on this type descriptor
-
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))returnstruebecause a String value can be assigned to a CharSequence variable. On the other hand,valueOf(Number.class).isAssignableTo(valueOf(Integer.class))returnsfalsebecause, 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>.
- 返回:
trueif this type is assignable to the type represented by the provided type descriptor
-
isNestedAssignable
private boolean isNestedAssignable(@Nullable TypeDescriptor nestedTypeDescriptor, @Nullable TypeDescriptor otherNestedTypeDescriptor) -
getElementDescriptor
If this type is an array, returns the array's component type. If this type is aStream, returns the stream's component type. If this type is aCollectionand it is parameterized, returns the Collection's element type. If the Collection is not parameterized, returnsnullindicating the element type is not declared.- 返回:
- the array component type or Collection element type, or
nullif this type is not an array type or ajava.util.Collectionor if its element type is not parameterized - 另请参阅:
-
elementDescriptor
If this type is aCollectionor an array, creates a element TypeDescriptor from the provided collection or array element.Narrows the
elementTypeproperty to the class of the provided collection or array element. For example, if this describes ajava.util.List<java.lang.Number<and the element argument is anjava.lang.Integer, the returned TypeDescriptor will bejava.lang.Integer. If this describes ajava.util.List<?>and the element argument is anjava.lang.Integer, the returned TypeDescriptor will bejava.lang.Integeras well.Annotation and nested type context will be preserved in the narrowed TypeDescriptor that is returned.
- 参数:
element- the collection or array element- 返回:
- a element type descriptor, narrowed to the type of the provided element
- 另请参阅:
-
isMap
public boolean isMap()Is this type aMaptype? -
getMapKeyDescriptor
If this type is aMapand its key type is parameterized, returns the map's key type. If the Map's key type is not parameterized, returnsnullindicating the key type is not declared.- 返回:
- the Map key type, or
nullif this type is a Map but its key type is not parameterized - 抛出:
IllegalStateException- if this type is not ajava.util.Map
-
getMapKeyDescriptor
If this type is aMap, creates a mapKeyTypeDescriptorfrom the provided map key.Narrows the
mapKeyTypeproperty to the class of the provided map key. For example, if this describes ajava.util.Map<java.lang.Number, java.lang.String<and the key argument is ajava.lang.Integer, the returned TypeDescriptor will bejava.lang.Integer. If this describes ajava.util.Map<?, ?>and the key argument is ajava.lang.Integer, the returned TypeDescriptor will bejava.lang.Integeras well.Annotation and nested type context will be preserved in the narrowed TypeDescriptor that is returned.
- 参数:
mapKey- the map key- 返回:
- the map key type descriptor
- 抛出:
IllegalStateException- if this type is not ajava.util.Map- 另请参阅:
-
getMapValueDescriptor
If this type is aMapand its value type is parameterized, returns the map's value type.If the Map's value type is not parameterized, returns
nullindicating the value type is not declared.- 返回:
- the Map value type, or
nullif this type is a Map but its value type is not parameterized - 抛出:
IllegalStateException- if this type is not ajava.util.Map
-
getMapValueDescriptor
If this type is aMap, creates a mapValueTypeDescriptorfrom the provided map value.Narrows the
mapValueTypeproperty to the class of the provided map value. For example, if this describes ajava.util.Map<java.lang.String, java.lang.Number<and the value argument is ajava.lang.Integer, the returned TypeDescriptor will bejava.lang.Integer. If this describes ajava.util.Map<?, ?>and the value argument is ajava.lang.Integer, the returned TypeDescriptor will bejava.lang.Integeras well.Annotation and nested type context will be preserved in the narrowed TypeDescriptor that is returned.
- 参数:
mapValue- the map value- 返回:
- the map value type descriptor
- 抛出:
IllegalStateException- if this type is not ajava.util.Map- 另请参阅:
-
narrow
@Nullable private TypeDescriptor narrow(@Nullable Object value, @Nullable TypeDescriptor typeDescriptor) -
equals
-
annotationsMatch
-
annotationEquals
-
hashCode
public int hashCode() -
toString
-
getName
-
forObject
Create a new type descriptor for an object.Use this factory method to introspect a source object before asking the conversion system to convert it to some another type.
If the provided object is
null, returnsnull, else callsvalueOf(Class)to build a TypeDescriptor from the object's class.- 参数:
source- the source object- 返回:
- the type descriptor
-
valueOf
Create a new type descriptor from the given type.Use this to instruct the conversion system to convert an object to a specific target type, when no type location such as a method parameter or field is available to provide additional conversion context.
Generally prefer use of
forObject(Object)for constructing type descriptors from source objects, as it handles thenullobject case.- 参数:
type- the class (may benullto indicateObject.class)- 返回:
- the corresponding type descriptor
-
collection
-
collection
public static TypeDescriptor collection(Class<?> collectionType, @Nullable TypeDescriptor elementDesc) Create a new type descriptor from aCollectiontype.Useful for converting to typed Collections.
For example, a
List<String>could be converted to aList<EmailAddress>by converting to a targetType built with this method. The method call to construct such aTypeDescriptorwould look something like:collection(List.class, TypeDescriptor.valueOf(EmailAddress.class));- 参数:
collectionType- the collection type, which must implementCollection.elementDesc- a descriptor for the collection's element type, used to convert collection elements- 返回:
- the collection type descriptor
-
map
-
map
public static TypeDescriptor map(Class<?> mapType, @Nullable TypeDescriptor keyDesc, @Nullable TypeDescriptor valueDesc) Create a new type descriptor from aMaptype.Useful for converting to typed Maps.
For example, a Map<String, String> could be converted to a Map<Id, EmailAddress> by converting to a targetType built with this method: The method call to construct such a TypeDescriptor would look something like:
map(Map.class, TypeDescriptor.valueOf(Id.class), TypeDescriptor.valueOf(EmailAddress.class));
- 参数:
mapType- the map type, which must implementMapkeyDesc- a descriptor for the map's key type, used to convert map keysvalueDesc- the map's value type, used to convert map values- 返回:
- the map type descriptor
-
array
Create a new type descriptor as an array of the specified type.For example to create a
Map<String,String>[]use:TypeDescriptor.array(TypeDescriptor.map(Map.class, TypeDescriptor.value(String.class), TypeDescriptor.value(String.class)));
- 参数:
elementDescriptor- theTypeDescriptorof the array element ornull- 返回:
- an array
TypeDescriptorornullifelementDescriptorisnull
-
nested
Create a type descriptor for a nested type declared within the field.For example, if the field is a
List<String>and the nesting level is 1, the nested type descriptor will beString.class.If the field is a
List<List<String>>and the nesting level is 2, the nested type descriptor will also be aString.class.If the field is a
Map<Integer, String>and the nesting level is 1, the nested type descriptor will be String, derived from the map value.If the field is a
List<Map<Integer, String>>and the nesting level is 2, the nested type descriptor will be String, derived from the map value.Returns
nullif a nested type cannot be obtained because it was not declared. For example, if the field is aList<?>, the nested type descriptor returned will benull.- 参数:
field- the fieldnestingLevel- the nesting level of the collection/array element or map key/value declaration within the field- 返回:
- the nested type descriptor at the specified nesting level,
or
nullif it could not be obtained - 抛出:
IllegalArgumentException- if the types up to the specified nesting level are not of collection, array, or map types
-
nested
Create a type descriptor for a nested type declared within the method parameter.For example, if the methodParameter is a
List<String>and the nesting level is 1, the nested type descriptor will be String.class.If the methodParameter is a
List<List<String>>and the nesting level is 2, the nested type descriptor will also be a String.class.If the methodParameter is a
Map<Integer, String>and the nesting level is 1, the nested type descriptor will be String, derived from the map value.If the methodParameter is a
List<Map<Integer, String>>and the nesting level is 2, the nested type descriptor will be String, derived from the map value.Returns
nullif a nested type cannot be obtained because it was not declared. For example, if the method parameter is aList<?>, the nested type descriptor returned will benull.- 参数:
methodParameter- the method parameter with a nestingLevel of 1nestingLevel- the nesting level of the collection/array element or map key/value declaration within the method parameter- 返回:
- the nested type descriptor at the specified nesting level,
or
nullif it could not be obtained - 抛出:
IllegalArgumentException- if the nesting level of the inputMethodParameterargument is not 1, or if the types up to the specified nesting level are not of collection, array, or map types- 从以下版本开始:
- 4.0
-
getRelatedIfResolvable
-
forParameter
-
fromParameter
- 从以下版本开始:
- 3.0.2
-