// Purpose.  Walk and display the type hierarchy of the specified classes
                                                       // [Gosling, p304]
import java.lang.reflect.*;

public class ReflectTypeHierarchy {
   private static java.io.PrintStream out = System.out;
   private static String[] 
      basic   = { "class",   "interface"  },
      supercl = { "extends", "implements" },
      // An interface always "extends" other interfaces
      iface   = { null,      "extends"    };

   private static void printType( Class type, int depth, String[] labels ) {
      if (type == null) return;

      // Print this type
      for (int i=0; i < depth; i++)
         out.print( "   " );
      out.print( labels[type.isInterface() ? 1 : 0] + " " );
      out.println( type.getName() );

      // Print all interfaces this class implements
      Class[] interfaces = type.getInterfaces();
      for (int i=0; i < interfaces.length; i++)
         printType( interfaces[i], depth+1,
            type.isInterface() ? iface : supercl );

      // Recurse on the superclass
      printType( type.getSuperclass(), depth+1, supercl );
   }

   public static void main( String[] args ) {
      for (int i=0; i < args.length; i++)
         try {
            Class classDS = Class.forName( args[i] );
            printType( classDS, 0, basic );
         } catch (ClassNotFoundException e) { System.out.println( e ); }
   }
}

// ----- a fully-qualified name is required by the forName() method -----
// C:> java ReflectTypeHierarchy javax.swing.JButton java.io.BufferedReader
// class javax.swing.JButton
//    implements javax.accessibility.Accessible
//    extends javax.swing.AbstractButton
//       implements java.awt.ItemSelectable
//       implements javax.swing.SwingConstants
//       extends javax.swing.JComponent
//          implements java.io.Serializable
//          extends java.awt.Container
//             extends java.awt.Component
//                implements java.awt.image.ImageObserver
//                implements java.awt.MenuContainer
//                implements java.io.Serializable
//                extends java.lang.Object
// class java.io.BufferedReader
//    extends java.io.Reader
//       extends java.lang.Object
