Java Reference
In-Depth Information
16.9. Generic Type Inspection
As you saw in Figure 16-1 on page 400 , there are a number of interfaces
to represent the different kinds of types that can exist in your programs.
So far we have focussed on Class objects and Member objects since they
are the more commonly used reflection objects, and we have only men-
tioned the other kinds of Type objects in passing. This section looks at
those other Type interfaces in more detail.
16.9.1. Type Variables
The GenericDeclaration interface, which is implemented by Class , Method ,
and Constructor , has the single method getTypeParameters , which returns
an array of TypeVariable objects. The TypeVariable interface is itself a gen-
eric interface, declared as
interface TypeVariable<D extends GenericDeclaration>
So, for example, the TypeVariable objects Method.getTypeParameter s returns
would be of type TypeVariable<Method> .
Each type variable has a name returned by getName . It also has one or
more upper bounds, obtained as a Type[] from getBounds . Recall that if
there is no explicit upper bound then the upper bound is Object .
The getGenericDeclaration method returns the Type object for the Gen-
ericDeclaration in which the TypeVariable was declared. For example,
the expression TypeVariable.class.getTypeParameters()[0] would yield a
TypeVariable object that represents D in the declaration above. If you in-
voked getGenericDeclaration on this object, it would return the Class ob-
ject for the TypeVariable interface. More generally, for any GenericDeclar-
ation object g with at least one type parameter,
g.getTypeParameters()[i].getGenericDeclaration() == g
 
Search WWH ::




Custom Search