javap/src/main/java/sun/tools/javap/Main.java
branchjavap
changeset 144 b06660b614db
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/javap/src/main/java/sun/tools/javap/Main.java	Fri Nov 09 21:33:22 2012 +0100
     1.3 @@ -0,0 +1,216 @@
     1.4 +/*
     1.5 + * Copyright (c) 2002, 2003, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.  Oracle designates this
    1.11 + * particular file as subject to the "Classpath" exception as provided
    1.12 + * by Oracle in the LICENSE file that accompanied this code.
    1.13 + *
    1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.16 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.17 + * version 2 for more details (a copy is included in the LICENSE file that
    1.18 + * accompanied this code).
    1.19 + *
    1.20 + * You should have received a copy of the GNU General Public License version
    1.21 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.23 + *
    1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.25 + * or visit www.oracle.com if you need additional information or have any
    1.26 + * questions.
    1.27 + */
    1.28 +
    1.29 +
    1.30 +
    1.31 +package sun.tools.javap;
    1.32 +
    1.33 +import java.util.*;
    1.34 +import java.io.*;
    1.35 +
    1.36 +/**
    1.37 + * Entry point for javap, class file disassembler.
    1.38 + *
    1.39 + * @author  Sucheta Dambalkar (Adopted code from old javap)
    1.40 + */
    1.41 +public class Main{
    1.42 +
    1.43 +    private Vector classList = new Vector();
    1.44 +    private PrintWriter out;
    1.45 +    JavapEnvironment env = new JavapEnvironment();
    1.46 +    private static boolean errorOccurred = false;
    1.47 +    private static final String progname = "javap";
    1.48 +
    1.49 +
    1.50 +    public Main(PrintWriter out){
    1.51 +        this.out = out;
    1.52 +    }
    1.53 +
    1.54 +    public static void main(String argv[]) {
    1.55 +        entry(argv);
    1.56 +        if (errorOccurred) {
    1.57 +            System.exit(1);
    1.58 +        }
    1.59 +    }
    1.60 +
    1.61 +
    1.62 +    /**
    1.63 +     * Entry point for tool if you don't want System.exit() called.
    1.64 +     */
    1.65 +    public static void entry(String argv[]) {
    1.66 +        PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
    1.67 +        try {
    1.68 +
    1.69 +            Main jpmain = new Main(out);
    1.70 +            jpmain.perform(argv);
    1.71 +
    1.72 +        } finally {
    1.73 +            out.close();
    1.74 +        }
    1.75 +    }
    1.76 +
    1.77 +    /**
    1.78 +     * Process the arguments and perform the desired action
    1.79 +     */
    1.80 +    private void perform(String argv[]) {
    1.81 +        if (parseArguments(argv)) {
    1.82 +            displayResults();
    1.83 +
    1.84 +        }
    1.85 +    }
    1.86 +
    1.87 +    private void error(String msg) {
    1.88 +        errorOccurred = true;
    1.89 +        System.err.println(msg);
    1.90 +        System.err.flush();
    1.91 +    }
    1.92 +
    1.93 +    /**
    1.94 +     * Print usage information
    1.95 +     */
    1.96 +    private void usage() {
    1.97 +        java.io.PrintStream out = System.out;
    1.98 +        out.println("Usage: " + progname + " <options> <classes>...");
    1.99 +        out.println();
   1.100 +        out.println("where options include:");
   1.101 +        out.println("   -c                        Disassemble the code");
   1.102 +        out.println("   -classpath <pathlist>     Specify where to find user class files");
   1.103 +        out.println("   -extdirs <dirs>           Override location of installed extensions");
   1.104 +        out.println("   -help                     Print this usage message");
   1.105 +        out.println("   -J<flag>                  Pass <flag> directly to the runtime system");
   1.106 +        out.println("   -l                        Print line number and local variable tables");
   1.107 +        out.println("   -public                   Show only public classes and members");
   1.108 +        out.println("   -protected                Show protected/public classes and members");
   1.109 +        out.println("   -package                  Show package/protected/public classes");
   1.110 +        out.println("                             and members (default)");
   1.111 +        out.println("   -private                  Show all classes and members");
   1.112 +        out.println("   -s                        Print internal type signatures");
   1.113 +        out.println("   -bootclasspath <pathlist> Override location of class files loaded");
   1.114 +        out.println("                             by the bootstrap class loader");
   1.115 +        out.println("   -verbose                  Print stack size, number of locals and args for methods");
   1.116 +        out.println("                             If verifying, print reasons for failure");
   1.117 +        out.println();
   1.118 +    }
   1.119 +
   1.120 +    /**
   1.121 +     * Parse the command line arguments.
   1.122 +     * Set flags, construct the class list and create environment.
   1.123 +     */
   1.124 +    private boolean parseArguments(String argv[]) {
   1.125 +        for (int i = 0 ; i < argv.length ; i++) {
   1.126 +            String arg = argv[i];
   1.127 +            if (arg.startsWith("-")) {
   1.128 +                if (arg.equals("-l")) {
   1.129 +                    env.showLineAndLocal = true;
   1.130 +                } else if (arg.equals("-private") || arg.equals("-p")) {
   1.131 +                    env.showAccess = env.PRIVATE;
   1.132 +                } else if (arg.equals("-package")) {
   1.133 +                    env.showAccess = env.PACKAGE;
   1.134 +                } else if (arg.equals("-protected")) {
   1.135 +                    env.showAccess = env.PROTECTED;
   1.136 +                } else if (arg.equals("-public")) {
   1.137 +                    env.showAccess = env.PUBLIC;
   1.138 +                } else if (arg.equals("-c")) {
   1.139 +                    env.showDisassembled = true;
   1.140 +                } else if (arg.equals("-s")) {
   1.141 +                    env.showInternalSigs = true;
   1.142 +                } else if (arg.equals("-verbose"))  {
   1.143 +                    env.showVerbose = true;
   1.144 +                } else if (arg.equals("-v")) {
   1.145 +                    env.showVerbose = true;
   1.146 +                } else if (arg.equals("-h")) {
   1.147 +                    error("-h is no longer available - use the 'javah' program");
   1.148 +                    return false;
   1.149 +                } else if (arg.equals("-verify")) {
   1.150 +                    error("-verify is no longer available - use 'java -verify'");
   1.151 +                    return false;
   1.152 +                } else if (arg.equals("-verify-verbose")) {
   1.153 +                    error("-verify is no longer available - use 'java -verify'");
   1.154 +                    return false;
   1.155 +                } else if (arg.equals("-help")) {
   1.156 +                    usage();
   1.157 +                    return false;
   1.158 +                } else if (arg.equals("-classpath")) {
   1.159 +                    if ((i + 1) < argv.length) {
   1.160 +                        env.classPathString = argv[++i];
   1.161 +                    } else {
   1.162 +                        error("-classpath requires argument");
   1.163 +                        usage();
   1.164 +                        return false;
   1.165 +                    }
   1.166 +                } else if (arg.equals("-bootclasspath")) {
   1.167 +                    if ((i + 1) < argv.length) {
   1.168 +                        env.bootClassPathString = argv[++i];
   1.169 +                    } else {
   1.170 +                        error("-bootclasspath requires argument");
   1.171 +                        usage();
   1.172 +                        return false;
   1.173 +                    }
   1.174 +                } else if (arg.equals("-extdirs")) {
   1.175 +                    if ((i + 1) < argv.length) {
   1.176 +                        env.extDirsString = argv[++i];
   1.177 +                    } else {
   1.178 +                        error("-extdirs requires argument");
   1.179 +                        usage();
   1.180 +                        return false;
   1.181 +                    }
   1.182 +                } else if (arg.equals("-all")) {
   1.183 +                    env.showallAttr = true;
   1.184 +                } else {
   1.185 +                    error("invalid flag: " + arg);
   1.186 +                    usage();
   1.187 +                    return false;
   1.188 +                }
   1.189 +            } else {
   1.190 +                classList.addElement(arg);
   1.191 +                env.nothingToDo = false;
   1.192 +            }
   1.193 +        }
   1.194 +        if (env.nothingToDo) {
   1.195 +            System.out.println("No classes were specified on the command line.  Try -help.");
   1.196 +            errorOccurred = true;
   1.197 +            return false;
   1.198 +        }
   1.199 +        return true;
   1.200 +    }
   1.201 +
   1.202 +    /**
   1.203 +     * Display results
   1.204 +     */
   1.205 +    private void displayResults() {
   1.206 +        for (int i = 0; i < classList.size() ; i++ ) {
   1.207 +            String Name = (String)classList.elementAt(i);
   1.208 +            InputStream classin = env.getFileInputStream(Name);
   1.209 +
   1.210 +            try {
   1.211 +                JavapPrinter printer = new JavapPrinter(classin, out, env);
   1.212 +                printer.print();                // actual do display
   1.213 +
   1.214 +            } catch (IllegalArgumentException exc) {
   1.215 +                error(exc.getMessage());
   1.216 +            }
   1.217 +        }
   1.218 +    }
   1.219 +}