6715251: javap should be consistent with javac and return 2 if given no arguments
Reviewed-by: ksrini
--- a/langtools/src/share/classes/com/sun/tools/javap/JavapTask.java Wed Jul 05 16:39:00 2017 +0200
+++ b/langtools/src/share/classes/com/sun/tools/javap/JavapTask.java Tue Jul 08 16:59:27 2008 -0700
@@ -306,14 +306,32 @@
};
}
+ /** Result codes.
+ */
+ static final int
+ EXIT_OK = 0, // Compilation completed with no errors.
+ EXIT_ERROR = 1, // Completed but reported errors.
+ EXIT_CMDERR = 2, // Bad command-line arguments
+ EXIT_SYSERR = 3, // System error or resource exhaustion.
+ EXIT_ABNORMAL = 4; // Compiler terminated abnormally
+
int run(String[] args) {
try {
handleOptions(args);
+
+ // the following gives consistent behavior with javac
+ if (classes == null || classes.size() == 0) {
+ if (options.help || options.version || options.fullVersion)
+ return EXIT_OK;
+ else
+ return EXIT_CMDERR;
+ }
+
boolean ok = run();
- return ok ? 0 : 1;
+ return ok ? EXIT_OK : EXIT_ERROR;
} catch (BadArgs e) {
diagnosticListener.report(createDiagnostic(e.key, e.args));
- return 1;
+ return EXIT_CMDERR;
} catch (InternalError e) {
Object[] e_args;
if (e.getCause() == null)
@@ -324,7 +342,7 @@
System.arraycopy(e.args, 0, e_args, 1, e.args.length);
}
diagnosticListener.report(createDiagnostic("err.internal.error", e_args));
- return 1;
+ return EXIT_ABNORMAL;
} finally {
log.flush();
}
@@ -349,8 +367,7 @@
fileManager = getDefaultFileManager(diagnosticListener, log);
Iterator<String> iter = args.iterator();
- if (!iter.hasNext())
- options.help = true;
+ boolean noArgs = !iter.hasNext();
while (iter.hasNext()) {
String arg = iter.next();
@@ -370,9 +387,15 @@
((JavapFileManager) fileManager).setIgnoreSymbolFile(true);
if ((classes == null || classes.size() == 0) &&
- !(options.help || options.version || options.fullVersion)) {
+ !(noArgs || options.help || options.version || options.fullVersion)) {
throw new BadArgs("err.no.classes.specified");
}
+
+ if (noArgs || options.help)
+ showHelp();
+
+ if (options.version || options.fullVersion)
+ showVersion(options.fullVersion);
}
private void handleOption(String name, Iterator<String> rest) throws BadArgs {
@@ -405,14 +428,8 @@
}
public boolean run() {
- if (options.help)
- showHelp();
-
- if (options.version || options.fullVersion)
- showVersion(options.fullVersion);
-
if (classes == null || classes.size() == 0)
- return true;
+ return false;
context.put(PrintWriter.class, log);
ClassWriter classWriter = ClassWriter.instance(context);
--- a/langtools/test/tools/javap/T4876942.java Wed Jul 05 16:39:00 2017 +0200
+++ b/langtools/test/tools/javap/T4876942.java Tue Jul 08 16:59:27 2008 -0700
@@ -23,7 +23,7 @@
/*
* @test
- * @bug 4876942
+ * @bug 4876942 6715251
* @summary javap invoked without args does not print help screen
*/
@@ -48,7 +48,7 @@
PrintWriter out = new PrintWriter(sw);
//sun.tools.javap.Main.entry(args);
int rc = com.sun.tools.javap.Main.run(args, out);
- if (rc != 0)
+ if (rc != (args.length == 0 ? 2 : 0))
throw new Error("javap failed. rc=" + rc);
out.close();
return sw.toString();
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/langtools/test/tools/javap/T6715251.java Tue Jul 08 16:59:27 2008 -0700
@@ -0,0 +1,74 @@
+/*
+ * Copyright 2008 Sun Microsystems, Inc. All Rights Reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
+ * CA 95054 USA or visit www.sun.com if you need additional information or
+ * have any questions.
+ */
+
+import java.io.*;
+import java.util.*;
+
+/*
+ * @test
+ * @bug 6715251
+ * @summary javap should be consistent with javac and return 2 if given no arguments
+ */
+
+public class T6715251 {
+ public static void main(String... args) throws Exception {
+ new T6715251().run();
+ }
+
+ void run() throws Exception {
+ String testClasses = System.getProperty("test.classes", ".");
+
+ test(2);
+ test(0, "-help");
+ test(0, "-version");
+ test(0, "-fullversion");
+ test(0, "-classpath", testClasses, "T6715251");
+
+ if (errors > 0)
+ throw new Exception(errors + " errors received");
+ }
+
+ void test(int expect, String ... args) {
+ int rc = javap(args);
+ if (rc != expect)
+ error("bad result: expected: " + expect + ", found " + rc + "\n"
+ + log);
+
+ }
+
+ int javap(String... args) {
+ StringWriter sw = new StringWriter();
+ PrintWriter pw = new PrintWriter(sw);
+ int rc = com.sun.tools.javap.Main.run(args, pw);
+ log = sw.toString();
+ return rc;
+ }
+
+ void error(String msg) {
+ System.err.println(msg);
+ errors++;
+ }
+
+ String log;
+ int errors;
+}
\ No newline at end of file