7075721: javac should have public enum for exit codes
authorjjg
Thu, 22 Sep 2011 09:24:01 -0700
changeset 10638 c8e9604cf151
parent 10637 2ea5fbb913ac
child 10639 d22d3f1f29a2
7075721: javac should have public enum for exit codes Reviewed-by: mcimadamore
langtools/src/share/classes/com/sun/tools/javac/Main.java
langtools/src/share/classes/com/sun/tools/javac/api/JavacTaskImpl.java
langtools/src/share/classes/com/sun/tools/javac/main/Main.java
langtools/test/tools/javac/diags/ArgTypeCompilerFactory.java
langtools/test/tools/javac/diags/Example.java
langtools/test/tools/javac/lib/CompileFail.java
langtools/test/tools/javac/util/context/T7021650.java
--- a/langtools/src/share/classes/com/sun/tools/javac/Main.java	Wed Sep 21 21:56:53 2011 -0700
+++ b/langtools/src/share/classes/com/sun/tools/javac/Main.java	Thu Sep 22 09:24:01 2011 -0700
@@ -73,7 +73,7 @@
     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 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;
     }
 }
--- a/langtools/src/share/classes/com/sun/tools/javac/api/JavacTaskImpl.java	Wed Sep 21 21:56:53 2011 -0700
+++ b/langtools/src/share/classes/com/sun/tools/javac/api/JavacTaskImpl.java	Thu Sep 22 09:24:01 2011 -0700
@@ -78,7 +78,7 @@
     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 @@
             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'");
         }
--- a/langtools/src/share/classes/com/sun/tools/javac/main/Main.java	Wed Sep 21 21:56:53 2011 -0700
+++ b/langtools/src/share/classes/com/sun/tools/javac/main/Main.java	Thu Sep 22 09:24:01 2011 -0700
@@ -76,12 +76,23 @@
 
     /** 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 @@
     /** 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 @@
         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 @@
         try {
             if (args.length == 0 && fileObjects.isEmpty()) {
                 help();
-                return EXIT_CMDERR;
+                return Result.CMDERR;
             }
 
             Collection<File> files;
@@ -363,26 +374,26 @@
                 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 @@
             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 @@
             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 @@
             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 @@
             filenames = null;
             options = null;
         }
-        return EXIT_OK;
+        return Result.OK;
     }
 
     /** Print a message reporting an internal error.
--- a/langtools/test/tools/javac/diags/ArgTypeCompilerFactory.java	Wed Sep 21 21:56:53 2011 -0700
+++ b/langtools/test/tools/javac/diags/ArgTypeCompilerFactory.java	Thu Sep 22 09:24:01 2011 -0700
@@ -146,9 +146,9 @@
             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 @@
             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();
         }
 
     }
--- a/langtools/test/tools/javac/diags/Example.java	Wed Sep 21 21:56:53 2011 -0700
+++ b/langtools/test/tools/javac/diags/Example.java	Thu Sep 22 09:24:01 2011 -0700
@@ -41,6 +41,7 @@
 
 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 @@
             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 {
--- a/langtools/test/tools/javac/lib/CompileFail.java	Wed Sep 21 21:56:53 2011 -0700
+++ b/langtools/test/tools/javac/lib/CompileFail.java	Thu Sep 22 09:24:01 2011 -0700
@@ -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 @@
     }
 
     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
 }
--- a/langtools/test/tools/javac/util/context/T7021650.java	Wed Sep 21 21:56:53 2011 -0700
+++ b/langtools/test/tools/javac/util/context/T7021650.java	Thu Sep 22 09:24:01 2011 -0700
@@ -101,13 +101,13 @@
         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 {