6715251: javap should be consistent with javac and return 2 if given no arguments

Tue, 08 Jul 2008 16:59:27 -0700

author
jjg
date
Tue, 08 Jul 2008 16:59:27 -0700
changeset 64
5e270b126573
parent 62
07c916ecfc71
child 65
0d4aa3c00af5

6715251: javap should be consistent with javac and return 2 if given no arguments
Reviewed-by: ksrini

src/share/classes/com/sun/tools/javap/JavapTask.java file | annotate | diff | comparison | revisions
test/tools/javap/T4876942.java file | annotate | diff | comparison | revisions
test/tools/javap/T6715251.java file | annotate | diff | comparison | revisions
     1.1 --- a/src/share/classes/com/sun/tools/javap/JavapTask.java	Fri Jul 04 15:06:27 2008 -0700
     1.2 +++ b/src/share/classes/com/sun/tools/javap/JavapTask.java	Tue Jul 08 16:59:27 2008 -0700
     1.3 @@ -306,14 +306,32 @@
     1.4          };
     1.5      }
     1.6  
     1.7 +    /** Result codes.
     1.8 +     */
     1.9 +    static final int
    1.10 +        EXIT_OK = 0,        // Compilation completed with no errors.
    1.11 +        EXIT_ERROR = 1,     // Completed but reported errors.
    1.12 +        EXIT_CMDERR = 2,    // Bad command-line arguments
    1.13 +        EXIT_SYSERR = 3,    // System error or resource exhaustion.
    1.14 +        EXIT_ABNORMAL = 4;  // Compiler terminated abnormally
    1.15 +
    1.16      int run(String[] args) {
    1.17          try {
    1.18              handleOptions(args);
    1.19 +
    1.20 +            // the following gives consistent behavior with javac
    1.21 +            if (classes == null || classes.size() == 0) {
    1.22 +                if (options.help || options.version || options.fullVersion)
    1.23 +                    return EXIT_OK;
    1.24 +                else
    1.25 +                    return EXIT_CMDERR;
    1.26 +            }
    1.27 +
    1.28              boolean ok = run();
    1.29 -            return ok ? 0 : 1;
    1.30 +            return ok ? EXIT_OK : EXIT_ERROR;
    1.31          } catch (BadArgs e) {
    1.32              diagnosticListener.report(createDiagnostic(e.key, e.args));
    1.33 -            return 1;
    1.34 +            return EXIT_CMDERR;
    1.35          } catch (InternalError e) {
    1.36              Object[] e_args;
    1.37              if (e.getCause() == null)
    1.38 @@ -324,7 +342,7 @@
    1.39                  System.arraycopy(e.args, 0, e_args, 1, e.args.length);
    1.40              }
    1.41              diagnosticListener.report(createDiagnostic("err.internal.error", e_args));
    1.42 -            return 1;
    1.43 +            return EXIT_ABNORMAL;
    1.44          } finally {
    1.45              log.flush();
    1.46          }
    1.47 @@ -349,8 +367,7 @@
    1.48              fileManager = getDefaultFileManager(diagnosticListener, log);
    1.49  
    1.50          Iterator<String> iter = args.iterator();
    1.51 -        if (!iter.hasNext())
    1.52 -            options.help = true;
    1.53 +        boolean noArgs = !iter.hasNext();
    1.54  
    1.55          while (iter.hasNext()) {
    1.56              String arg = iter.next();
    1.57 @@ -370,9 +387,15 @@
    1.58              ((JavapFileManager) fileManager).setIgnoreSymbolFile(true);
    1.59  
    1.60          if ((classes == null || classes.size() == 0) &&
    1.61 -                !(options.help || options.version || options.fullVersion)) {
    1.62 +                !(noArgs || options.help || options.version || options.fullVersion)) {
    1.63              throw new BadArgs("err.no.classes.specified");
    1.64          }
    1.65 +
    1.66 +        if (noArgs || options.help)
    1.67 +            showHelp();
    1.68 +
    1.69 +        if (options.version || options.fullVersion)
    1.70 +            showVersion(options.fullVersion);
    1.71      }
    1.72  
    1.73      private void handleOption(String name, Iterator<String> rest) throws BadArgs {
    1.74 @@ -405,14 +428,8 @@
    1.75      }
    1.76  
    1.77      public boolean run() {
    1.78 -        if (options.help)
    1.79 -            showHelp();
    1.80 -
    1.81 -        if (options.version || options.fullVersion)
    1.82 -            showVersion(options.fullVersion);
    1.83 -
    1.84          if (classes == null || classes.size() == 0)
    1.85 -            return true;
    1.86 +            return false;
    1.87  
    1.88          context.put(PrintWriter.class, log);
    1.89          ClassWriter classWriter = ClassWriter.instance(context);
     2.1 --- a/test/tools/javap/T4876942.java	Fri Jul 04 15:06:27 2008 -0700
     2.2 +++ b/test/tools/javap/T4876942.java	Tue Jul 08 16:59:27 2008 -0700
     2.3 @@ -23,7 +23,7 @@
     2.4  
     2.5  /*
     2.6   * @test
     2.7 - * @bug 4876942
     2.8 + * @bug 4876942 6715251
     2.9   * @summary javap invoked without args does not print help screen
    2.10   */
    2.11  
    2.12 @@ -48,7 +48,7 @@
    2.13          PrintWriter out = new PrintWriter(sw);
    2.14          //sun.tools.javap.Main.entry(args);
    2.15          int rc = com.sun.tools.javap.Main.run(args, out);
    2.16 -        if (rc != 0)
    2.17 +        if (rc != (args.length == 0 ? 2 : 0))
    2.18              throw new Error("javap failed. rc=" + rc);
    2.19          out.close();
    2.20          return sw.toString();
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/test/tools/javap/T6715251.java	Tue Jul 08 16:59:27 2008 -0700
     3.3 @@ -0,0 +1,74 @@
     3.4 +/*
     3.5 + * Copyright 2008 Sun Microsystems, Inc.  All Rights Reserved.
     3.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3.7 + *
     3.8 + * This code is free software; you can redistribute it and/or modify it
     3.9 + * under the terms of the GNU General Public License version 2 only, as
    3.10 + * published by the Free Software Foundation.
    3.11 + *
    3.12 + * This code is distributed in the hope that it will be useful, but WITHOUT
    3.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    3.14 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    3.15 + * version 2 for more details (a copy is included in the LICENSE file that
    3.16 + * accompanied this code).
    3.17 + *
    3.18 + * You should have received a copy of the GNU General Public License version
    3.19 + * 2 along with this work; if not, write to the Free Software Foundation,
    3.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    3.21 + *
    3.22 + * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
    3.23 + * CA 95054 USA or visit www.sun.com if you need additional information or
    3.24 + * have any questions.
    3.25 + */
    3.26 +
    3.27 +import java.io.*;
    3.28 +import java.util.*;
    3.29 +
    3.30 +/*
    3.31 + * @test
    3.32 + * @bug 6715251
    3.33 + * @summary javap should be consistent with javac and return 2 if given no arguments
    3.34 + */
    3.35 +
    3.36 +public class T6715251 {
    3.37 +    public static void main(String... args) throws Exception {
    3.38 +        new T6715251().run();
    3.39 +    }
    3.40 +
    3.41 +    void run() throws Exception {
    3.42 +        String testClasses = System.getProperty("test.classes", ".");
    3.43 +
    3.44 +        test(2);
    3.45 +        test(0, "-help");
    3.46 +        test(0, "-version");
    3.47 +        test(0, "-fullversion");
    3.48 +        test(0, "-classpath", testClasses, "T6715251");
    3.49 +
    3.50 +        if (errors > 0)
    3.51 +            throw new Exception(errors + " errors received");
    3.52 +    }
    3.53 +
    3.54 +    void test(int expect, String ... args) {
    3.55 +        int rc = javap(args);
    3.56 +        if (rc != expect)
    3.57 +            error("bad result: expected: " + expect + ", found " + rc + "\n"
    3.58 +                  + log);
    3.59 +
    3.60 +    }
    3.61 +
    3.62 +    int javap(String... args) {
    3.63 +        StringWriter sw = new StringWriter();
    3.64 +        PrintWriter pw = new PrintWriter(sw);
    3.65 +        int rc = com.sun.tools.javap.Main.run(args, pw);
    3.66 +        log = sw.toString();
    3.67 +        return rc;
    3.68 +    }
    3.69 +
    3.70 +    void error(String msg) {
    3.71 +        System.err.println(msg);
    3.72 +        errors++;
    3.73 +    }
    3.74 +
    3.75 +    String log;
    3.76 +    int errors;
    3.77 +}
    3.78 \ No newline at end of file

mercurial