Java Reference
In-Depth Information
This program loops through the names provided on the command line,
obtains the
Class
object for each named type and invokes
printType
on
each of them. It must do this inside a
TRy
block in case there is no class
of the specified name. Here is its output when invoked on the utility
class
java.util.HashMap
:
class java.util.HashMap<K, V>
implements java.util.Map<K, V>
implements java.lang.Cloneable
implements java.io.Serializable
extends java.util.AbstractMap<K, V>
implements java.util.Map<K, V>
extends java.lang.Object
After the
main
method is the declaration of the output stream to use, by
default
System.out
. The
String
arrays are described shortly.
The
printType
method prints its own
type
parameter's description and
then invokes itself recursively to print the description of
type
's super-
types. Because we initially have general
Type
objects, we first have to
convert them to
Class
objects. The
depth
parameter keeps track of how
far up the type hierarchy it has climbed, indenting each description line
according to its depth. The depth is incremented at each recursion level.
The
labels
array specifies how to label the class
labels[0]
is the label if
the type is a class;
labels[1]
is for interfaces;
labels[2]
for enums; and
labels
[3] for annotation types. Note the order in which we check what
kind of type we havean enum type is a class, and an annotation type is
an interface, so we have to check for these more specific kinds of type
first.
Three arrays are defined for these labels:
basic
is used at the top level,
supercl
is used for superclasses, and
iFace
is used for superinterfaces of
interfaces, which extend, not implement, each other. After we print the
right prefix, we use
getCanonicalName
to print the full name of the type.