public class Types extends Object
Constructor | Description |
---|---|
Types() |
Modifier and Type | Method | Description |
---|---|---|
static Type |
bind(Type t,
GenericDeclaration decl,
ParameterizedType args) |
Replaces the type variables in
t by its actual values. |
static ParameterizedType |
createParameterizedType(Class rawType,
Type... arguments) |
Returns the
Type object that represents clazz<T1,T2,T3> . |
static <T> Class<T> |
erasure(Type t) |
Returns the
Class representation of the given type. |
static Type |
getBaseClass(Type type,
Class baseType) |
Gets the parameterization of the given base type.
|
static Type |
getComponentType(Type t) |
Gets the component type of the array.
|
static Type |
getTypeArgument(Type type,
int i) |
Gets the i-th type argument from a parameterized type.
|
static Type |
getTypeArgument(Type type,
int i,
Type defaultValue) |
Gets the i-th type argument from a parameterized type.
|
static String |
getTypeName(Type type) |
Gets the display name of the type object
|
static boolean |
isArray(Type t) |
Checks if the type is an array type.
|
static boolean |
isArrayButNotByteArray(Type t) |
Checks if the type is an array type but not byte[].
|
static boolean |
isOverriding(Method method,
Class base) |
Tests if the given method overrides another method defined in 'base' (or its super types.)
|
static boolean |
isPrimitive(Type type) |
Checks if the given type is a primitive type.
|
static boolean |
isSubClassOf(Type sub,
Type sup) |
Checks if
sub is a sub-type of sup . |
public static Type bind(Type t, GenericDeclaration decl, ParameterizedType args)
t
by its actual values.
This is primarily used to resolve a method of a generic type to a concrete signature.
For example, binding Collection<T>
with T=List<String>
results in
Collection<List<String>>
.
decl
- provides a list of type variables. See GenericDeclaration.getTypeParameters()
args
- actual arguments. See ParameterizedType.getActualTypeArguments()
public static Type getBaseClass(Type type, Class baseType)
For example, given the following
This method works like this:interface Foo extends List > {} interface Bar extends Foo
{}
getBaseClass( Bar, List ) = List getBaseClass( Bar, Foo ) = Foo
getBaseClass( Foo extends Number>, Collection ) = Collection > getBaseClass( ArrayList extends BigInteger>, List ) = List extends BigInteger>
type
- The type that derives from baseType
baseType
- The class whose parameterization we are interested in.baseType
in type
.
or null if the type is not assignable to the base type.public static String getTypeName(Type type)
public static boolean isSubClassOf(Type sub, Type sup)
sub
is a sub-type of sup
.public static <T> Class<T> erasure(Type t)
Class
representation of the given type.
This corresponds to the notion of the erasure in JSR-14.
It made me realize how difficult it is to define the common navigation layer for two different underlying reflection library. The other way is to throw away the entire parameterization and go to the wrapper approach.
public static ParameterizedType createParameterizedType(Class rawType, Type... arguments)
Type
object that represents clazz<T1,T2,T3>
.public static boolean isArray(Type t)
public static boolean isArrayButNotByteArray(Type t)
public static Type getComponentType(Type t)
t
- must be an array.public static Type getTypeArgument(Type type, int i)
Unlike getTypeArgument(Type, int, Type)
, this method
throws IllegalArgumentException
if the given type is
not parameterized.
public static Type getTypeArgument(Type type, int i, Type defaultValue)
For example, getTypeArgument([Map<Integer,String>],0)=Integer
If the given type is not a parameterized type, returns the specified
default value.
This is convenient for handling raw types and parameterized types uniformly.
IndexOutOfBoundsException
- If i is out of range.public static boolean isPrimitive(Type type)
Copyright © 2019. All rights reserved.