Thu, 22 Sep 2011 09:24:01 -0700
7075721: javac should have public enum for exit codes
Reviewed-by: mcimadamore
1.1 --- a/src/share/classes/com/sun/tools/javac/Main.java Wed Sep 21 21:56:53 2011 -0700 1.2 +++ b/src/share/classes/com/sun/tools/javac/Main.java Thu Sep 22 09:24:01 2011 -0700 1.3 @@ -73,7 +73,7 @@ 1.4 public static int compile(String[] args) { 1.5 com.sun.tools.javac.main.Main compiler = 1.6 new com.sun.tools.javac.main.Main("javac"); 1.7 - return compiler.compile(args); 1.8 + return compiler.compile(args).exitCode; 1.9 } 1.10 1.11 1.12 @@ -91,6 +91,6 @@ 1.13 public static int compile(String[] args, PrintWriter out) { 1.14 com.sun.tools.javac.main.Main compiler = 1.15 new com.sun.tools.javac.main.Main("javac", out); 1.16 - return compiler.compile(args); 1.17 + return compiler.compile(args).exitCode; 1.18 } 1.19 }
2.1 --- a/src/share/classes/com/sun/tools/javac/api/JavacTaskImpl.java Wed Sep 21 21:56:53 2011 -0700 2.2 +++ b/src/share/classes/com/sun/tools/javac/api/JavacTaskImpl.java Thu Sep 22 09:24:01 2011 -0700 2.3 @@ -78,7 +78,7 @@ 2.4 private AtomicBoolean used = new AtomicBoolean(); 2.5 private Iterable<? extends Processor> processors; 2.6 2.7 - private Integer result = null; 2.8 + private Main.Result result = null; 2.9 2.10 JavacTaskImpl(Main compilerMain, 2.11 String[] args, 2.12 @@ -131,7 +131,7 @@ 2.13 compilerMain.setAPIMode(true); 2.14 result = compilerMain.compile(args, context, fileObjects, processors); 2.15 cleanup(); 2.16 - return result == 0; 2.17 + return result.isOK(); 2.18 } else { 2.19 throw new IllegalStateException("multiple calls to method 'call'"); 2.20 }
3.1 --- a/src/share/classes/com/sun/tools/javac/main/Main.java Wed Sep 21 21:56:53 2011 -0700 3.2 +++ b/src/share/classes/com/sun/tools/javac/main/Main.java Thu Sep 22 09:24:01 2011 -0700 3.3 @@ -76,12 +76,23 @@ 3.4 3.5 /** Result codes. 3.6 */ 3.7 - static final int 3.8 - EXIT_OK = 0, // Compilation completed with no errors. 3.9 - EXIT_ERROR = 1, // Completed but reported errors. 3.10 - EXIT_CMDERR = 2, // Bad command-line arguments 3.11 - EXIT_SYSERR = 3, // System error or resource exhaustion. 3.12 - EXIT_ABNORMAL = 4; // Compiler terminated abnormally 3.13 + public enum Result { 3.14 + OK(0), // Compilation completed with no errors. 3.15 + ERROR(1), // Completed but reported errors. 3.16 + CMDERR(2), // Bad command-line arguments 3.17 + SYSERR(3), // System error or resource exhaustion. 3.18 + ABNORMAL(4); // Compiler terminated abnormally 3.19 + 3.20 + Result(int exitCode) { 3.21 + this.exitCode = exitCode; 3.22 + } 3.23 + 3.24 + public boolean isOK() { 3.25 + return (exitCode == 0); 3.26 + } 3.27 + 3.28 + public final int exitCode; 3.29 + } 3.30 3.31 private Option[] recognizedOptions = RecognizedOptions.getJavaCompilerOptions(new OptionHelper() { 3.32 3.33 @@ -318,10 +329,10 @@ 3.34 /** Programmatic interface for main function. 3.35 * @param args The command line parameters. 3.36 */ 3.37 - public int compile(String[] args) { 3.38 + public Result compile(String[] args) { 3.39 Context context = new Context(); 3.40 JavacFileManager.preRegister(context); // can't create it until Log has been set up 3.41 - int result = compile(args, context); 3.42 + Result result = compile(args, context); 3.43 if (fileManager instanceof JavacFileManager) { 3.44 // A fresh context was created above, so jfm must be a JavacFileManager 3.45 ((JavacFileManager)fileManager).close(); 3.46 @@ -329,14 +340,14 @@ 3.47 return result; 3.48 } 3.49 3.50 - public int compile(String[] args, Context context) { 3.51 + public Result compile(String[] args, Context context) { 3.52 return compile(args, context, List.<JavaFileObject>nil(), null); 3.53 } 3.54 3.55 /** Programmatic interface for main function. 3.56 * @param args The command line parameters. 3.57 */ 3.58 - public int compile(String[] args, 3.59 + public Result compile(String[] args, 3.60 Context context, 3.61 List<JavaFileObject> fileObjects, 3.62 Iterable<? extends Processor> processors) 3.63 @@ -355,7 +366,7 @@ 3.64 try { 3.65 if (args.length == 0 && fileObjects.isEmpty()) { 3.66 help(); 3.67 - return EXIT_CMDERR; 3.68 + return Result.CMDERR; 3.69 } 3.70 3.71 Collection<File> files; 3.72 @@ -363,26 +374,26 @@ 3.73 files = processArgs(CommandLine.parse(args)); 3.74 if (files == null) { 3.75 // null signals an error in options, abort 3.76 - return EXIT_CMDERR; 3.77 + return Result.CMDERR; 3.78 } else if (files.isEmpty() && fileObjects.isEmpty() && classnames.isEmpty()) { 3.79 // it is allowed to compile nothing if just asking for help or version info 3.80 if (options.isSet(HELP) 3.81 || options.isSet(X) 3.82 || options.isSet(VERSION) 3.83 || options.isSet(FULLVERSION)) 3.84 - return EXIT_OK; 3.85 + return Result.OK; 3.86 if (JavaCompiler.explicitAnnotationProcessingRequested(options)) { 3.87 error("err.no.source.files.classes"); 3.88 } else { 3.89 error("err.no.source.files"); 3.90 } 3.91 - return EXIT_CMDERR; 3.92 + return Result.CMDERR; 3.93 } 3.94 } catch (java.io.FileNotFoundException e) { 3.95 Log.printLines(out, ownName + ": " + 3.96 getLocalizedString("err.file.not.found", 3.97 e.getMessage())); 3.98 - return EXIT_SYSERR; 3.99 + return Result.SYSERR; 3.100 } 3.101 3.102 boolean forceStdOut = options.isSet("stdout"); 3.103 @@ -402,7 +413,7 @@ 3.104 fileManager = context.get(JavaFileManager.class); 3.105 3.106 comp = JavaCompiler.instance(context); 3.107 - if (comp == null) return EXIT_SYSERR; 3.108 + if (comp == null) return Result.SYSERR; 3.109 3.110 Log log = Log.instance(context); 3.111 3.112 @@ -423,32 +434,32 @@ 3.113 if (log.expectDiagKeys != null) { 3.114 if (log.expectDiagKeys.isEmpty()) { 3.115 Log.printLines(log.noticeWriter, "all expected diagnostics found"); 3.116 - return EXIT_OK; 3.117 + return Result.OK; 3.118 } else { 3.119 Log.printLines(log.noticeWriter, "expected diagnostic keys not found: " + log.expectDiagKeys); 3.120 - return EXIT_ERROR; 3.121 + return Result.ERROR; 3.122 } 3.123 } 3.124 3.125 if (comp.errorCount() != 0) 3.126 - return EXIT_ERROR; 3.127 + return Result.ERROR; 3.128 } catch (IOException ex) { 3.129 ioMessage(ex); 3.130 - return EXIT_SYSERR; 3.131 + return Result.SYSERR; 3.132 } catch (OutOfMemoryError ex) { 3.133 resourceMessage(ex); 3.134 - return EXIT_SYSERR; 3.135 + return Result.SYSERR; 3.136 } catch (StackOverflowError ex) { 3.137 resourceMessage(ex); 3.138 - return EXIT_SYSERR; 3.139 + return Result.SYSERR; 3.140 } catch (FatalError ex) { 3.141 feMessage(ex); 3.142 - return EXIT_SYSERR; 3.143 + return Result.SYSERR; 3.144 } catch (AnnotationProcessingError ex) { 3.145 if (apiMode) 3.146 throw new RuntimeException(ex.getCause()); 3.147 apMessage(ex); 3.148 - return EXIT_SYSERR; 3.149 + return Result.SYSERR; 3.150 } catch (ClientCodeException ex) { 3.151 // as specified by javax.tools.JavaCompiler#getTask 3.152 // and javax.tools.JavaCompiler.CompilationTask#call 3.153 @@ -462,7 +473,7 @@ 3.154 if (comp == null || comp.errorCount() == 0 || 3.155 options == null || options.isSet("dev")) 3.156 bugMessage(ex); 3.157 - return EXIT_ABNORMAL; 3.158 + return Result.ABNORMAL; 3.159 } finally { 3.160 if (comp != null) { 3.161 try { 3.162 @@ -474,7 +485,7 @@ 3.163 filenames = null; 3.164 options = null; 3.165 } 3.166 - return EXIT_OK; 3.167 + return Result.OK; 3.168 } 3.169 3.170 /** Print a message reporting an internal error.
4.1 --- a/test/tools/javac/diags/ArgTypeCompilerFactory.java Wed Sep 21 21:56:53 2011 -0700 4.2 +++ b/test/tools/javac/diags/ArgTypeCompilerFactory.java Thu Sep 22 09:24:01 2011 -0700 4.3 @@ -146,9 +146,9 @@ 4.4 JavacFileManager.preRegister(c); // can't create it until Log has been set up 4.5 ArgTypeJavaCompiler.preRegister(c); 4.6 ArgTypeMessages.preRegister(c); 4.7 - int result = main.compile(args.toArray(new String[args.size()]), c); 4.8 + Main.Result result = main.compile(args.toArray(new String[args.size()]), c); 4.9 4.10 - return (result == 0); 4.11 + return result.isOK(); 4.12 } 4.13 } 4.14 4.15 @@ -172,10 +172,10 @@ 4.16 JavacFileManager.preRegister(c); // can't create it until Log has been set up 4.17 ArgTypeJavaCompiler.preRegister(c); 4.18 ArgTypeMessages.preRegister(c); 4.19 - com.sun.tools.javac.main.Main m = new com.sun.tools.javac.main.Main("javac", out); 4.20 - int rc = m.compile(args.toArray(new String[args.size()]), c); 4.21 + Main m = new Main("javac", out); 4.22 + Main.Result result = m.compile(args.toArray(new String[args.size()]), c); 4.23 4.24 - return (rc == 0); 4.25 + return result.isOK(); 4.26 } 4.27 4.28 }
5.1 --- a/test/tools/javac/diags/Example.java Wed Sep 21 21:56:53 2011 -0700 5.2 +++ b/test/tools/javac/diags/Example.java Thu Sep 22 09:24:01 2011 -0700 5.3 @@ -41,6 +41,7 @@ 5.4 5.5 import com.sun.tools.javac.api.ClientCodeWrapper; 5.6 import com.sun.tools.javac.file.JavacFileManager; 5.7 +import com.sun.tools.javac.main.Main; 5.8 import com.sun.tools.javac.util.Context; 5.9 import com.sun.tools.javac.util.JavacMessages; 5.10 import com.sun.tools.javac.util.JCDiagnostic; 5.11 @@ -515,14 +516,14 @@ 5.12 Context c = new Context(); 5.13 JavacFileManager.preRegister(c); // can't create it until Log has been set up 5.14 MessageTracker.preRegister(c, keys); 5.15 - com.sun.tools.javac.main.Main m = new com.sun.tools.javac.main.Main("javac", pw); 5.16 - int rc = m.compile(args.toArray(new String[args.size()]), c); 5.17 + Main m = new Main("javac", pw); 5.18 + Main.Result rc = m.compile(args.toArray(new String[args.size()]), c); 5.19 5.20 if (keys != null) { 5.21 pw.close(); 5.22 } 5.23 5.24 - return (rc == 0); 5.25 + return rc.isOK(); 5.26 } 5.27 5.28 static class MessageTracker extends JavacMessages {
6.1 --- a/test/tools/javac/lib/CompileFail.java Wed Sep 21 21:56:53 2011 -0700 6.2 +++ b/test/tools/javac/lib/CompileFail.java Thu Sep 22 09:24:01 2011 -0700 6.3 @@ -23,6 +23,7 @@ 6.4 6.5 import java.io.*; 6.6 import java.util.*; 6.7 +import com.sun.tools.javac.main.Main; 6.8 6.9 /* 6.10 * Utility class to emulate jtreg @compile/fail, but also checking the specific 6.11 @@ -58,32 +59,7 @@ 6.12 } 6.13 6.14 static int getReturnCode(String name) { 6.15 - switch (name) { 6.16 - case "OK": 6.17 - return EXIT_OK; 6.18 - 6.19 - case "ERROR": 6.20 - return EXIT_ERROR; 6.21 - 6.22 - case "CMDERR": 6.23 - return EXIT_CMDERR; 6.24 - 6.25 - case "SYSERR": 6.26 - return EXIT_SYSERR; 6.27 - 6.28 - case "ABNORMAL": 6.29 - return EXIT_ABNORMAL; 6.30 - 6.31 - default: 6.32 - throw new IllegalArgumentException(name); 6.33 - } 6.34 + return Main.Result.valueOf(name).exitCode; 6.35 } 6.36 6.37 - // The following is cut-n-paste from com.sun.tools.javac.main.Main 6.38 - static final int 6.39 - EXIT_OK = 0, // Compilation completed with no errors. 6.40 - EXIT_ERROR = 1, // Completed but reported errors. 6.41 - EXIT_CMDERR = 2, // Bad command-line arguments 6.42 - EXIT_SYSERR = 3, // System error or resource exhaustion. 6.43 - EXIT_ABNORMAL = 4; // Compiler terminated abnormally 6.44 }
7.1 --- a/test/tools/javac/util/context/T7021650.java Wed Sep 21 21:56:53 2011 -0700 7.2 +++ b/test/tools/javac/util/context/T7021650.java Thu Sep 22 09:24:01 2011 -0700 7.3 @@ -101,13 +101,13 @@ 7.4 StringWriter sw = new StringWriter(); 7.5 PrintWriter pw = new PrintWriter(sw); 7.6 Main m = new Main("javac", pw); 7.7 - int rc = m.compile(args, context); 7.8 + Main.Result res = m.compile(args, context); 7.9 pw.close(); 7.10 String out = sw.toString(); 7.11 if (!out.isEmpty()) 7.12 System.err.println(out); 7.13 - if (rc != 0) 7.14 - throw new Exception("compilation failed unexpectedly: rc=" + rc); 7.15 + if (!res.isOK()) 7.16 + throw new Exception("compilation failed unexpectedly: result=" + res); 7.17 } 7.18 7.19 void checkEqual(String label, int found, int expect) throws Exception {