public class Types extends Object
Modifier and Type | Method and Description |
---|---|
static Type |
getArrayComponentType(Type array)
Returns the component type of the given array type, assuming
isArray(Type) . |
static Type |
getBound(WildcardType wildcardType)
Returns the only bound of the given wildcard type.
|
static Type |
getIterableParameter(Type iterableType)
Returns the type parameter of
Iterable that is assignable from the given iterable type. |
static Type |
getMapValueParameter(Type mapType)
Returns the value type parameter of
Map that is assignable from the given map type. |
static Class<?> |
getRawArrayComponentType(List<Type> context,
Type componentType)
Returns the raw array component type to use -- for example for the first parameter of
Array.newInstance(Class, int) -- for the given component type. |
static Class<?> |
getRawClass(ParameterizedType parameterType)
Returns the raw class for the given parameter type as defined in
ParameterizedType.getRawType() . |
static ParameterizedType |
getSuperParameterizedType(Type type,
Class<?> superClass)
Returns the parameterized type that is or extends the given type that matches the given super
class.
|
static boolean |
isArray(Type type)
Returns whether the given type is an array.
|
static boolean |
isAssignableToOrFrom(Class<?> classToCheck,
Class<?> anotherClass)
Returns whether a class is either assignable to or from another class.
|
static <T> Iterable<T> |
iterableOf(Object value)
Returns an iterable for an input iterable or array value.
|
static <T> T |
newInstance(Class<T> clazz)
Creates a new instance of the given class by invoking its default constructor.
|
static Type |
resolveTypeVariable(List<Type> context,
TypeVariable<?> typeVariable)
Resolves the actual type of the given type variable that comes from a field type based on the
given context list.
|
static Object |
toArray(Collection<?> collection,
Class<?> componentType)
Returns a new array of the given component type (possibly a Java primitive) that is a copy of
the content of the given collection.
|
public static ParameterizedType getSuperParameterizedType(Type type, Class<?> superClass)
For example, if the input type is HashMap<String,Integer>
and the input super class
is Map.class
, it will return the extended parameterized type Map
, but which
retains the actual type information from the original HashMap
.
type
- class or parameterized typesuperClass
- super classnull
public static boolean isAssignableToOrFrom(Class<?> classToCheck, Class<?> anotherClass)
classToCheck
- class to checkanotherClass
- another classpublic static <T> T newInstance(Class<T> clazz)
The given class must be public and must have a public default constructor, and must not be an array or an interface or be abstract. If an enclosing class, it must be static.
public static boolean isArray(Type type)
public static Type getArrayComponentType(Type array)
isArray(Type)
.
Return type will either be class, parameterized type, generic array type, or type variable, but not a wildcard type.
ClassCastException
- if isArray(Type)
is falsepublic static Class<?> getRawClass(ParameterizedType parameterType)
ParameterizedType.getRawType()
.parameterType
- parameter typepublic static Type getBound(WildcardType wildcardType)
wildcardType
- wildcard typeObject.class
for nonepublic static Type resolveTypeVariable(List<Type> context, TypeVariable<?> typeVariable)
In case the type variable can be resolved partially, it will return the partially resolved type variable.
context
- context list, ordering from least specific to most specific type context, for
example container class and then its fieldtypeVariable
- type variablenull
if unable to resolve at allpublic static Class<?> getRawArrayComponentType(List<Type> context, Type componentType)
Array.newInstance(Class, int)
-- for the given component type.context
- context list, ordering from least specific to most specific type context, for
example container class and then its fieldcomponentType
- array component type or null
for Object.class
resultpublic static Type getIterableParameter(Type iterableType)
Iterable
that is assignable from the given iterable type.
For example, for the type ArrayList<Integer>
-- or for a class that extends ArrayList<Integer>
-- it will return Integer
.
iterableType
- iterable type (must extend Iterable
)public static Type getMapValueParameter(Type mapType)
Map
that is assignable from the given map type.
For example, for the type Map<String, Integer>
-- or for a class that extends Map<String, Integer>
-- it will return Integer
.
mapType
- map type (must extend Map
)public static <T> Iterable<T> iterableOf(Object value)
If the input value extends Iterable
, it will just return the input value. Otherwise,
it will return an iterable that can handle arrays of primitive and non-primitive component
type.
value
- iterable (extends Iterable
) or array valuepublic static Object toArray(Collection<?> collection, Class<?> componentType)
collection
- collectioncomponentType
- component type (possibly a Java primitive)Copyright © 2011–2022 Google. All rights reserved.