Class Types (1.42.3)

public class Types

Utilities for working with Java types.

Inheritance

Object > Types

Static Methods

<T>iterableOf(Object value)

public static Iterable<T> <T>iterableOf(Object value)

Returns an iterable for an input iterable or array 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.

Parameter
NameDescription
valueObject

iterable (extends Iterable) or array value

Returns
TypeDescription
Iterable<T>

iterable

<T>newInstance(Class<T> clazz)

public static T <T>newInstance(Class<T> clazz)

Creates a new instance of the given class by invoking its default constructor.

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.

Parameter
NameDescription
clazzClass<T>
Returns
TypeDescription
T

getArrayComponentType(Type array)

public static Type getArrayComponentType(Type array)

Returns the component type of the given array type, assuming #isArray(Type).

Return type will either be class, parameterized type, generic array type, or type variable, but not a wildcard type.

Parameter
NameDescription
arrayType
Returns
TypeDescription
Type

getBound(WildcardType wildcardType)

public static Type getBound(WildcardType wildcardType)

Returns the only bound of the given wildcard type.

Parameter
NameDescription
wildcardTypeWildcardType

wildcard type

Returns
TypeDescription
Type

only bound or Object.class for none

getIterableParameter(Type iterableType)

public static Type getIterableParameter(Type iterableType)

Returns the type parameter of 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.

Parameter
NameDescription
iterableTypeType

iterable type (must extend Iterable)

Returns
TypeDescription
Type

type parameter, which may be any type

getMapValueParameter(Type mapType)

public static Type getMapValueParameter(Type mapType)

Returns the value type parameter of 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.

Parameter
NameDescription
mapTypeType

map type (must extend Map)

Returns
TypeDescription
Type

type parameter, which may be any type

getRawArrayComponentType(List<Type> context, Type componentType)

public 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.

Parameters
NameDescription
contextList<Type>

context list, ordering from least specific to most specific type context, for example container class and then its field

componentTypeType

array component type or null for Object.class result

Returns
TypeDescription
Class<?>

raw array component type

getRawClass(ParameterizedType parameterType)

public static Class<?> getRawClass(ParameterizedType parameterType)

Returns the raw class for the given parameter type as defined in ParameterizedType#getRawType().

Parameter
NameDescription
parameterTypeParameterizedType

parameter type

Returns
TypeDescription
Class<?>

raw class

getSuperParameterizedType(Type type, Class<?> superClass)

public static ParameterizedType getSuperParameterizedType(Type type, Class<?> superClass)

Returns the parameterized type that is or extends the given type that matches the given super class.

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.

Parameters
NameDescription
typeType

class or parameterized type

superClassClass<?>

super class

Returns
TypeDescription
ParameterizedType

matching parameterized type or null

isArray(Type type)

public static boolean isArray(Type type)

Returns whether the given type is an array.

Parameter
NameDescription
typeType
Returns
TypeDescription
boolean

isAssignableToOrFrom(Class<?> classToCheck, Class<?> anotherClass)

public static boolean isAssignableToOrFrom(Class<?> classToCheck, Class<?> anotherClass)

Returns whether a class is either assignable to or from another class.

Parameters
NameDescription
classToCheckClass<?>

class to check

anotherClassClass<?>

another class

Returns
TypeDescription
boolean

resolveTypeVariable(List<Type> context, TypeVariable<?> typeVariable)

public 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.

In case the type variable can be resolved partially, it will return the partially resolved type variable.

Parameters
NameDescription
contextList<Type>

context list, ordering from least specific to most specific type context, for example container class and then its field

typeVariableTypeVariable<?>

type variable

Returns
TypeDescription
Type

resolved or partially resolved actual type (type variable, class, parameterized type, or generic array type, but not wildcard type) or null if unable to resolve at all

toArray(Collection<?> collection, Class<?> componentType)

public 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.

Parameters
NameDescription
collectionCollection<?>

collection

componentTypeClass<?>

component type (possibly a Java primitive)

Returns
TypeDescription
Object

new array