of length one, containing String.class as its only element.
The getOwnerType method (which perhaps would have been better called geTDeclaringType) returns
the Type object for the type in which this ParameterizedType object is a member. If this is not a
member of another type then null is returned. This is analogous to the Class.getDeclaringClass
method except that a Type object is returned.
Like TypeVariable objects, ParameterizedType objects are created on demand and are not always
the same object, so you should use equals not == to check for equivalent parameterized type objects. Also,
when a parameterized type is created, all its type arguments are also created, and this applies recursively. Both
of the above methods will sometimes throw either TypeNotFoundException or
MalformedParameterizedTypeException.
Finally, ParameterizedType also has the method getrawType, which returns the Class object for the
raw type of the parameterized type. For example, if getrawType were invoked on the parameterized type
List
or interface, getrawType returns a Type instance rather than a Class<?>, so a cast must be applied, as
you saw in the TypeDesc program.
16.9.3. Wildcards
A wildcard type parameter is represented by an instance that implements the WildcardType interface. For
example, given a parameterized type for List<?extendsNumber>, getActualTypeArguments will
give an array of length one containing a WildcardType object representing "?extendsNumber".
WildcardType has two methods: getUpperBounds and getLowerBounds, which return Type
arrays representing the upper and lower bounds of the wildcard, respectively. If no upper bound was specified,
the upper bound is Object. If a wildcard has no lower bound, getLowerBounds returns an empty array.
As with TypeVariable, the type objects for bounds are created on demand, so
TypeNotPresentException or MalformedParameterizedTypeException may be thrown.
16.9.4. Generic Arrays
The last of the type related interfaces is GenericArrayType. This represents array types in which the
component type is a parameterized type or a type variable.[4]GenericArrayType has a single method,
getGenericComponentType, which returns the Type for the component type of the array, which will be
either a ParameterizedType or a TypeVariable. For example, for a List
getGenericType would return a GenericArrayType object whose getComponentType would
return a ParameterizedType for List
[4] You may recall that you cannot create such an array, but you are permitted to declare
variables of that type. The actual array creation can use only an unbounded wildcard type,
such as new List<?>[1]. When you first assign the new array to a more specific variable,
such as a List<String>[], you will get an "unchecked" warning because the compiler
cannot guarantee that the current or future contents of the array will actually be
List<String> objects. Such arrays are inherently unsafe and should be used with extreme
cautionyou should generally not create methods that return such arrays or take them as
parameters.
The component type object gets created when getGenericComponentType is invoked, so as you might
expect, you may get a TypeNotPresentException or