public class TypeInfo extends Object
Type to offer easy retrieval of various type information.| Modifier | Constructor and Description |
|---|---|
protected |
TypeInfo()
No-args constructor for overriding classes which define the type to wrap by overriding
inferTypeForNoArgsConstructor(). |
|
TypeInfo(Type type)
Constructor.
|
| Modifier and Type | Method and Description |
|---|---|
boolean |
equals(Object obj) |
boolean |
equalTo(Class<?> clazz)
Returns whether this type info, as class (
toClass()), is equal to the given class. |
Set<TypeInfo> |
getAllTypeInfos()
Returns all types that this wrapped type is an instance of, as TypeInfo instances.
|
Set<Type> |
getAllTypes()
Returns all types that this wrapped type is an instance of, i.e.
|
@Nullable TypeInfo |
getComponentType()
Returns the array component type of this type, if applicable.
|
@Nullable TypeInfo |
getEnclosingType()
Returns a type info of the enclosing type (i.e.
|
Class<?> |
getSafeToReadClass()
Returns a
Class object of the wrapped type which is safe for reading. |
Type |
getType() |
@Nullable Class<?> |
getTypeArgumentAsClass(int index)
Returns the type argument for the given index as Class object, provided that the type is a parameterized type
and that the index is valid.
|
@Nullable TypeInfo |
getTypeArgumentInfo(int index)
Returns the type argument at the given index for the wrapped type, provided that the type is a parameterized type
and that the index is valid.
|
int |
hashCode() |
protected Type |
inferTypeForNoArgsConstructor()
Called in the no-args constructor, allowing to infer the type through some other way.
|
boolean |
isAssignableFrom(Class<?> clazz)
Returns whether the class represented by this type info is equal or assignable from the given class.
|
static TypeInfo |
of(Field field)
Creates a new instance wrapping the generic type of the given field.
|
static TypeInfo |
of(Type type)
Creates a new instance wrapping the given type.
|
TypeInfo |
resolve(Type type)
Resolves the given type if possible, based on information from the wrapped Type of this instance.
|
@Nullable TypeInfo |
resolveSuperclass(Class<?> clazz)
Returns the given class' including its type arguments if it is a superclass of this wrapped type.
|
@Nullable Class<?> |
toClass()
Returns a
Class object of the wrapped type which is safe for writing to. |
String |
toString() |
void |
visitAllTypes(Consumer<Type> typeVisitor)
Invokes the given consumer for each type that this wrapped type can be assigned to (i.e.
|
public TypeInfo(Type type)
type - the type to wrap (e.g. coming from Field.getGenericType())protected TypeInfo()
inferTypeForNoArgsConstructor().TypeReferencepublic static TypeInfo of(Type type)
type - the type to wrappublic static TypeInfo of(Field field)
field - the field whose generic type should be usedpublic Type getType()
@Nullable public @Nullable TypeInfo getTypeArgumentInfo(int index)
Examples for index = 0:
type = String -> result = nulltype = List<String> -> result = String.classtype = Map<List<Integer>, String> -> result = List<Integer>type = List -> result = nullindex - the index of the type parameter to get (0-based)@Nullable public @Nullable Class<?> getTypeArgumentAsClass(int index)
safe-to-write Class.
Examples for index = 0:
type = String -> result = nulltype = List<String> -> result = String.classtype = Map<List<Integer>, String> -> result = List.classtype = List<T> -> result = nulltype = List -> result = nullindex - the index of the type parameter to get (0-based)@Nullable public @Nullable Class<?> toClass()
Class object of the wrapped type which is safe for writing to. In other words, if
this instance wraps the Type of a field, an object of the same Class as returned by this method can
be set to the field.
Examples:
type = String -> result = String.classtype = List<String> -> result = List.classtype = ? super Integer -> result = Integer.classtype = ? extends Comparable -> result = nullpublic Class<?> getSafeToReadClass()
Class object of the wrapped type which is safe for reading. For example, if this instance
wraps the Type of a field, then the value on the field is guaranteed to be of the Class type returned by this
method (unless the value is null). The returned Class is as specific as possible.
Examples:
type = String -> result = String.classtype = List<String> -> result = List.classtype = ? super Integer -> result = Object.classtype = ? extends Comparable -> result = Comparable.classpublic boolean equalTo(Class<?> clazz)
toClass()), is equal to the given class.
Examples:
new TypeInfo(String.class).equalTo(String.class) // truenew TypeReference<List<String>>() { }.equalTo(List.class) // truenew TypeReference<List<String>>() { }.equalTo(ArrayList.class) // falseclazz - the class to check for equalitypublic boolean isAssignableFrom(Class<?> clazz)
toClass() checks with Class.isAssignableFrom(java.lang.Class<?>) whether the given class
is equal or an extension of this type info's class.
Examples:
new TypeInfo(String.class).isAssignableFrom(Double.class) // falsenew TypeReference<List<String>>() { }.isAssignableFrom(List.class) // truenew TypeReference<List<String>>() { }.isAssignableFrom(ArrayList.class) // trueclazz - the class to check with@Nullable public @Nullable TypeInfo getEnclosingType()
@Nullable public @Nullable TypeInfo getComponentType()
public TypeInfo resolve(Type type)
Optional<String> and the input type is the Optional's T type,
String will be returned.
Supported actions:
T to StringMap<K, V> to Map<String, Long>.T[] to List<String>[]? super T to ? super Serializable
type - the type to resolve@Nullable public @Nullable TypeInfo resolveSuperclass(Class<?> clazz)
clazz - the desired superclass to get the info forpublic Set<Type> getAllTypes()
public Set<TypeInfo> getAllTypeInfos()
public void visitAllTypes(Consumer<Type> typeVisitor)
typeVisitor - callback run for each type that this wrapped type can be assigned toprotected Type inferTypeForNoArgsConstructor()
Copyright © 2023. All rights reserved.