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