MalformedParameterizedTypeException.
16.9.5. String Representations of Type Objects
None of the interfaces described above define the toString method, or any other general way to obtain a
string representation of a typewith the exception of TypeVariable, which has the getName method.
However, all the type objects will have a toString method defined. With no specification of what
toString should return for Type objects, you cannot rely on toString to give a reasonable
representation of the type. If you want a string representation of a type, you will need to assemble it yourself
from the information available. For example, if a WildcardType object has no lower bound and an upper
bound of X, then the wildcard is "?extendsX". For a ParameterizedType you can construct the string
representation using the raw type and the actual type parameter types.
Exercise 16.9: Use reflection to write a program that will print a full declaration of a named class, including
everything except the import statements, comments, and code for initializers, constructors, and methods. The
member declarations should appear just as you would write them. You will need to use all the reflection
classes you have seen. Also note that the toString methods of many of the reflection objects will not
provide the information you want in the correct format, so you will need to piece together the individual
pieces of information.
16.10. Arrays
An array is an object but has no membersthe implicit length "field" of an array is not an actual field. Asking
the Class object of an array for fields, methods, or constructors will all yield empty arrays. To create arrays
and to get and set the values of elements stored in an array, you can use the static methods of the Array
class. You can create arrays with either of two newInstance methods.
public static ObjectnewInstance(Class<?> componentType, int
length)
Returns a reference to a new array of the specified length and with
component type componentType.
public static ObjectnewInstance(Class<?> componentType, int[]
dimensions)
Returns a reference to a multidimensional array, with dimensions as specified
by the elements of the dimensions array and with the component type
componentType. If the dimensions array is empty or has a length
greater than the number of dimensions allowed by the implementation
(typically 255), an IllegalArgumentException is thrown.
For primitive types, use the class literal to obtain the Class object. For example, use byte.class to create
a byte array. The statement
byte[] ba = (byte[]) Array.newInstance(byte.class, 13);
is equivalent to
byte[] ba = new byte[13];