001: /*
002: * Copyright 2002-2003 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025:
026: package sun.tools.javap;
027:
028: import java.util.*;
029: import java.io.*;
030:
031: /**
032: * Entry point for javap, class file disassembler.
033: *
034: * @author Sucheta Dambalkar (Adopted code from old javap)
035: */
036: public class Main {
037:
038: private Vector classList = new Vector();
039: private PrintWriter out;
040: JavapEnvironment env = new JavapEnvironment();
041: private static boolean errorOccurred = false;
042: private static final String progname = "javap";
043:
044: public Main(PrintWriter out) {
045: this .out = out;
046: }
047:
048: public static void main(String argv[]) {
049: entry(argv);
050: if (errorOccurred) {
051: System.exit(1);
052: }
053: }
054:
055: /**
056: * Entry point for tool if you don't want System.exit() called.
057: */
058: public static void entry(String argv[]) {
059: PrintWriter out = new PrintWriter(new OutputStreamWriter(
060: System.out));
061: try {
062:
063: Main jpmain = new Main(out);
064: jpmain.perform(argv);
065:
066: } finally {
067: out.close();
068: }
069: }
070:
071: /**
072: * Process the arguments and perform the desired action
073: */
074: private void perform(String argv[]) {
075: if (parseArguments(argv)) {
076: displayResults();
077:
078: }
079: }
080:
081: private void error(String msg) {
082: errorOccurred = true;
083: System.err.println(msg);
084: System.err.flush();
085: }
086:
087: /**
088: * Print usage information
089: */
090: private void usage() {
091: java.io.PrintStream out = System.out;
092: out.println("Usage: " + progname + " <options> <classes>...");
093: out.println();
094: out.println("where options include:");
095: out
096: .println(" -c Disassemble the code");
097: out
098: .println(" -classpath <pathlist> Specify where to find user class files");
099: out
100: .println(" -extdirs <dirs> Override location of installed extensions");
101: out
102: .println(" -help Print this usage message");
103: out
104: .println(" -J<flag> Pass <flag> directly to the runtime system");
105: out
106: .println(" -l Print line number and local variable tables");
107: out
108: .println(" -public Show only public classes and members");
109: out
110: .println(" -protected Show protected/public classes and members");
111: out
112: .println(" -package Show package/protected/public classes");
113: out
114: .println(" and members (default)");
115: out
116: .println(" -private Show all classes and members");
117: out
118: .println(" -s Print internal type signatures");
119: out
120: .println(" -bootclasspath <pathlist> Override location of class files loaded");
121: out
122: .println(" by the bootstrap class loader");
123: out
124: .println(" -verbose Print stack size, number of locals and args for methods");
125: out
126: .println(" If verifying, print reasons for failure");
127: out.println();
128: }
129:
130: /**
131: * Parse the command line arguments.
132: * Set flags, construct the class list and create environment.
133: */
134: private boolean parseArguments(String argv[]) {
135: for (int i = 0; i < argv.length; i++) {
136: String arg = argv[i];
137: if (arg.startsWith("-")) {
138: if (arg.equals("-l")) {
139: env.showLineAndLocal = true;
140: } else if (arg.equals("-private") || arg.equals("-p")) {
141: env.showAccess = env.PRIVATE;
142: } else if (arg.equals("-package")) {
143: env.showAccess = env.PACKAGE;
144: } else if (arg.equals("-protected")) {
145: env.showAccess = env.PROTECTED;
146: } else if (arg.equals("-public")) {
147: env.showAccess = env.PUBLIC;
148: } else if (arg.equals("-c")) {
149: env.showDisassembled = true;
150: } else if (arg.equals("-s")) {
151: env.showInternalSigs = true;
152: } else if (arg.equals("-verbose")) {
153: env.showVerbose = true;
154: } else if (arg.equals("-v")) {
155: env.showVerbose = true;
156: } else if (arg.equals("-h")) {
157: error("-h is no longer available - use the 'javah' program");
158: return false;
159: } else if (arg.equals("-verify")) {
160: error("-verify is no longer available - use 'java -verify'");
161: return false;
162: } else if (arg.equals("-verify-verbose")) {
163: error("-verify is no longer available - use 'java -verify'");
164: return false;
165: } else if (arg.equals("-help")) {
166: usage();
167: return false;
168: } else if (arg.equals("-classpath")) {
169: if ((i + 1) < argv.length) {
170: env.classPathString = argv[++i];
171: } else {
172: error("-classpath requires argument");
173: usage();
174: return false;
175: }
176: } else if (arg.equals("-bootclasspath")) {
177: if ((i + 1) < argv.length) {
178: env.bootClassPathString = argv[++i];
179: } else {
180: error("-bootclasspath requires argument");
181: usage();
182: return false;
183: }
184: } else if (arg.equals("-extdirs")) {
185: if ((i + 1) < argv.length) {
186: env.extDirsString = argv[++i];
187: } else {
188: error("-extdirs requires argument");
189: usage();
190: return false;
191: }
192: } else if (arg.equals("-all")) {
193: env.showallAttr = true;
194: } else {
195: error("invalid flag: " + arg);
196: usage();
197: return false;
198: }
199: } else {
200: classList.addElement(arg);
201: env.nothingToDo = false;
202: }
203: }
204: if (env.nothingToDo) {
205: System.out
206: .println("No classes were specified on the command line. Try -help.");
207: errorOccurred = true;
208: return false;
209: }
210: return true;
211: }
212:
213: /**
214: * Display results
215: */
216: private void displayResults() {
217: for (int i = 0; i < classList.size(); i++) {
218: String Name = (String) classList.elementAt(i);
219: InputStream classin = env.getFileInputStream(Name);
220:
221: try {
222: JavapPrinter printer = new JavapPrinter(classin, out,
223: env);
224: printer.print(); // actual do display
225:
226: } catch (IllegalArgumentException exc) {
227: error(exc.getMessage());
228: }
229: }
230: }
231: }
|