1    
2    /**
3    
4      The author of this software is Ian Kaplan
5      Bear Products International
6      www.bearcave.com
7      iank@bearcave.com
8    
9      Copyright (c) Ian Kaplan, 1999, 2000
10   
11     See copyright file for usage and licensing
12   
13   */
14   
15   /*
16   
17     To generate Java class documentation use
18   
19       javadoc -private -author -d doc javad.java attr/*.java classfile/*.java jconst/*.java util/*.java
20   
21     if you have a UNIX style shell.  If you don't I recomment getting Bash from
22     cygnus.com.  Bash is a UNIX style shell that runs on Windows NT.  Live free
23     or die!
24   
25    */
26   
27   package javad;
28   
29   import java.io.*;
30   //
31   // local utility directory, not to be confused with
32   // java.lang.util.*
33   //
34   import util.*;
35   import classfile.*;
36   
37   /**
38      Dump a compiled Java JVM class file in human readable form.
39   
40   <p>
41      This object is invoked via the constructor, which is passed
42      the file name path.
43   
44    */
45   class jvmDump {
46     private String fileName;
47     private FileInputStream fStream;
48     private boolean fileIsOpen = false;
49   
50   
51     //
52     // jvmDump: class constructor
53     //
54     jvmDump( String name ) { 
55       DataInputStream dStream;
56   
57       fileName = name;
58   
59       dStream = openFile( name );
60       if (dStream != null) {
61         classFile curClassFile;
62   
63         curClassFile = new classFile( dStream );
64         System.out.println();
65         curClassFile.pr();
66         
67         closeFile();
68         fileIsOpen = false;
69       }
70     }  // jvmDump constructor
71   
72   
73     private DataInputStream openFile( String name ) {
74       DataInputStream dStream;
75   
76       // try the file open
77       try {
78         BufferedInputStream bufStream;
79   
80         fStream = new FileInputStream( name );
81         bufStream = new BufferedInputStream( fStream );
82         dStream = new DataInputStream( bufStream );
83         fileIsOpen = true;
84       } catch( Exception e ) {
85         dStream = null;  // file open did not succeed
86   
87         // the constructor can throw either the FileNotFoundException
88         // or the SecurityException
89         if (e instanceof FileNotFoundException) {
90   	errorMessage.errorPrint("could not open file " + name );
91         }
92         else if (e instanceof SecurityException) {
93   	errorMessage.errorPrint("not allowed to open file " + name );
94         }
95         else {
96   	errorMessage.errorPrint(e.toString() + "unexpected exception" );
97         }
98         fileIsOpen = false;
99       } // catch
100  
101      return dStream;
102    } // openFile
103  
104  
105    //
106    // closeFile
107    //
108    private void closeFile() {
109      // we're done with the file, so be a good citizen
110      // and release the file descriptor.
111      try {
112        fStream.close();
113      } catch (Exception e) {
114        if (e instanceof IOException) {
115  	errorMessage.errorPrint( e.getMessage() );
116        }
117        else {
118  	errorMessage.errorPrint(e.toString() + "unexpected exception" );
119        }
120      } // catch
121    } // closeFile
122  
123  
124    /**
125  
126       Use a finalize method to free up the file descriptor.
127       Finalize is always called, even if the object terminates
128       with an exception.
129  
130     */
131    protected void finalize() {
132      if (fileIsOpen) {
133        closeFile();
134      }
135    } // finalize
136  
137  } // jvdDump
138  
139  
140  /** 
141  
142  The <b>javad</b> class contains the <i>main</i> for the <i>javad</i>
143  program.  The <i>javad</i> program reads a Java class file and
144  writes out a Java like source representation of the class that
145  generated the file. 
146  
147  */
148  class main {
149  
150    static void usage() {
151      errorMessage.errorPrint(" this program takes a set of one or more" +
152  			    " .class file names as its argument");
153    }
154  
155    public static void main( String[] args ) {
156  
157      errorMessage.setProgName( "javad" );
158  
159      if (args.length == 0) {
160        usage();
161      }
162      else {
163        for (int i = 0; i < args.length; i++) {
164  	jvmDump dumpObj = new jvmDump( args[i] );
165        }
166      }
167    }
168  } // main
169