KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > gnu > bytecode > dump


1 // Copyright (c) 1997 Per M.A. Bothner.
2
// This is free software; for terms and warranty disclaimer see ./COPYING.
3

4 package gnu.bytecode;
5 import java.io.InputStream JavaDoc;
6 import java.io.IOException JavaDoc;
7 import java.io.FileInputStream JavaDoc;
8
9 /** Class to read a ClassType from a DataInputStream (.class file).
10  * @author Per Bothner
11  */

12
13 public class dump extends ClassFileInput
14 {
15   ClassTypeWriter writer;
16
17   public dump (InputStream JavaDoc str)
18        throws IOException JavaDoc, ClassFormatError JavaDoc
19   {
20     super(str);
21     this.ctype = new ClassType();
22     writer = new ClassTypeWriter (ctype, System.out, 0);
23     if (!readHeader())
24       throw new ClassFormatError JavaDoc("invalid magic number");
25     readConstants();
26     readClassInfo();
27     readFields();
28     readMethods();
29     readAttributes(ctype);
30
31     writer.printClassInfo();
32     writer.printFields();
33     writer.printMethods();
34     printAttributes ();
35     writer.flush();
36   }
37
38   public ConstantPool readConstants () throws IOException JavaDoc
39   {
40     ctype.constants = super.readConstants();
41     if (writer.printConstants)
42       writer.printConstantPool();
43     return ctype.constants;
44   }
45
46   public Attribute readAttribute (String JavaDoc name, int length, AttrContainer container)
47     throws IOException JavaDoc
48   {
49     return super.readAttribute (name, length, container);
50   }
51
52   public void printAttributes ()
53   {
54     AttrContainer attrs = ctype;
55     writer.println();
56     writer.print("Attributes (count: ");
57     writer.print(Attribute.count(attrs));
58     writer.println("):");
59     writer.printAttributes (attrs);
60   }
61
62   /** Reads a .class file, and prints out the contents to System.out.
63    * Very rudimentary - prints out the constant pool, and field and method
64    * names and types, but only minimal attributes (i.e. no dis-assembly yet).
65    * @param args One argument - the name of a .class file.
66    */

67   public static void main (String JavaDoc[] args)
68   {
69     if (args.length == 0)
70       usage();
71     String JavaDoc filename = args[0];
72     try
73       {
74     java.io.InputStream JavaDoc inp = new FileInputStream JavaDoc(filename);
75     new dump(inp);
76       }
77     catch (java.io.FileNotFoundException JavaDoc e)
78       {
79     System.err.println("File "+filename+" not found");
80     System.exit(-1);
81       }
82     catch (java.io.IOException JavaDoc e)
83       {
84     System.err.println(e);
85     System.exit(-1);
86       }
87   }
88
89   public static void usage()
90   {
91     System.err.println("Usage: foo.class");
92     System.exit(-1);
93   }
94 }
95
96
Popular Tags