提交 acc5bf50 编写于 作者: J jjg

7075721: javac should have public enum for exit codes

Reviewed-by: mcimadamore
上级 2e2265fa
......@@ -73,7 +73,7 @@ public class Main {
public static int compile(String[] args) {
com.sun.tools.javac.main.Main compiler =
new com.sun.tools.javac.main.Main("javac");
return compiler.compile(args);
return compiler.compile(args).exitCode;
}
......@@ -91,6 +91,6 @@ public class Main {
public static int compile(String[] args, PrintWriter out) {
com.sun.tools.javac.main.Main compiler =
new com.sun.tools.javac.main.Main("javac", out);
return compiler.compile(args);
return compiler.compile(args).exitCode;
}
}
......@@ -78,7 +78,7 @@ public class JavacTaskImpl extends JavacTask {
private AtomicBoolean used = new AtomicBoolean();
private Iterable<? extends Processor> processors;
private Integer result = null;
private Main.Result result = null;
JavacTaskImpl(Main compilerMain,
String[] args,
......@@ -131,7 +131,7 @@ public class JavacTaskImpl extends JavacTask {
compilerMain.setAPIMode(true);
result = compilerMain.compile(args, context, fileObjects, processors);
cleanup();
return result == 0;
return result.isOK();
} else {
throw new IllegalStateException("multiple calls to method 'call'");
}
......
......@@ -76,12 +76,23 @@ public class Main {
/** 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
public enum Result {
OK(0), // Compilation completed with no errors.
ERROR(1), // Completed but reported errors.
CMDERR(2), // Bad command-line arguments
SYSERR(3), // System error or resource exhaustion.
ABNORMAL(4); // Compiler terminated abnormally
Result(int exitCode) {
this.exitCode = exitCode;
}
public boolean isOK() {
return (exitCode == 0);
}
public final int exitCode;
}
private Option[] recognizedOptions = RecognizedOptions.getJavaCompilerOptions(new OptionHelper() {
......@@ -318,10 +329,10 @@ public class Main {
/** Programmatic interface for main function.
* @param args The command line parameters.
*/
public int compile(String[] args) {
public Result compile(String[] args) {
Context context = new Context();
JavacFileManager.preRegister(context); // can't create it until Log has been set up
int result = compile(args, context);
Result result = compile(args, context);
if (fileManager instanceof JavacFileManager) {
// A fresh context was created above, so jfm must be a JavacFileManager
((JavacFileManager)fileManager).close();
......@@ -329,14 +340,14 @@ public class Main {
return result;
}
public int compile(String[] args, Context context) {
public Result compile(String[] args, Context context) {
return compile(args, context, List.<JavaFileObject>nil(), null);
}
/** Programmatic interface for main function.
* @param args The command line parameters.
*/
public int compile(String[] args,
public Result compile(String[] args,
Context context,
List<JavaFileObject> fileObjects,
Iterable<? extends Processor> processors)
......@@ -355,7 +366,7 @@ public class Main {
try {
if (args.length == 0 && fileObjects.isEmpty()) {
help();
return EXIT_CMDERR;
return Result.CMDERR;
}
Collection<File> files;
......@@ -363,26 +374,26 @@ public class Main {
files = processArgs(CommandLine.parse(args));
if (files == null) {
// null signals an error in options, abort
return EXIT_CMDERR;
return Result.CMDERR;
} else if (files.isEmpty() && fileObjects.isEmpty() && classnames.isEmpty()) {
// it is allowed to compile nothing if just asking for help or version info
if (options.isSet(HELP)
|| options.isSet(X)
|| options.isSet(VERSION)
|| options.isSet(FULLVERSION))
return EXIT_OK;
return Result.OK;
if (JavaCompiler.explicitAnnotationProcessingRequested(options)) {
error("err.no.source.files.classes");
} else {
error("err.no.source.files");
}
return EXIT_CMDERR;
return Result.CMDERR;
}
} catch (java.io.FileNotFoundException e) {
Log.printLines(out, ownName + ": " +
getLocalizedString("err.file.not.found",
e.getMessage()));
return EXIT_SYSERR;
return Result.SYSERR;
}
boolean forceStdOut = options.isSet("stdout");
......@@ -402,7 +413,7 @@ public class Main {
fileManager = context.get(JavaFileManager.class);
comp = JavaCompiler.instance(context);
if (comp == null) return EXIT_SYSERR;
if (comp == null) return Result.SYSERR;
Log log = Log.instance(context);
......@@ -423,32 +434,32 @@ public class Main {
if (log.expectDiagKeys != null) {
if (log.expectDiagKeys.isEmpty()) {
Log.printLines(log.noticeWriter, "all expected diagnostics found");
return EXIT_OK;
return Result.OK;
} else {
Log.printLines(log.noticeWriter, "expected diagnostic keys not found: " + log.expectDiagKeys);
return EXIT_ERROR;
return Result.ERROR;
}
}
if (comp.errorCount() != 0)
return EXIT_ERROR;
return Result.ERROR;
} catch (IOException ex) {
ioMessage(ex);
return EXIT_SYSERR;
return Result.SYSERR;
} catch (OutOfMemoryError ex) {
resourceMessage(ex);
return EXIT_SYSERR;
return Result.SYSERR;
} catch (StackOverflowError ex) {
resourceMessage(ex);
return EXIT_SYSERR;
return Result.SYSERR;
} catch (FatalError ex) {
feMessage(ex);
return EXIT_SYSERR;
return Result.SYSERR;
} catch (AnnotationProcessingError ex) {
if (apiMode)
throw new RuntimeException(ex.getCause());
apMessage(ex);
return EXIT_SYSERR;
return Result.SYSERR;
} catch (ClientCodeException ex) {
// as specified by javax.tools.JavaCompiler#getTask
// and javax.tools.JavaCompiler.CompilationTask#call
......@@ -462,7 +473,7 @@ public class Main {
if (comp == null || comp.errorCount() == 0 ||
options == null || options.isSet("dev"))
bugMessage(ex);
return EXIT_ABNORMAL;
return Result.ABNORMAL;
} finally {
if (comp != null) {
try {
......@@ -474,7 +485,7 @@ public class Main {
filenames = null;
options = null;
}
return EXIT_OK;
return Result.OK;
}
/** Print a message reporting an internal error.
......
......@@ -146,9 +146,9 @@ class ArgTypeCompilerFactory implements Example.Compiler.Factory {
JavacFileManager.preRegister(c); // can't create it until Log has been set up
ArgTypeJavaCompiler.preRegister(c);
ArgTypeMessages.preRegister(c);
int result = main.compile(args.toArray(new String[args.size()]), c);
Main.Result result = main.compile(args.toArray(new String[args.size()]), c);
return (result == 0);
return result.isOK();
}
}
......@@ -172,10 +172,10 @@ class ArgTypeCompilerFactory implements Example.Compiler.Factory {
JavacFileManager.preRegister(c); // can't create it until Log has been set up
ArgTypeJavaCompiler.preRegister(c);
ArgTypeMessages.preRegister(c);
com.sun.tools.javac.main.Main m = new com.sun.tools.javac.main.Main("javac", out);
int rc = m.compile(args.toArray(new String[args.size()]), c);
Main m = new Main("javac", out);
Main.Result result = m.compile(args.toArray(new String[args.size()]), c);
return (rc == 0);
return result.isOK();
}
}
......
......@@ -41,6 +41,7 @@ import javax.tools.ToolProvider;
import com.sun.tools.javac.api.ClientCodeWrapper;
import com.sun.tools.javac.file.JavacFileManager;
import com.sun.tools.javac.main.Main;
import com.sun.tools.javac.util.Context;
import com.sun.tools.javac.util.JavacMessages;
import com.sun.tools.javac.util.JCDiagnostic;
......@@ -515,14 +516,14 @@ class Example implements Comparable<Example> {
Context c = new Context();
JavacFileManager.preRegister(c); // can't create it until Log has been set up
MessageTracker.preRegister(c, keys);
com.sun.tools.javac.main.Main m = new com.sun.tools.javac.main.Main("javac", pw);
int rc = m.compile(args.toArray(new String[args.size()]), c);
Main m = new Main("javac", pw);
Main.Result rc = m.compile(args.toArray(new String[args.size()]), c);
if (keys != null) {
pw.close();
}
return (rc == 0);
return rc.isOK();
}
static class MessageTracker extends JavacMessages {
......
......@@ -23,6 +23,7 @@
import java.io.*;
import java.util.*;
import com.sun.tools.javac.main.Main;
/*
* Utility class to emulate jtreg @compile/fail, but also checking the specific
......@@ -58,32 +59,7 @@ public class CompileFail {
}
static int getReturnCode(String name) {
switch (name) {
case "OK":
return EXIT_OK;
case "ERROR":
return EXIT_ERROR;
case "CMDERR":
return EXIT_CMDERR;
case "SYSERR":
return EXIT_SYSERR;
case "ABNORMAL":
return EXIT_ABNORMAL;
default:
throw new IllegalArgumentException(name);
}
return Main.Result.valueOf(name).exitCode;
}
// The following is cut-n-paste from com.sun.tools.javac.main.Main
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
}
......@@ -101,13 +101,13 @@ public class T7021650 extends JavacTestingAbstractProcessor {
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
Main m = new Main("javac", pw);
int rc = m.compile(args, context);
Main.Result res = m.compile(args, context);
pw.close();
String out = sw.toString();
if (!out.isEmpty())
System.err.println(out);
if (rc != 0)
throw new Exception("compilation failed unexpectedly: rc=" + rc);
if (!res.isOK())
throw new Exception("compilation failed unexpectedly: result=" + res);
}
void checkEqual(String label, int found, int expect) throws Exception {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册