01: // Copyright (c) 1997 Per M.A. Bothner.
02: // This is free software; for terms and warranty disclaimer see ./COPYING.
03:
04: package gnu.bytecode;
05:
06: import java.io.InputStream;
07: import java.io.IOException;
08: import java.io.FileInputStream;
09:
10: /** Class to read a ClassType from a DataInputStream (.class file).
11: * @author Per Bothner
12: */
13:
14: public class dump extends ClassFileInput {
15: ClassTypeWriter writer;
16:
17: public dump(InputStream str) throws IOException, ClassFormatError {
18: super (str);
19: this .ctype = new ClassType();
20: writer = new ClassTypeWriter(ctype, System.out, 0);
21: if (!readHeader())
22: throw new ClassFormatError("invalid magic number");
23: readConstants();
24: readClassInfo();
25: readFields();
26: readMethods();
27: readAttributes(ctype);
28:
29: writer.printClassInfo();
30: writer.printFields();
31: writer.printMethods();
32: printAttributes();
33: writer.flush();
34: }
35:
36: public ConstantPool readConstants() throws IOException {
37: ctype.constants = super .readConstants();
38: if (writer.printConstants)
39: writer.printConstantPool();
40: return ctype.constants;
41: }
42:
43: public Attribute readAttribute(String name, int length,
44: AttrContainer container) throws IOException {
45: return super .readAttribute(name, length, container);
46: }
47:
48: public void printAttributes() {
49: AttrContainer attrs = ctype;
50: writer.println();
51: writer.print("Attributes (count: ");
52: writer.print(Attribute.count(attrs));
53: writer.println("):");
54: writer.printAttributes(attrs);
55: }
56:
57: /** Reads a .class file, and prints out the contents to System.out.
58: * Very rudimentary - prints out the constant pool, and field and method
59: * names and types, but only minimal attributes (i.e. no dis-assembly yet).
60: * @param args One argument - the name of a .class file.
61: */
62: public static void main(String[] args) {
63: if (args.length == 0)
64: usage();
65: String filename = args[0];
66: try {
67: java.io.InputStream inp = new FileInputStream(filename);
68: new dump(inp);
69: } catch (java.io.FileNotFoundException e) {
70: System.err.println("File " + filename + " not found");
71: System.exit(-1);
72: } catch (java.io.IOException e) {
73: System.err.println(e);
74: System.exit(-1);
75: }
76: }
77:
78: public static void usage() {
79: System.err.println("Usage: foo.class");
80: System.exit(-1);
81: }
82: }
|