jtulach@144: /* jtulach@144: * Copyright (c) 2002, 2003, Oracle and/or its affiliates. All rights reserved. jtulach@144: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. jtulach@144: * jtulach@144: * This code is free software; you can redistribute it and/or modify it jtulach@144: * under the terms of the GNU General Public License version 2 only, as jtulach@144: * published by the Free Software Foundation. Oracle designates this jtulach@144: * particular file as subject to the "Classpath" exception as provided jtulach@144: * by Oracle in the LICENSE file that accompanied this code. jtulach@144: * jtulach@144: * This code is distributed in the hope that it will be useful, but WITHOUT jtulach@144: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or jtulach@144: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License jtulach@144: * version 2 for more details (a copy is included in the LICENSE file that jtulach@144: * accompanied this code). jtulach@144: * jtulach@144: * You should have received a copy of the GNU General Public License version jtulach@144: * 2 along with this work; if not, write to the Free Software Foundation, jtulach@144: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. jtulach@144: * jtulach@144: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA jtulach@144: * or visit www.oracle.com if you need additional information or have any jtulach@144: * questions. jtulach@144: */ jtulach@144: jtulach@144: jtulach@144: jtulach@144: package sun.tools.javap; jtulach@144: jtulach@144: import java.util.*; jtulach@144: import java.io.*; jtulach@144: jtulach@144: /** jtulach@144: * Entry point for javap, class file disassembler. jtulach@144: * jtulach@144: * @author Sucheta Dambalkar (Adopted code from old javap) jtulach@144: */ jtulach@144: public class Main{ jtulach@144: jtulach@144: private Vector classList = new Vector(); jtulach@144: private PrintWriter out; jtulach@144: JavapEnvironment env = new JavapEnvironment(); jtulach@144: private static boolean errorOccurred = false; jtulach@144: private static final String progname = "javap"; jtulach@144: jtulach@144: jtulach@144: public Main(PrintWriter out){ jtulach@144: this.out = out; jtulach@144: } jtulach@144: jtulach@144: public static void main(String argv[]) { jtulach@144: entry(argv); jtulach@144: if (errorOccurred) { jtulach@144: System.exit(1); jtulach@144: } jtulach@144: } jtulach@144: jtulach@144: jtulach@144: /** jtulach@144: * Entry point for tool if you don't want System.exit() called. jtulach@144: */ jtulach@144: public static void entry(String argv[]) { jtulach@144: PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out)); jtulach@144: try { jtulach@144: jtulach@144: Main jpmain = new Main(out); jtulach@144: jpmain.perform(argv); jtulach@144: jtulach@144: } finally { jtulach@144: out.close(); jtulach@144: } jtulach@144: } jtulach@144: jtulach@144: /** jtulach@144: * Process the arguments and perform the desired action jtulach@144: */ jtulach@144: private void perform(String argv[]) { jtulach@144: if (parseArguments(argv)) { jtulach@144: displayResults(); jtulach@144: jtulach@144: } jtulach@144: } jtulach@144: jtulach@144: private void error(String msg) { jtulach@144: errorOccurred = true; jtulach@144: System.err.println(msg); jtulach@144: System.err.flush(); jtulach@144: } jtulach@144: jtulach@144: /** jtulach@144: * Print usage information jtulach@144: */ jtulach@144: private void usage() { jtulach@144: java.io.PrintStream out = System.out; jtulach@144: out.println("Usage: " + progname + " ..."); jtulach@144: out.println(); jtulach@144: out.println("where options include:"); jtulach@144: out.println(" -c Disassemble the code"); jtulach@144: out.println(" -classpath Specify where to find user class files"); jtulach@144: out.println(" -extdirs Override location of installed extensions"); jtulach@144: out.println(" -help Print this usage message"); jtulach@144: out.println(" -J Pass directly to the runtime system"); jtulach@144: out.println(" -l Print line number and local variable tables"); jtulach@144: out.println(" -public Show only public classes and members"); jtulach@144: out.println(" -protected Show protected/public classes and members"); jtulach@144: out.println(" -package Show package/protected/public classes"); jtulach@144: out.println(" and members (default)"); jtulach@144: out.println(" -private Show all classes and members"); jtulach@144: out.println(" -s Print internal type signatures"); jtulach@144: out.println(" -bootclasspath Override location of class files loaded"); jtulach@144: out.println(" by the bootstrap class loader"); jtulach@144: out.println(" -verbose Print stack size, number of locals and args for methods"); jtulach@144: out.println(" If verifying, print reasons for failure"); jtulach@144: out.println(); jtulach@144: } jtulach@144: jtulach@144: /** jtulach@144: * Parse the command line arguments. jtulach@144: * Set flags, construct the class list and create environment. jtulach@144: */ jtulach@144: private boolean parseArguments(String argv[]) { jtulach@144: for (int i = 0 ; i < argv.length ; i++) { jtulach@144: String arg = argv[i]; jtulach@144: if (arg.startsWith("-")) { jtulach@144: if (arg.equals("-l")) { jtulach@144: env.showLineAndLocal = true; jtulach@144: } else if (arg.equals("-private") || arg.equals("-p")) { jtulach@144: env.showAccess = env.PRIVATE; jtulach@144: } else if (arg.equals("-package")) { jtulach@144: env.showAccess = env.PACKAGE; jtulach@144: } else if (arg.equals("-protected")) { jtulach@144: env.showAccess = env.PROTECTED; jtulach@144: } else if (arg.equals("-public")) { jtulach@144: env.showAccess = env.PUBLIC; jtulach@144: } else if (arg.equals("-c")) { jtulach@144: env.showDisassembled = true; jtulach@144: } else if (arg.equals("-s")) { jtulach@144: env.showInternalSigs = true; jtulach@144: } else if (arg.equals("-verbose")) { jtulach@144: env.showVerbose = true; jtulach@144: } else if (arg.equals("-v")) { jtulach@144: env.showVerbose = true; jtulach@144: } else if (arg.equals("-h")) { jtulach@144: error("-h is no longer available - use the 'javah' program"); jtulach@144: return false; jtulach@144: } else if (arg.equals("-verify")) { jtulach@144: error("-verify is no longer available - use 'java -verify'"); jtulach@144: return false; jtulach@144: } else if (arg.equals("-verify-verbose")) { jtulach@144: error("-verify is no longer available - use 'java -verify'"); jtulach@144: return false; jtulach@144: } else if (arg.equals("-help")) { jtulach@144: usage(); jtulach@144: return false; jtulach@144: } else if (arg.equals("-classpath")) { jtulach@144: if ((i + 1) < argv.length) { jtulach@144: env.classPathString = argv[++i]; jtulach@144: } else { jtulach@144: error("-classpath requires argument"); jtulach@144: usage(); jtulach@144: return false; jtulach@144: } jtulach@144: } else if (arg.equals("-bootclasspath")) { jtulach@144: if ((i + 1) < argv.length) { jtulach@144: env.bootClassPathString = argv[++i]; jtulach@144: } else { jtulach@144: error("-bootclasspath requires argument"); jtulach@144: usage(); jtulach@144: return false; jtulach@144: } jtulach@144: } else if (arg.equals("-extdirs")) { jtulach@144: if ((i + 1) < argv.length) { jtulach@144: env.extDirsString = argv[++i]; jtulach@144: } else { jtulach@144: error("-extdirs requires argument"); jtulach@144: usage(); jtulach@144: return false; jtulach@144: } jtulach@144: } else if (arg.equals("-all")) { jtulach@144: env.showallAttr = true; jtulach@144: } else { jtulach@144: error("invalid flag: " + arg); jtulach@144: usage(); jtulach@144: return false; jtulach@144: } jtulach@144: } else { jtulach@144: classList.addElement(arg); jtulach@144: env.nothingToDo = false; jtulach@144: } jtulach@144: } jtulach@144: if (env.nothingToDo) { jtulach@144: System.out.println("No classes were specified on the command line. Try -help."); jtulach@144: errorOccurred = true; jtulach@144: return false; jtulach@144: } jtulach@144: return true; jtulach@144: } jtulach@144: jtulach@144: /** jtulach@144: * Display results jtulach@144: */ jtulach@144: private void displayResults() { jtulach@144: for (int i = 0; i < classList.size() ; i++ ) { jtulach@144: String Name = (String)classList.elementAt(i); jtulach@144: InputStream classin = env.getFileInputStream(Name); jtulach@144: jtulach@144: try { jtulach@144: JavapPrinter printer = new JavapPrinter(classin, out, env); jtulach@144: printer.print(); // actual do display jtulach@144: jtulach@144: } catch (IllegalArgumentException exc) { jtulach@144: error(exc.getMessage()); jtulach@144: } jtulach@144: } jtulach@144: } jtulach@144: }