Reflector.java

import java.lang.reflect.*;


/**
This class prints (to System.out) a description of the public
constructors, methods, and fields of the class name passed on
its command line.
*/
class Reflector
{
    /**
    Prints (to System.out) a description of the constructors, 
    methods, and fields of the class whose name is specified on
    the command line.
    */
    public static void main(String argv[])
    {
        String classname;

        if (argv.length == 1)
            classname = argv[0];
        else
            classname = "java.lang.Double";

        printClassDef(classname);
    }


    /**
    Prints (to System.out) a description of the constructors, 
    methods, and fields of a class 

    @param classname the name of the class whose description will 
        be printed
    */
    public static void printClassDef(String classname)
    {
        try {
            Class c1 = Class.forName(classname);

            Class c2 = c1.getSuperclass();
            System.out.print("class " + classname);

            if (c2 != null && c2 != Object.class)
                System.out.print("\n\textends " + c2.getName());

            Class[] interfaces = c1.getInterfaces();
            if (interfaces.length > 0) {
                System.out.print("\n\timplements");
                for (int i=0; i<interfaces.length; i++) {
                    if (i == 0)
                        System.out.print(" ");
                    else
                        System.out.print(",");
                    System.out.print(interfaces[i].getName());
                    }
                }

            System.out.println("\n{");
            printConstructors(c1);
            printMethods(c1);
            printFields(c1);
            System.out.println("}");
            }
        catch(Exception e) {
            e.printStackTrace();
            }
    }


    /**
    Prints all constructors of a class.

    @param c a Class object
    */
    public static void printConstructors(Class c)
    {
        Constructor cons[] = c.getDeclaredConstructors();

        System.out.println("\n\t// Constructors:");
        for (int i=0; i<cons.length; i++) {
            Constructor con = cons[i];
            String name = con.getName();
            System.out.print("\t");
            System.out.print(Modifier.toString(con.getModifiers()));
            System.out.print(" "+name+"(");
            Class ptypes[] = con.getParameterTypes();
            for (int j=0; j<ptypes.length; j++) {
                if (j > 0)
                    System.out.print(",");
                System.out.print(ptypes[j].getName());
                }
            System.out.println(");");
            }
    }


    /**
    Prints all methods of a class.

    @param c a Class object
    */
    public static void printMethods(Class c)
    {
        Method meths[] = c.getDeclaredMethods();

        System.out.println("\n\t// Methods:");
        for (int i=0; i<meths.length; i++) {
            Method m = meths[i];
            Class rtype = m.getReturnType();
            String name = m.getName();
            System.out.print("\t");
            System.out.print(Modifier.toString(m.getModifiers()));
            System.out.print(" "+rtype.getName()+" "+name+"(");
            Class ptypes[] = m.getParameterTypes();
            for (int j=0; j<ptypes.length; j++) {
                if (j > 0)
                    System.out.print(",");
                if (ptypes[j].isArray())
                    System.out.print(ptypes[j].getComponentType()+"[]");
                else
                    System.out.print(ptypes[j]);
                }
            System.out.println(");");
            }
    }


    /**
    Prints all fields of a class.

    @param c a Class object
    */
    public static void printFields(Class c)
    {
        Field fields[] = c.getDeclaredFields();

        System.out.println("\n\t// Fields:");
        for (int i=0; i<fields.length; i++) {
            Field f = fields[i];
            Class type = f.getType();
            String name = f.getName();
            System.out.print("\t");
            String mod = Modifier.toString(f.getModifiers());
            if (!mod.equals(""))
                System.out.print(mod+" ");
            if (type.isArray())
                System.out.print(type.getComponentType()+
                    "[]" + " " + name + ";");
            else
                System.out.print(type.getName() + " " + name + ";");
            System.out.println();
            }
    }
}