Class Types (1.45.0)

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
Name Description
value Object

iterable (extends Iterable) or array value

Returns
Type Description
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
Name Description
clazz Class<T>
Returns
Type Description
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
Name Description
array Type
Returns
Type Description
Type

getBound(WildcardType wildcardType)

public static Type getBound(WildcardType wildcardType)

Returns the only bound of the given wildcard type.

Parameter
Name Description
wildcardType WildcardType

wildcard type

Returns
Type Description
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
Name Description
iterableType Type

iterable type (must extend Iterable)

Returns
Type Description
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
Name Description
mapType Type

map type (must extend Map)

Returns
Type Description
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
Name Description
context List<Type>

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

componentType Type

array component type or null for Object.class result

Returns
Type Description
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
Name Description
parameterType ParameterizedType

parameter type

Returns
Type Description
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
Name Description
type Type

class or parameterized type

superClass Class<?>

super class

Returns
Type Description
ParameterizedType

matching parameterized type or null

isArray(Type type)

public static boolean isArray(Type type)

Returns whether the given type is an array.

Parameter
Name Description
type Type
Returns
Type Description
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
Name Description
classToCheck Class<?>

class to check

anotherClass Class<?>

another class

Returns
Type Description
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
Name Description
context List<Type>

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

typeVariable TypeVariable<?>

type variable

Returns
Type Description
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
Name Description
collection Collection<?>

collection

componentType Class<?>

component type (possibly a Java primitive)

Returns
Type Description
Object

new array