Listing 3: ClassInspector.java — Prints minimal class information, including interfaces implemented

class ClassInspector
{
   public static void inspect(Class c)
   {
      // Determine type of class
      String type;
      if (c.isInterface())
         type = "interface";
      else if (c.isArray())
         type = "array";
      else if (c.isPrimitive())
         type = "primitive";
      else
         type = "class";
      
      // Print name
      System.out.println(type + " " + c.getName());

      // Print component type, if array
      if (type == "array")
         System.out.println("\tarray of " +
                            c.getComponentType().getName());

      // Print Superclass
      Class superClass = c.getSuperclass();
      if (superClass != null)
         System.out.println("\textends " +
                            superClass.getName());

      // Print interfaces implemented, if any
      Class[] interfaces = c.getInterfaces();
      for (int i = 0; i < interfaces.length; ++i)
         System.out.println("\timplements " +
                            interfaces[i].getName());
}

   public static void main(String[] args)
      throws ClassNotFoundException
   {
      inspect(Class.forName("Sub"));
      inspect(Class.forName("Super"));
      inspect(Class.forName("java.lang.Object"));
      inspect(int.class);
      Super[] arr = new Super[2];
      inspect(arr.getClass());
      inspect(Class.forName("Inter"));
   }
}

/* Output:
class Sub
    extends Super
class Super
    extends java.lang.Object
class java.lang.Object
primitive int
array [LSuper;
    array of Super
    extends java.lang.Object
    implements java.lang.Cloneable
    implements java.io.Serializable
interface Inter
*/
— End of Listing —