langtools/src/share/classes/sun/tools/javap/Main.java
changeset 10 06bc494ca11e
child 656 4718b910737c
equal deleted inserted replaced
0:fd16c54261b3 10:06bc494ca11e
       
     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  */
       
    25 
       
    26 
       
    27 
       
    28 package sun.tools.javap;
       
    29 
       
    30 import java.util.*;
       
    31 import java.io.*;
       
    32 
       
    33 /**
       
    34  * Entry point for javap, class file disassembler.
       
    35  *
       
    36  * @author  Sucheta Dambalkar (Adopted code from old javap)
       
    37  */
       
    38 public class Main{
       
    39 
       
    40     private Vector classList = new Vector();
       
    41     private PrintWriter out;
       
    42     JavapEnvironment env = new JavapEnvironment();
       
    43     private static boolean errorOccurred = false;
       
    44     private static final String progname = "javap";
       
    45 
       
    46 
       
    47     public Main(PrintWriter out){
       
    48         this.out = out;
       
    49     }
       
    50 
       
    51     public static void main(String argv[]) {
       
    52         entry(argv);
       
    53         if (errorOccurred) {
       
    54             System.exit(1);
       
    55         }
       
    56     }
       
    57 
       
    58 
       
    59     /**
       
    60      * Entry point for tool if you don't want System.exit() called.
       
    61      */
       
    62     public static void entry(String argv[]) {
       
    63         PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
       
    64         try {
       
    65 
       
    66             Main jpmain = new Main(out);
       
    67             jpmain.perform(argv);
       
    68 
       
    69         } finally {
       
    70             out.close();
       
    71         }
       
    72     }
       
    73 
       
    74     /**
       
    75      * Process the arguments and perform the desired action
       
    76      */
       
    77     private void perform(String argv[]) {
       
    78         if (parseArguments(argv)) {
       
    79             displayResults();
       
    80 
       
    81         }
       
    82     }
       
    83 
       
    84     private void error(String msg) {
       
    85         errorOccurred = true;
       
    86         System.err.println(msg);
       
    87         System.err.flush();
       
    88     }
       
    89 
       
    90     /**
       
    91      * Print usage information
       
    92      */
       
    93     private void usage() {
       
    94         java.io.PrintStream out = System.out;
       
    95         out.println("Usage: " + progname + " <options> <classes>...");
       
    96         out.println();
       
    97         out.println("where options include:");
       
    98         out.println("   -c                        Disassemble the code");
       
    99         out.println("   -classpath <pathlist>     Specify where to find user class files");
       
   100         out.println("   -extdirs <dirs>           Override location of installed extensions");
       
   101         out.println("   -help                     Print this usage message");
       
   102         out.println("   -J<flag>                  Pass <flag> directly to the runtime system");
       
   103         out.println("   -l                        Print line number and local variable tables");
       
   104         out.println("   -public                   Show only public classes and members");
       
   105         out.println("   -protected                Show protected/public classes and members");
       
   106         out.println("   -package                  Show package/protected/public classes");
       
   107         out.println("                             and members (default)");
       
   108         out.println("   -private                  Show all classes and members");
       
   109         out.println("   -s                        Print internal type signatures");
       
   110         out.println("   -bootclasspath <pathlist> Override location of class files loaded");
       
   111         out.println("                             by the bootstrap class loader");
       
   112         out.println("   -verbose                  Print stack size, number of locals and args for methods");
       
   113         out.println("                             If verifying, print reasons for failure");
       
   114         out.println();
       
   115     }
       
   116 
       
   117     /**
       
   118      * Parse the command line arguments.
       
   119      * Set flags, construct the class list and create environment.
       
   120      */
       
   121     private boolean parseArguments(String argv[]) {
       
   122         for (int i = 0 ; i < argv.length ; i++) {
       
   123             String arg = argv[i];
       
   124             if (arg.startsWith("-")) {
       
   125                 if (arg.equals("-l")) {
       
   126                     env.showLineAndLocal = true;
       
   127                 } else if (arg.equals("-private") || arg.equals("-p")) {
       
   128                     env.showAccess = env.PRIVATE;
       
   129                 } else if (arg.equals("-package")) {
       
   130                     env.showAccess = env.PACKAGE;
       
   131                 } else if (arg.equals("-protected")) {
       
   132                     env.showAccess = env.PROTECTED;
       
   133                 } else if (arg.equals("-public")) {
       
   134                     env.showAccess = env.PUBLIC;
       
   135                 } else if (arg.equals("-c")) {
       
   136                     env.showDisassembled = true;
       
   137                 } else if (arg.equals("-s")) {
       
   138                     env.showInternalSigs = true;
       
   139                 } else if (arg.equals("-verbose"))  {
       
   140                     env.showVerbose = true;
       
   141                 } else if (arg.equals("-v")) {
       
   142                     env.showVerbose = true;
       
   143                 } else if (arg.equals("-h")) {
       
   144                     error("-h is no longer available - use the 'javah' program");
       
   145                     return false;
       
   146                 } else if (arg.equals("-verify")) {
       
   147                     error("-verify is no longer available - use 'java -verify'");
       
   148                     return false;
       
   149                 } else if (arg.equals("-verify-verbose")) {
       
   150                     error("-verify is no longer available - use 'java -verify'");
       
   151                     return false;
       
   152                 } else if (arg.equals("-help")) {
       
   153                     usage();
       
   154                     return false;
       
   155                 } else if (arg.equals("-classpath")) {
       
   156                     if ((i + 1) < argv.length) {
       
   157                         env.classPathString = argv[++i];
       
   158                     } else {
       
   159                         error("-classpath requires argument");
       
   160                         usage();
       
   161                         return false;
       
   162                     }
       
   163                 } else if (arg.equals("-bootclasspath")) {
       
   164                     if ((i + 1) < argv.length) {
       
   165                         env.bootClassPathString = argv[++i];
       
   166                     } else {
       
   167                         error("-bootclasspath requires argument");
       
   168                         usage();
       
   169                         return false;
       
   170                     }
       
   171                 } else if (arg.equals("-extdirs")) {
       
   172                     if ((i + 1) < argv.length) {
       
   173                         env.extDirsString = argv[++i];
       
   174                     } else {
       
   175                         error("-extdirs requires argument");
       
   176                         usage();
       
   177                         return false;
       
   178                     }
       
   179                 } else if (arg.equals("-all")) {
       
   180                     env.showallAttr = true;
       
   181                 } else {
       
   182                     error("invalid flag: " + arg);
       
   183                     usage();
       
   184                     return false;
       
   185                 }
       
   186             } else {
       
   187                 classList.addElement(arg);
       
   188                 env.nothingToDo = false;
       
   189             }
       
   190         }
       
   191         if (env.nothingToDo) {
       
   192             System.out.println("No classes were specified on the command line.  Try -help.");
       
   193             errorOccurred = true;
       
   194             return false;
       
   195         }
       
   196         return true;
       
   197     }
       
   198 
       
   199     /**
       
   200      * Display results
       
   201      */
       
   202     private void displayResults() {
       
   203         for (int i = 0; i < classList.size() ; i++ ) {
       
   204             String Name = (String)classList.elementAt(i);
       
   205             InputStream classin = env.getFileInputStream(Name);
       
   206 
       
   207             try {
       
   208                 JavapPrinter printer = new JavapPrinter(classin, out, env);
       
   209                 printer.print();                // actual do display
       
   210 
       
   211             } catch (IllegalArgumentException exc) {
       
   212                 error(exc.getMessage());
       
   213             }
       
   214         }
       
   215     }
       
   216 }