Tue, 03 Jun 2008 13:26:47 -0700
4075303: Use javap to enquire aboput a specific inner class
4348375: Javap is not internationalized
4459541: "javap -l" shows line numbers as signed short; they should be unsigned
4501660: change diagnostic of -help as 'print this help message and exit'
4776241: unused source file in javap...
4870651: javap should recognize generics, varargs, enum
4876942: javap invoked without args does not print help screen
4880663: javap could output whitespace between class name and opening brace
4975569: javap doesn't print new flag bits
6271787: javap dumps LocalVariableTypeTable attribute in hex, needs to print a table
6305779: javap: support annotations
6439940: Clean up javap implementation
6469569: wrong check of searchpath in JavapEnvironment
6474890: javap does not open .zip files in -classpath
6587786: Javap throws error : "ERROR:Could not find <classname>" for JRE classes
6622215: javap ignores certain relevant access flags
6622216: javap names some attributes incorrectly
6622232: javap gets whitespace confused
6622260: javap prints negative bytes incorrectly in hex
Reviewed-by: ksrini
1 /*
2 * Copyright 2002-2003 Sun Microsystems, Inc. All Rights Reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Sun designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Sun in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
22 * CA 95054 USA or visit www.sun.com if you need additional information or
23 * have any questions.
24 */
28 package sun.tools.javap;
30 import java.util.*;
31 import java.io.*;
33 /**
34 * Entry point for javap, class file disassembler.
35 *
36 * @author Sucheta Dambalkar (Adopted code from old javap)
37 */
38 public class Main {
40 private Vector<String> classList = new Vector<String>();
41 private PrintWriter out;
42 JavapEnvironment env = new JavapEnvironment();
43 private static boolean errorOccurred = false;
44 private static final String progname = "javap";
47 public Main(PrintWriter out){
48 this.out = out;
49 }
51 public static void main(String argv[]) {
52 // unless first arg is -Xold, use new javap
53 if (!(argv.length >= 1 && argv[0].equals("-Xold"))) {
54 com.sun.tools.javap.Main.main(argv);
55 return;
56 }
58 entry(argv);
59 if (errorOccurred) {
60 System.exit(1);
61 }
62 }
65 /**
66 * Entry point for tool if you don't want System.exit() called.
67 */
68 public static void entry(String argv[]) {
69 PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
70 try {
72 Main jpmain = new Main(out);
73 jpmain.perform(argv);
75 } finally {
76 out.close();
77 }
78 }
80 /**
81 * Process the arguments and perform the desired action
82 */
83 private void perform(String argv[]) {
84 if (parseArguments(argv)) {
85 displayResults();
87 }
88 }
90 private void error(String msg) {
91 errorOccurred = true;
92 System.err.println(msg);
93 System.err.flush();
94 }
96 /**
97 * Print usage information
98 */
99 private void usage() {
100 java.io.PrintStream out = System.out;
101 out.println("Usage: " + progname + " <options> <classes>...");
102 out.println();
103 out.println("where options include:");
104 out.println(" -c Disassemble the code");
105 out.println(" -classpath <pathlist> Specify where to find user class files");
106 out.println(" -extdirs <dirs> Override location of installed extensions");
107 out.println(" -help Print this usage message");
108 out.println(" -J<flag> Pass <flag> directly to the runtime system");
109 out.println(" -l Print line number and local variable tables");
110 out.println(" -public Show only public classes and members");
111 out.println(" -protected Show protected/public classes and members");
112 out.println(" -package Show package/protected/public classes");
113 out.println(" and members (default)");
114 out.println(" -private Show all classes and members");
115 out.println(" -s Print internal type signatures");
116 out.println(" -bootclasspath <pathlist> Override location of class files loaded");
117 out.println(" by the bootstrap class loader");
118 out.println(" -verbose Print stack size, number of locals and args for methods");
119 out.println(" If verifying, print reasons for failure");
120 out.println();
121 }
123 /**
124 * Parse the command line arguments.
125 * Set flags, construct the class list and create environment.
126 */
127 private boolean parseArguments(String argv[]) {
128 for (int i = 0 ; i < argv.length ; i++) {
129 String arg = argv[i];
130 if (arg.startsWith("-")) {
131 if (arg.equals("-l")) {
132 env.showLineAndLocal = true;
133 } else if (arg.equals("-private") || arg.equals("-p")) {
134 env.showAccess = env.PRIVATE;
135 } else if (arg.equals("-package")) {
136 env.showAccess = env.PACKAGE;
137 } else if (arg.equals("-protected")) {
138 env.showAccess = env.PROTECTED;
139 } else if (arg.equals("-public")) {
140 env.showAccess = env.PUBLIC;
141 } else if (arg.equals("-c")) {
142 env.showDisassembled = true;
143 } else if (arg.equals("-s")) {
144 env.showInternalSigs = true;
145 } else if (arg.equals("-verbose")) {
146 env.showVerbose = true;
147 } else if (arg.equals("-v")) {
148 env.showVerbose = true;
149 } else if (arg.equals("-h")) {
150 error("-h is no longer available - use the 'javah' program");
151 return false;
152 } else if (arg.equals("-verify")) {
153 error("-verify is no longer available - use 'java -verify'");
154 return false;
155 } else if (arg.equals("-verify-verbose")) {
156 error("-verify is no longer available - use 'java -verify'");
157 return false;
158 } else if (arg.equals("-help")) {
159 usage();
160 return false;
161 } else if (arg.equals("-classpath")) {
162 if ((i + 1) < argv.length) {
163 env.classPathString = argv[++i];
164 } else {
165 error("-classpath requires argument");
166 usage();
167 return false;
168 }
169 } else if (arg.equals("-bootclasspath")) {
170 if ((i + 1) < argv.length) {
171 env.bootClassPathString = argv[++i];
172 } else {
173 error("-bootclasspath requires argument");
174 usage();
175 return false;
176 }
177 } else if (arg.equals("-extdirs")) {
178 if ((i + 1) < argv.length) {
179 env.extDirsString = argv[++i];
180 } else {
181 error("-extdirs requires argument");
182 usage();
183 return false;
184 }
185 } else if (arg.equals("-all")) {
186 env.showallAttr = true;
187 } else if (arg.equals("-Xold")) {
188 // ignore: this is old javap
189 } else {
190 error("invalid flag: " + arg);
191 usage();
192 return false;
193 }
194 } else {
195 classList.addElement(arg);
196 env.nothingToDo = false;
197 }
198 }
199 if (env.nothingToDo) {
200 System.out.println("No classes were specified on the command line. Try -help.");
201 errorOccurred = true;
202 return false;
203 }
204 return true;
205 }
207 /**
208 * Display results
209 */
210 private void displayResults() {
211 for (int i = 0; i < classList.size() ; i++ ) {
212 String Name = classList.elementAt(i);
213 InputStream classin = env.getFileInputStream(Name);
215 try {
216 JavapPrinter printer = new JavapPrinter(classin, out, env);
217 printer.print(); // actual do display
219 } catch (IllegalArgumentException exc) {
220 error(exc.getMessage());
221 }
222 }
223 }
224 }