Java Reference
In-Depth Information
public class SimpleClassDesc {
public static void main(String[] args) {
Class type = null;
try {
type = Class.forName(args[0]);
} catch (ClassNotFoundException e) {
err.println(e);
return;
}
out.print("class " + type.getSimpleName());
Class superclass = type.getSuperclass();
if (superclass != null)
out.println(" extends " +
superclass.getCanonicalName());
else
out.println();
Method[] methods = type.getDeclaredMethods();
for (Method m : methods)
if (Modifier.isPublic(m.getModifiers()))
out.println(" " + m);
}
}
Given the fully qualified name of a class (such
java.lang.String
) the pro-
gram first tries to obtain a
Class
object for that class, using the static
method
Class.forName
. If the class can't be found an exception is thrown
which the program catches and reports. Otherwise, the simple name of
the class
String
, for exampleis printed. Next the
Class
object is asked
for its superclass's
Class
object. As long as the named class is not
Ob-
ject
and is a class rather than an interface,
getSuperclass
will return a
superclass's
Class
object. The name of the super class is printed in full
using
getCanonicalName
(there are a number of ways to name a class as
for all the methods that it declares. The declared methods include all