001package edu.pdx.cs410J.reflect;
002
003import java.io.*;
004import java.lang.reflect.*;
005
006/**
007 * This program uses Java reflection to describe a class, its methods,
008 * and fields.
009 */
010public class DescribeClass {
011  private static PrintStream out = System.out;
012  private static PrintStream err = System.err;
013
014  /**
015   * Prints a description of a field
016   */
017  private static void describe(Field f) {
018    out.print("  ");
019    out.print(Modifier.toString(f.getModifiers()));
020    out.print(" ");
021    out.print(f.getType().getName());
022    out.print(" ");
023    out.println(f.getName());
024  }
025
026  /**
027   * Prints a description of a method
028   */
029  private static void describe(Method m) {
030    out.print("  ");
031    out.print(Modifier.toString(m.getModifiers()));
032    out.print(" ");
033    out.print(m.getReturnType().getName());
034    out.print(" ");
035    out.print(m.getName());
036    out.print("(");
037
038    Class[] params = m.getParameterTypes();
039    for (int i = 0; i < params.length; i++) {
040      out.print(params[i].getName());
041      if (i < params.length - 1)
042        out.print(", ");
043    }
044    out.println(")");
045
046    Class[] exs = m.getExceptionTypes();
047    if (exs.length > 0) {
048      out.print("    throws ");
049      for (int i = 0; i < exs.length; i++) {
050        out.print(exs[i].getName());
051        if (i < exs.length - 1)
052          out.print(", ");
053      }
054      out.println("");
055    }
056  }
057
058  /**
059   * Prints a description of a class
060   */
061  private static void describe(Class c) {
062    out.print(Modifier.toString(c.getModifiers()));
063    out.print(" ");
064    out.print((c.isInterface() ? "interface " : "class "));
065    out.println(c.getName());
066
067    Class superclass = c.getSuperclass();
068    if (superclass != null) {
069      out.print("  extends ");
070      out.println(superclass.getName());
071    }
072
073    Class[] ifaces = c.getInterfaces();
074    if (ifaces.length > 0) {
075      out.print("  implements ");
076      for (int i = 0; i < ifaces.length; i++) {
077        out.print(ifaces[i].getName());
078        if (i < ifaces.length - 1)
079          out.print(", ");
080      }
081      out.println("");
082    }
083
084    out.println("{");
085
086    Field[] fields = c.getDeclaredFields();
087    for (int i = 0; i < fields.length; i++) {
088      describe(fields[i]);
089    }
090
091    out.println("");
092
093    Method[] methods = c.getDeclaredMethods();
094    for (int i = 0; i < methods.length; i++) {
095      describe(methods[i]);
096    }
097
098    out.println("}");
099
100  }
101
102  /**
103   * Main program that reads the name of a class from the command
104   * line, loads it, and prints out a description of it.
105   */
106  public static void main(String[] args) {
107    String className = args[0];
108    try {
109      Class c = Class.forName(className);
110      describe(c);
111
112    } catch (ClassNotFoundException ex) {
113      err.println("Could not load " + className);
114    }
115  }
116
117}