javap/src/main/java/sun/tools/javap/Main.java
author Jaroslav Tulach <jtulach@netbeans.org>
Fri, 09 Nov 2012 21:33:22 +0100
branchjavap
changeset 144 b06660b614db
permissions -rw-r--r--
javap as of revision jdk6-4ab5d66aaf2b
jtulach@144
     1
/*
jtulach@144
     2
 * Copyright (c) 2002, 2003, Oracle and/or its affiliates. All rights reserved.
jtulach@144
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
jtulach@144
     4
 *
jtulach@144
     5
 * This code is free software; you can redistribute it and/or modify it
jtulach@144
     6
 * under the terms of the GNU General Public License version 2 only, as
jtulach@144
     7
 * published by the Free Software Foundation.  Oracle designates this
jtulach@144
     8
 * particular file as subject to the "Classpath" exception as provided
jtulach@144
     9
 * by Oracle in the LICENSE file that accompanied this code.
jtulach@144
    10
 *
jtulach@144
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
jtulach@144
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
jtulach@144
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
jtulach@144
    14
 * version 2 for more details (a copy is included in the LICENSE file that
jtulach@144
    15
 * accompanied this code).
jtulach@144
    16
 *
jtulach@144
    17
 * You should have received a copy of the GNU General Public License version
jtulach@144
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
jtulach@144
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
jtulach@144
    20
 *
jtulach@144
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
jtulach@144
    22
 * or visit www.oracle.com if you need additional information or have any
jtulach@144
    23
 * questions.
jtulach@144
    24
 */
jtulach@144
    25
jtulach@144
    26
jtulach@144
    27
jtulach@144
    28
package sun.tools.javap;
jtulach@144
    29
jtulach@144
    30
import java.util.*;
jtulach@144
    31
import java.io.*;
jtulach@144
    32
jtulach@144
    33
/**
jtulach@144
    34
 * Entry point for javap, class file disassembler.
jtulach@144
    35
 *
jtulach@144
    36
 * @author  Sucheta Dambalkar (Adopted code from old javap)
jtulach@144
    37
 */
jtulach@144
    38
