001: /*
002: *
003: *
004: * Copyright 1990-2007 Sun Microsystems, Inc. All Rights Reserved.
005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License version
009: * 2 only, as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful, but
012: * WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * General Public License version 2 for more details (a copy is
015: * included at /legal/license.txt).
016: *
017: * You should have received a copy of the GNU General Public License
018: * version 2 along with this work; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA
021: *
022: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
023: * Clara, CA 95054 or visit www.sun.com if you need additional
024: * information or have any questions.
025: */
026:
027: package util;
028:
029: import components.*;
030:
031: import java.io.DataInput;
032: import java.io.DataInputStream;
033: import java.io.DataOutput;
034: import java.io.IOException;
035: import java.io.PrintStream;
036: import java.io.InputStream;
037:
038: import java.util.Enumeration;
039: import java.util.Hashtable;
040: import jcc.Const;
041:
042: /*
043: * Reading of class files.
044: * Which is different from reading a class from a multi-class file.
045: * But not much.
046: * Much of the real work is done in components.ClassInfo.
047: *
048: * There would be stuff for writing class files here, too, but we've
049: * never been called upon to do that operation.
050: *
051: * Also includes a small test program for reading and dumping
052: * a class file.
053: */
054:
055: public class ClassFile {
056: // Input file name
057: public String fileName;
058: public boolean didRead = false;
059: public Exception failureMode; // only for didRead == false.
060: public boolean verbose = false;
061: public ClassInfo clas;
062: PrintStream log = System.out;
063: InputStream istream;
064:
065: // Identifies the classfile format
066: int minorID, majorID;
067:
068: public ClassFile(String name, boolean v) {
069: fileName = name;
070: verbose = v;
071: clas = new ClassInfo(v);
072: istream = null;
073: }
074:
075: public ClassFile(String name, InputStream i, boolean v) {
076: fileName = name;
077: verbose = v;
078: clas = new ClassInfo(v);
079: istream = i;
080: }
081:
082: // Read in the entire class file
083: public boolean readClassFile(ConstantPool t) {
084: DataInputStream in = null;
085: failureMode = null;
086: done: try {
087: int magic;
088: if (istream == null) {
089: istream = new java.io.BufferedInputStream(
090: new java.io.FileInputStream(fileName));
091: }
092: in = new DataInputStream(istream);
093: if ((magic = in.readInt()) != Const.JAVA_MAGIC) {
094: failureMode = new IOException(Localizer
095: .getString("classfile.bad_magic.ioexception"));
096: break done; // bad magic -- bail immediately.
097: }
098:
099: minorID = in.readShort();
100: majorID = in.readShort();
101: //if (minorID != Const.JAVA_MINOR_VERSION) {
102: // failureMode = new IOException(Localizer.getString("classfile.bad_minor_version_number.ioexception"));
103: // break done; // bad magic -- bail immediately.
104: //}
105: //
106: //if (majorID != Const.JAVA_VERSION) {
107: // failureMode = new IOException(Localizer.getString("classfile.bad_major_version_number.ioexception"));
108: // break done; // bad magic -- bail immediately.
109: //}
110: if (verbose) {
111: log.println(Localizer.getString("classfile._file",
112: fileName)
113: + Localizer.getString(
114: "classfile._magic/major/minor", Integer
115: .toHexString(magic), Integer
116: .toHexString(majorID), Integer
117: .toHexString(minorID)));
118: }
119: clas.read(in, true, t);
120: didRead = true;
121: } catch (Exception e) {
122: failureMode = e;
123: }
124: if (in != null) {
125: try {
126: in.close();
127: } catch (IOException e) {
128: if (didRead == true) {
129: didRead = false;
130: failureMode = e;
131: }
132: }
133: }
134: return didRead;
135: }
136:
137: public void dump(PrintStream o) {
138: o.println(Localizer.getString("classfile.file", fileName));
139: if (!didRead) {
140: o.println(Localizer.getString(
141: "classfile.could_not_be_read_because_of",
142: failureMode));
143: if (failureMode != null) // IMPL_NOTE: consider whether this
144: // should be fixed
145: failureMode.printStackTrace(o);
146: return;
147: }
148: clas.dump(o);
149: }
150:
151: /*
152: * Simple test program.
153: * Parameters: list of class files to read and process.
154: */
155: public static void main(String clist[]) {
156: for (int i = 0; i < clist.length; i++) {
157: ClassFile f = new ClassFile(clist[i], true);
158: if (f.readClassFile(null) != true) {
159: System.out.println(Localizer.getString(
160: "classfile.read_of", clist[i]));
161: f.dump(System.out);
162: continue;
163: }
164: f.clas.countReferences(true);
165: f.dump(System.out);
166: }
167: }
168:
169: }
|