提交 6cc1c39b 编写于 作者: J jjg

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

Reviewed-by: ksrini
上级 e6180220
......@@ -306,14 +306,32 @@ public class JavapTask implements DisassemblerTool.DisassemblerTask {
};
}
/** 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 @@ public class JavapTask implements DisassemblerTool.DisassemblerTask {
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 @@ public class JavapTask implements DisassemblerTool.DisassemblerTask {
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 @@ public class JavapTask implements DisassemblerTool.DisassemblerTask {
((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 class JavapTask implements DisassemblerTool.DisassemblerTask {
}
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);
......
......@@ -23,7 +23,7 @@
/*
* @test
* @bug 4876942
* @bug 4876942 6715251
* @summary javap invoked without args does not print help screen
*/
......@@ -48,7 +48,7 @@ public class T4876942 {
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();
......
/*
* 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
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册