public class Main{
jtulach@144
    39
jtulach@144
    40
    private Vector classList = new Vector();
jtulach@144
    41
    private PrintWriter out;
jtulach@144
    42
    JavapEnvironment env = new JavapEnvironment();
jtulach@144
    43
    private static boolean errorOccurred = false;
jtulach@144
    44
    private static final String progname = "javap";
jtulach@144
    45
jtulach@144
    46
jtulach@144
    47
    public Main(PrintWriter out){
jtulach@144
    48
        this.out = out;
jtulach@144
    49
    }
jtulach@144
    50
jtulach@144
    51
    public static void main(String argv[]) {
jtulach@144
    52
        entry(argv);
jtulach@144
    53
        if (errorOccurred) {
jtulach@144
    54
            System.exit(1);
jtulach@144
    55
        }
jtulach@144
    56
    }
jtulach@144
    57
jtulach@144
    58
jtulach@144
    59
    /**
jtulach@144
    60
     * Entry point for tool if you don't want System.exit() called.
jtulach@144
    61
     */
jtulach@144
    62
    public static void entry(String argv[]) {
jtulach@144
    63
        PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
jtulach@144
    64
        try {
jtulach@144
    65
jtulach@144
    66
            Main jpmain = new Main(out);
jtulach@144
    67
            jpmain.perform(argv);
jtulach@144
    68
jtulach@144
    69
        } finally {
jtulach@144
    70
            out.close();
jtulach@144
    71
        }
jtulach@144
    72
    }
jtulach@144
    73
jtulach@144
    74
    /**
jtulach@144
    75
     * Process the arguments and perform the desired action
jtulach@144
    76
     */
jtulach@144
    77
    private void perform(String argv[]) {
jtulach@144
    78
        if (parseArguments(argv)) {
jtulach@144
    79
            displayResults();
jtulach@144
    80
jtulach@144
    81
        }
jtulach@144
    82
    }
jtulach@144
    83
jtulach@144
    84
    private void error(String msg) {
jtulach@144
    85
        errorOccurred = true;
jtulach@144
    86
        System.err.println(msg);
jtulach@144
    87
        System.err.flush();
jtulach@144
    88
    }
jtulach@144
    89
jtulach@144
    90
    /**
jtulach@144
    91
     * Print usage information
jtulach@144
    92
     */
jtulach@144
    93
    private void usage() {
jtulach@144
    94
        java.io.PrintStream out = System.out;
jtulach@144
    95
        out.println("Usage: " + progname + " <options> <classes>...");
jtulach@144
    96
        out.println();
jtulach@144
    97
        out.println("where options include:");
jtulach@144
    98
        out.println("   -c                        Disassemble the code");
jtulach@144
    99
        out.println("   -classpath <pathlist>     Specify where to find user class files");
jtulach@144
   100
        out.println("   -extdirs <dirs>           Override location of installed extensions");
jtulach@144
   101
        out.println("   -help                     Print this usage message");
jtulach@144
   102
        out.println("   -J<flag>                  Pass <flag> directly to the runtime system");
jtulach@144
   103
        out.println("   -l                        Print line number and local variable tables");
jtulach@144
   104
        out.println("   -public                   Show only public classes and members");
jtulach@144
   105
        out.println("   -protected                Show protected/public classes and members");
jtulach@144
   106
        out.println("   -package                  Show package/protected/public classes");
jtulach@144
   107
        out.println("                             and members (default)");
jtulach@144
   108
        out.println("   -private                  Show all classes and members");
jtulach@144
   109
        out.println("   -s                        Print internal type signatures");
jtulach@144
   110
        out.println("   -bootclasspath <pathlist> Override location of class files loaded");
jtulach@144
   111
        out.println("                             by the bootstrap class loader");
jtulach@144
   112
        out.println("   -verbose                  Print stack size, number of locals and args for methods");
jtulach@144
   113
        out.println("                             If verifying, print reasons for failure");
jtulach@144
   114
        out.println();
jtulach@144
   115
    }
jtulach@144
   116
jtulach@144
   117
    /**
jtulach@144
   118
     * Parse the command line arguments.
jtulach@144
   119
     * Set flags, construct the class list and create environment.
jtulach@144
   120
     */
jtulach@144
   121
    private boolean parseArguments(String argv[]) {
jtulach@144
   122
        for (int i = 0 ; i < argv.length ; i++) {
jtulach@144
   123
            String arg = argv[i];
jtulach@144
   124
            if (arg.startsWith("-")) {
jtulach@144
   125
                if (arg.equals("-l")) {
jtulach@144
   126
                    env.showLineAndLocal = true;
jtulach@144
   127
                } else if (arg.equals("-private") || arg.equals("-p")) {
jtulach@144
   128
                    env.showAccess = env.PRIVATE;
jtulach@144
   129
                } else if (arg.equals("-package")) {
jtulach@144
   130
                    env.showAccess = env.PACKAGE;
jtulach@144
   131
                } else if (arg.equals("-protected")) {
jtulach@144
   132
                    env.showAccess = env.PROTECTED;
jtulach@144
   133
                } else if (arg.equals("-public")) {
jtulach@144
   134
                    env.showAccess = env.PUBLIC;
jtulach@144
   135
                } else if (arg.equals("-c")) {
jtulach@144
   136
                    env.showDisassembled = true;
jtulach@144
   137
                } else if (arg.equals("-s")) {
jtulach@144
   138
                    env.showInternalSigs = true;
jtulach@144
   139
                } else if (arg.equals("-verbose"))  {
jtulach@144
   140
                    env.showVerbose = true;
jtulach@144
   141
                } else if (arg.equals("-v")) {
jtulach@144
   142
                    env.showVerbose = true;
jtulach@144
   143
                } else if (arg.equals("-h")) {
jtulach@144
   144
                    error("-h is no longer available - use the 'javah' program");
jtulach@144
   145
                    return false;
jtulach@144
   146
                } else if (arg.equals("-verify")) {
jtulach@144
   147
                    error("-verify is no longer available - use 'java -verify'");
jtulach@144
   148
                    return false;
jtulach@144
   149
                } else if (arg.equals("-verify-verbose")) {
jtulach@144
   150
                    error("-verify is no longer available - use 'java -verify'");
jtulach@144
   151
                    return false;
jtulach@144
   152
                } else if (arg.equals("-help")) {
jtulach@144
   153
                    usage();
jtulach@144
   154
                    return false;
jtulach@144
   155
                } else if (arg.equals("-classpath")) {
jtulach@144
   156
                    if ((i + 1) < argv.length) {
jtulach@144
   157
                        env.classPathString = argv[++i];
jtulach@144
   158
                    } else {
jtulach@144
   159
                        error("-classpath requires argument");
jtulach@144
   160
                        usage();
jtulach@144
   161
                        return false;
jtulach@144
   162
                    }
jtulach@144
   163
                } else if (arg.equals("-bootclasspath")) {
jtulach@144
   164
                    if ((i + 1) < argv.length) {
jtulach@144
   165
                        env.bootClassPathString = argv[++i];
jtulach@144
   166
                    } else {
jtulach@144
   167
                        error("-bootclasspath requires argument");
jtulach@144
   168
                        usage();
jtulach@144
   169
                        return false;
jtulach@144
   170
                    }
jtulach@144
   171
                } else if (arg.equals("-extdirs")) {
jtulach@144
   172
                    if ((i + 1) < argv.length) {
jtulach@144
   173
                        env.extDirsString = argv[++i];
jtulach@144
   174
                    } else {
jtulach@144
   175
                        error("-extdirs requires argument");
jtulach@144
   176
                        usage();
jtulach@144
   177
                        return false;
jtulach@144
   178
                    }
jtulach@144
   179
                } else if (arg.equals("-all")) {
jtulach@144
   180
                    env.showallAttr = true;
jtulach@144
   181
                } else {
jtulach@144
   182
                    error("invalid flag: " + arg);
jtulach@144
   183
                    usage();
jtulach@144
   184
                    return false;
jtulach@144
   185
                }
jtulach@144
   186
            } else {
jtulach@144
   187
                classList.addElement(arg);
jtulach@144
   188
                env.nothingToDo = false;
jtulach@144
   189
            }
jtulach@144
   190
        }
jtulach@144
   191
        if (env.nothingToDo) {
jtulach@144
   192
            System.out.println("No classes were specified on the command line.  Try -help.");
jtulach@144
   193
            errorOccurred = true;
jtulach@144
   194
            return false;
jtulach@144
   195
        }
jtulach@144
   196
        return true;
jtulach@144
   197
    }
jtulach@144
   198
jtulach@144
   199
    /**
jtulach@144
   200
     * Display results
jtulach@144
   201
     */
jtulach@144
   202
    private void displayResults() {
jtulach@144
   203
        for (int i = 0; i < classList.size() ; i++ ) {
jtulach@144
   204
            String Name = (String)classList.elementAt(i);
jtulach@144
   205
            InputStream classin = env.getFileInputStream(Name);
jtulach@144
   206
jtulach@144
   207
            try {
jtulach@144
   208
                JavapPrinter printer = new JavapPrinter(classin, out, env);
jtulach@144
   209
                printer.print();                // actual do display
jtulach@144
   210
jtulach@144
   211
            } catch (IllegalArgumentException exc) {
jtulach@144
   212
                error(exc.getMessage());
jtulach@144
   213
            }
jtulach@144
   214
        }
jtulach@144
   215
    }
jtulach@144
   216
}