Java Reference
In-Depth Information
extended. As an example, this program prints the complete type hier-
archy of the type represented by a string passed as an argument:
import java.lang.reflect.*;
public class TypeDesc {
public static void main(String[] args) {
TypeDesc desc = new TypeDesc();
for (String name : args) {
try {
Class<?> startClass = Class.forName(name);
desc.printType(startClass, 0, basic);
} catch (ClassNotFoundException e) {
System.err.println(e); // report the error
}
}
}
// by default print on standard output
private java.io.PrintStream out = System.out;
// used in printType() for labeling type names
private static String[]
basic = { "class", "interface",
"enum", "annotation" },
supercl = { "extends", "implements" },
iFace = { null, "extends" };
private void printType(
Type type, int depth, String[] labels)
{
if (type == null) // stop recursion -- no supertype
return;
// turn the Type into a Class object
Class<?> cls = null;
if (type instanceof Class<?>)
cls = (Class<?>) type;
else if (type instanceof ParameterizedType)
 
Search WWH ::




Custom Search