Class Types
-
Nested Class Summary
Nested Classes -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final TypeVisitor
<Type, Class> private static final TypeVisitor
<Type, Types.BinderArg> private static final TypeVisitor
<Class, Void> Implements the logic forerasure(Type)
. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic Type
bind
(Type t, GenericDeclaration decl, ParameterizedType args) Replaces the type variables int
by its actual values.static ParameterizedType
createParameterizedType
(Class rawType, Type... arguments) Returns theType
object that representsclazz<T1,T2,T3>
.static <T> Class
<T> Returns theClass
representation of the given type.private static Type
JDK 5.0 has a bug of createingGenericArrayType
where it shouldn't.static Type
getBaseClass
(Type type, Class baseType) Gets the parameterization of the given base type.static Type
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 objectstatic boolean
Checks if the type is an array type.static boolean
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 ifsub
is a sub-type ofsup
.
-
Field Details
-
baseClassFinder
-
binder
-
eraser
Implements the logic forerasure(Type)
.
-
-
Constructor Details
-
Types
public Types()
-
-
Method Details
-
bind
Replaces the type variables int
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>
withT=List<String>
results inCollection<List<String>>
.- Parameters:
decl
- provides a list of type variables. SeeGenericDeclaration.getTypeParameters()
args
- actual arguments. SeeParameterizedType.getActualTypeArguments()
-
getBaseClass
Gets the parameterization of the given base type.For example, given the following
This method works like this:interface Foo extends Listinvalid input: '<'List > {} interface Bar extends Foo {} getBaseClass( Bar, List ) = Listinvalid input: '<'List getBaseClass( Bar, Foo ) = Foo getBaseClass( Fooinvalid input: '<'? extends Number>, Collection ) = Collectioninvalid input: '<'Listinvalid input: '<'? extends Number>> getBaseClass( ArrayListinvalid input: '<'? extends BigInteger>, List ) = Listinvalid input: '<'? extends BigInteger> - Parameters:
type
- The type that derives frombaseType
baseType
- The class whose parameterization we are interested in.- Returns:
- The use of
baseType
intype
. or null if the type is not assignable to the base type.
-
getTypeName
Gets the display name of the type object- Returns:
- a human-readable name that the type represents.
-
isSubClassOf
Checks ifsub
is a sub-type ofsup
. -
erasure
Returns theClass
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.
-
createParameterizedType
Returns theType
object that representsclazz<T1,T2,T3>
. -
isArray
Checks if the type is an array type. -
isArrayButNotByteArray
Checks if the type is an array type but not byte[]. -
getComponentType
Gets the component type of the array.- Parameters:
t
- must be an array.
-
getTypeArgument
Gets the i-th type argument from a parameterized type.Unlike
getTypeArgument(Type, int, Type)
, this method throwsIllegalArgumentException
if the given type is not parameterized. -
getTypeArgument
Gets the i-th type argument from a parameterized type.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.
- Throws:
IndexOutOfBoundsException
- If i is out of range.
-
isPrimitive
Checks if the given type is a primitive type. -
isOverriding
Tests if the given method overrides another method defined in 'base' (or its super types.) -
fix
JDK 5.0 has a bug of createingGenericArrayType
where it shouldn't. fix that manually to work around the problem. See bug 6202725.
-