Recipe 25.6 Printing Class Information


Problem

You want to print all the information about a class, similar to the way javap does.

Solution

Get a Class object, call its getFields( ) and getMethods( ), and print the results.

Discussion

The JDK includes a program called javap , the Java Printer. Sun's JDK version normally prints the outline of a class file a list of its methods and fields but can also print out the Java bytecodes or machine instructions. The Kaffe package did not include a version of javap, so I wrote one and contributed it (see Example 25-8). The Kaffe folk have expanded it somewhat, but it still works basically the same. My version doesn't print the bytecodes; it behaves rather like Sun's behaves when you don't give theirs any command-line options.

The getFields( ) and getMethods( ) methods return arrays of Field and Method, respectively; these are both in package java.lang.reflect. I use a Modifiers object to get details on the permissions and storage attributes of the fields and methods. In many Java implementations, you can bypass this and simply call toString( ) in each Field and Method object (as I do here for Constructors). Doing it this way gives me a bit more control over the formatting.

Example 25-8. MyJavaP.java
import java.lang.reflect.Constructor; import java.lang.reflect.Field; import java.lang.reflect.Method; import java.lang.reflect.Modifier; /**  * JavaP prints structural information about classes.  * For each class, all public fields and methods are listed.  * "Reflectance" is used to look up the information.  */ public class MyJavaP {     /** Simple main program, construct self, process each class name      * found in argv.      */     public static void main(String[] argv) {         MyJavaP pp = new MyJavaP( );         if (argv.length == 0) {             System.err.println("Usage: MyJavaP className [...]");             System.exit(1);         } else for (int i=0; i<argv.length; i++)             pp.doClass(argv[i]);     }     /** Format the fields and methods of one class, given its name.      */     protected void doClass(String className) {         try {             Class c = Class.forName(className);             System.out.println(Modifier.toString(c.getModifiers( )) + ' ' + c + " {");             int mods;             Field fields[] = c.getDeclaredFields( );             for (int i = 0; i < fields.length; i++) {                 if (!Modifier.isPrivate(fields[i].getModifiers( ))                  && !Modifier.isProtected(fields[i].getModifiers( )))                     System.out.println("\t" + fields[i]);             }             Constructor[] constructors = c.getConstructors( );             for (int j = 0; j < constructors.length; j++) {                 Constructor constructor = constructors[j];                 System.out.println("\t" + constructor);                              }             Method methods[] = c.getDeclaredMethods( );             for (int i = 0; i < methods.length; i++) {                 if (!Modifier.isPrivate(methods[i].getModifiers( ))                  && !Modifier.isProtected(methods[i].getModifiers( )))                     System.out.println("\t" + methods[i]);             }             System.out.println("}");         } catch (ClassNotFoundException e) {             System.err.println("Error: Class " +                  className + " not found!");         } catch (Exception e) {             System.err.println(e);         }     } }



Java Cookbook
Java Cookbook, Second Edition
ISBN: 0596007019
EAN: 2147483647
Year: 2003
Pages: 409
Authors: Ian F Darwin

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net