7108668: allow Log to be initialized and used earlier
authorjjg
Tue, 08 Nov 2011 17:06:08 -0800
changeset 11052 65b9fa7eaf55
parent 10950 e87b50888909
child 11053 48713f779b1d
7108668: allow Log to be initialized and used earlier Reviewed-by: mcimadamore
langtools/src/share/classes/com/sun/tools/apt/comp/Apt.java
langtools/src/share/classes/com/sun/tools/javac/api/JavacTool.java
langtools/src/share/classes/com/sun/tools/javac/jvm/ClassWriter.java
langtools/src/share/classes/com/sun/tools/javac/main/JavaCompiler.java
langtools/src/share/classes/com/sun/tools/javac/main/Main.java
langtools/src/share/classes/com/sun/tools/javac/util/JCDiagnostic.java
langtools/src/share/classes/com/sun/tools/javac/util/JavacMessages.java
langtools/src/share/classes/com/sun/tools/javac/util/Log.java
langtools/src/share/classes/com/sun/tools/javac/util/Options.java
langtools/src/share/classes/com/sun/tools/javadoc/Start.java
--- a/langtools/src/share/classes/com/sun/tools/apt/comp/Apt.java	Tue Nov 08 11:51:05 2011 -0800
+++ b/langtools/src/share/classes/com/sun/tools/apt/comp/Apt.java	Tue Nov 08 17:06:08 2011 -0800
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2004, 2010, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2004, 2011, Oracle and/or its affiliates. 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
@@ -214,7 +214,7 @@
                      AnnotationProcessorFactory providedFactory,
                      java.util.Set<Class<? extends AnnotationProcessorFactory> > productiveFactories) {
         Bark bark = Bark.instance(context);
-        java.io.PrintWriter out = bark.warnWriter;
+        java.io.PrintWriter out = bark.getWriter(Log.WriterKind.WARNING);
         Options options = Options.instance(context);
 
         Collection<TypeDeclaration> spectypedecls =     new LinkedHashSet<TypeDeclaration>();
--- a/langtools/src/share/classes/com/sun/tools/javac/api/JavacTool.java	Tue Nov 08 11:51:05 2011 -0800
+++ b/langtools/src/share/classes/com/sun/tools/javac/api/JavacTool.java	Tue Nov 08 17:06:08 2011 -0800
@@ -250,6 +250,8 @@
                     throw new IllegalArgumentException(flag);
             }
         }
+
+        optionTable.notifyListeners();
     }
 
     public int run(InputStream in, OutputStream out, OutputStream err, String... arguments) {
--- a/langtools/src/share/classes/com/sun/tools/javac/jvm/ClassWriter.java	Tue Nov 08 11:51:05 2011 -0800
+++ b/langtools/src/share/classes/com/sun/tools/javac/jvm/ClassWriter.java	Tue Nov 08 17:06:08 2011 -0800
@@ -892,8 +892,9 @@
             if ((flags & INTERFACE) != 0) flags |= ABSTRACT; // Interfaces are always ABSTRACT
             if (inner.name.isEmpty()) flags &= ~FINAL; // Anonymous class: unset FINAL flag
             if (dumpInnerClassModifiers) {
-                log.errWriter.println("INNERCLASS  " + inner.name);
-                log.errWriter.println("---" + flagNames(flags));
+                PrintWriter pw = log.getWriter(Log.WriterKind.ERROR);
+                pw.println("INNERCLASS  " + inner.name);
+                pw.println("---" + flagNames(flags));
             }
             databuf.appendChar(pool.get(inner));
             databuf.appendChar(
@@ -911,8 +912,9 @@
         int flags = adjustFlags(v.flags());
         databuf.appendChar(flags);
         if (dumpFieldModifiers) {
-            log.errWriter.println("FIELD  " + fieldName(v));
-            log.errWriter.println("---" + flagNames(v.flags()));
+            PrintWriter pw = log.getWriter(Log.WriterKind.ERROR);
+            pw.println("FIELD  " + fieldName(v));
+            pw.println("---" + flagNames(v.flags()));
         }
         databuf.appendChar(pool.put(fieldName(v)));
         databuf.appendChar(pool.put(typeSig(v.erasure(types))));
@@ -934,8 +936,9 @@
         int flags = adjustFlags(m.flags());
         databuf.appendChar(flags);
         if (dumpMethodModifiers) {
-            log.errWriter.println("METHOD  " + fieldName(m));
-            log.errWriter.println("---" + flagNames(m.flags()));
+            PrintWriter pw = log.getWriter(Log.WriterKind.ERROR);
+            pw.println("METHOD  " + fieldName(m));
+            pw.println("---" + flagNames(m.flags()));
         }
         databuf.appendChar(pool.put(fieldName(m)));
         databuf.appendChar(pool.put(typeSig(m.externalType(types))));
@@ -1483,9 +1486,10 @@
         if ((flags & INTERFACE) == 0) flags |= ACC_SUPER;
         if (c.isInner() && c.name.isEmpty()) flags &= ~FINAL;
         if (dumpClassModifiers) {
-            log.errWriter.println();
-            log.errWriter.println("CLASSFILE  " + c.getQualifiedName());
-            log.errWriter.println("---" + flagNames(flags));
+            PrintWriter pw = log.getWriter(Log.WriterKind.ERROR);
+            pw.println();
+            pw.println("CLASSFILE  " + c.getQualifiedName());
+            pw.println("---" + flagNames(flags));
         }
         databuf.appendChar(flags);
 
--- a/langtools/src/share/classes/com/sun/tools/javac/main/JavaCompiler.java	Tue Nov 08 11:51:05 2011 -0800
+++ b/langtools/src/share/classes/com/sun/tools/javac/main/JavaCompiler.java	Tue Nov 08 17:06:08 2011 -0800
@@ -1602,7 +1602,7 @@
     }
 
     protected void printNote(String lines) {
-        Log.printLines(log.noticeWriter, lines);
+        log.printLines(Log.WriterKind.NOTICE, lines);
     }
 
     /** Print numbers of errors and warnings.
@@ -1615,7 +1615,7 @@
             else
                 key = "count." + kind + ".plural";
             log.printErrLines(key, String.valueOf(count));
-            log.errWriter.flush();
+            log.flush(Log.WriterKind.ERROR);
         }
     }
 
--- a/langtools/src/share/classes/com/sun/tools/javac/main/Main.java	Tue Nov 08 11:51:05 2011 -0800
+++ b/langtools/src/share/classes/com/sun/tools/javac/main/Main.java	Tue Nov 08 17:06:08 2011 -0800
@@ -67,6 +67,10 @@
      */
     PrintWriter out;
 
+    /** The log to use for diagnostic output.
+     */
+    Log log;
+
     /**
      * If true, certain errors will cause an exception, such as command line
      * arg errors, or exceptions in user provided code.
@@ -98,6 +102,7 @@
 
         public void setOut(PrintWriter out) {
             Main.this.out = out;
+            Main.this.log.setWriters(out);
         }
 
         public void error(String key, Object... args) {
@@ -307,6 +312,8 @@
             showClass(showClass);
         }
 
+        options.notifyListeners();
+
         return filenames;
     }
     // where
@@ -352,6 +359,9 @@
                        List<JavaFileObject> fileObjects,
                        Iterable<? extends Processor> processors)
     {
+        context.put(Log.outKey, out);
+        log = Log.instance(context);
+
         if (options == null)
             options = Options.instance(context); // creates a new one
 
@@ -398,12 +408,11 @@
 
             boolean forceStdOut = options.isSet("stdout");
             if (forceStdOut) {
-                out.flush();
+                log.flush();
                 out = new PrintWriter(System.out, true);
+                log.setWriters(out);
             }
 
-            context.put(Log.outKey, out);
-
             // allow System property in following line as a Mustang legacy
             boolean batchMode = (options.isUnset("nonBatchMode")
                         && System.getProperty("nonBatchMode") == null);
@@ -415,8 +424,6 @@
             comp = JavaCompiler.instance(context);
             if (comp == null) return Result.SYSERR;
 
-            Log log = Log.instance(context);
-
             if (!files.isEmpty()) {
                 // add filenames to fileObjects
                 comp = JavaCompiler.instance(context);
@@ -433,10 +440,10 @@
 
             if (log.expectDiagKeys != null) {
                 if (log.expectDiagKeys.isEmpty()) {
-                    Log.printLines(log.noticeWriter, "all expected diagnostics found");
+                    log.printLines(Log.WriterKind.NOTICE, "all expected diagnostics found");
                     return Result.OK;
                 } else {
-                    Log.printLines(log.noticeWriter, "expected diagnostic keys not found: " + log.expectDiagKeys);
+                    log.printLines(Log.WriterKind.NOTICE, "expected diagnostic keys not found: " + log.expectDiagKeys);
                     return Result.ERROR;
                 }
             }
--- a/langtools/src/share/classes/com/sun/tools/javac/util/JCDiagnostic.java	Tue Nov 08 11:51:05 2011 -0800
+++ b/langtools/src/share/classes/com/sun/tools/javac/util/JCDiagnostic.java	Tue Nov 08 17:06:08 2011 -0800
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2003, 2011, Oracle and/or its affiliates. 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
@@ -70,7 +70,16 @@
             this(JavacMessages.instance(context), "compiler");
             context.put(diagnosticFactoryKey, this);
 
-            Options options = Options.instance(context);
+            final Options options = Options.instance(context);
+            initOptions(options);
+            options.addListener(new Runnable() {
+               public void run() {
+                   initOptions(options);
+               }
+            });
+        }
+
+        private void initOptions(Options options) {
             if (options.isSet("onlySyntaxErrorsUnrecoverable"))
                 defaultErrorFlags.add(DiagnosticFlag.RECOVERABLE);
         }
--- a/langtools/src/share/classes/com/sun/tools/javac/util/JavacMessages.java	Tue Nov 08 11:51:05 2011 -0800
+++ b/langtools/src/share/classes/com/sun/tools/javac/util/JavacMessages.java	Tue Nov 08 17:06:08 2011 -0800
@@ -82,14 +82,14 @@
     }
 
     /** Creates a JavacMessages object.
-     * @param bundleName the name to identify the resource buundle of localized messages.
+     * @param bundleName the name to identify the resource bundle of localized messages.
      */
     public JavacMessages(String bundleName) throws MissingResourceException {
         this(bundleName, null);
     }
 
     /** Creates a JavacMessages object.
-     * @param bundleName the name to identify the resource buundle of localized messages.
+     * @param bundleName the name to identify the resource bundle of localized messages.
      */
     public JavacMessages(String bundleName, Locale locale) throws MissingResourceException {
         bundleNames = List.nil();
--- a/langtools/src/share/classes/com/sun/tools/javac/util/Log.java	Tue Nov 08 11:51:05 2011 -0800
+++ b/langtools/src/share/classes/com/sun/tools/javac/util/Log.java	Tue Nov 08 17:06:08 2011 -0800
@@ -60,19 +60,18 @@
     public static final Context.Key<PrintWriter> outKey =
         new Context.Key<PrintWriter>();
 
-    //@Deprecated
-    public final PrintWriter errWriter;
+    public enum WriterKind { NOTICE, WARNING, ERROR };
+
+    protected PrintWriter errWriter;
 
-    //@Deprecated
-    public final PrintWriter warnWriter;
+    protected PrintWriter warnWriter;
 
-    //@Deprecated
-    public final PrintWriter noticeWriter;
+    protected PrintWriter noticeWriter;
 
     /** The maximum number of errors/warnings that are reported.
      */
-    public final int MaxErrors;
-    public final int MaxWarnings;
+    protected int MaxErrors;
+    protected int MaxWarnings;
 
     /** Switch: prompt user on each error.
      */
@@ -131,28 +130,39 @@
         this.warnWriter = warnWriter;
         this.noticeWriter = noticeWriter;
 
-        Options options = Options.instance(context);
-        this.dumpOnError = options.isSet(DOE);
-        this.promptOnError = options.isSet(PROMPT);
-        this.emitWarnings = options.isUnset(XLINT_CUSTOM, "none");
-        this.suppressNotes = options.isSet("suppressNotes");
-        this.MaxErrors = getIntOption(options, XMAXERRS, getDefaultMaxErrors());
-        this.MaxWarnings = getIntOption(options, XMAXWARNS, getDefaultMaxWarnings());
-
-        boolean rawDiagnostics = options.isSet("rawDiagnostics");
-        messages = JavacMessages.instance(context);
-        this.diagFormatter = rawDiagnostics ? new RawDiagnosticFormatter(options) :
-                                              new BasicDiagnosticFormatter(options, messages);
         @SuppressWarnings("unchecked") // FIXME
         DiagnosticListener<? super JavaFileObject> dl =
             context.get(DiagnosticListener.class);
         this.diagListener = dl;
 
-        String ek = options.get("expectKeys");
-        if (ek != null)
-            expectDiagKeys = new HashSet<String>(Arrays.asList(ek.split(", *")));
+        messages = JavacMessages.instance(context);
+
+        final Options options = Options.instance(context);
+        initOptions(options);
+        options.addListener(new Runnable() {
+            public void run() {
+                initOptions(options);
+            }
+        });
     }
     // where
+        private void initOptions(Options options) {
+            this.dumpOnError = options.isSet(DOE);
+            this.promptOnError = options.isSet(PROMPT);
+            this.emitWarnings = options.isUnset(XLINT_CUSTOM, "none");
+            this.suppressNotes = options.isSet("suppressNotes");
+            this.MaxErrors = getIntOption(options, XMAXERRS, getDefaultMaxErrors());
+            this.MaxWarnings = getIntOption(options, XMAXWARNS, getDefaultMaxWarnings());
+
+            boolean rawDiagnostics = options.isSet("rawDiagnostics");
+            this.diagFormatter = rawDiagnostics ? new RawDiagnosticFormatter(options) :
+                                                  new BasicDiagnosticFormatter(options, messages);
+
+            String ek = options.get("expectKeys");
+            if (ek != null)
+                expectDiagKeys = new HashSet<String>(Arrays.asList(ek.split(", *")));
+        }
+
         private int getIntOption(Options options, OptionName optionName, int defaultValue) {
             String s = options.get(optionName);
             try {
@@ -180,7 +190,7 @@
 
     /** The default writer for diagnostics
      */
-    static final PrintWriter defaultWriter(Context context) {
+    static PrintWriter defaultWriter(Context context) {
         PrintWriter result = context.get(outKey);
         if (result == null)
             context.put(outKey, result = new PrintWriter(System.err));
@@ -248,6 +258,30 @@
         this.diagFormatter = diagFormatter;
     }
 
+    public PrintWriter getWriter(WriterKind kind) {
+        switch (kind) {
+            case NOTICE:    return noticeWriter;
+            case WARNING:   return warnWriter;
+            case ERROR:     return errWriter;
+            default:        throw new IllegalArgumentException();
+        }
+    }
+
+    public void setWriter(WriterKind kind, PrintWriter pw) {
+        pw.getClass();
+        switch (kind) {
+            case NOTICE:    noticeWriter = pw;  break;
+            case WARNING:   warnWriter = pw;    break;
+            case ERROR:     errWriter = pw;     break;
+            default:        throw new IllegalArgumentException();
+        }
+    }
+
+    public void setWriters(PrintWriter pw) {
+        pw.getClass();
+        noticeWriter = warnWriter = errWriter = pw;
+    }
+
     /** Flush the logs
      */
     public void flush() {
@@ -256,6 +290,10 @@
         noticeWriter.flush();
     }
 
+    public void flush(WriterKind kind) {
+        getWriter(kind).flush();
+    }
+
     /** Returns true if an error needs to be reported for a given
      * source name and pos.
      */
@@ -313,6 +351,13 @@
     /** Print the text of a message, translating newlines appropriately
      *  for the platform.
      */
+    public void printLines(WriterKind kind, String msg) {
+        printLines(getWriter(kind), msg);
+    }
+
+    /** Print the text of a message, translating newlines appropriately
+     *  for the platform.
+     */
     public static void printLines(PrintWriter writer, String msg) {
         int nl;
         while ((nl = msg.indexOf('\n')) != -1) {
--- a/langtools/src/share/classes/com/sun/tools/javac/util/Options.java	Tue Nov 08 11:51:05 2011 -0800
+++ b/langtools/src/share/classes/com/sun/tools/javac/util/Options.java	Tue Nov 08 17:06:08 2011 -0800
@@ -157,6 +157,19 @@
         return values.size();
     }
 
+    // light-weight notification mechanism
+
+    private List<Runnable> listeners = List.nil();
+
+    public void addListener(Runnable listener) {
+        listeners = listeners.prepend(listener);
+    }
+
+    public void notifyListeners() {
+        for (Runnable r: listeners)
+            r.run();
+    }
+
     /** Check for a lint suboption. */
     public boolean lint(String s) {
         // return true if either the specific option is enabled, or
--- a/langtools/src/share/classes/com/sun/tools/javadoc/Start.java	Tue Nov 08 11:51:05 2011 -0800
+++ b/langtools/src/share/classes/com/sun/tools/javadoc/Start.java	Tue Nov 08 17:06:08 2011 -0800
@@ -31,6 +31,7 @@
 import com.sun.tools.javac.util.Context;
 import com.sun.tools.javac.util.List;
 import com.sun.tools.javac.util.ListBuffer;
+import com.sun.tools.javac.util.Log;
 import com.sun.tools.javac.util.Options;
 
 import java.io.IOException;
@@ -76,9 +77,6 @@
 
     private DocletInvoker docletInvoker;
 
-    private static final int F_VERBOSE = 1 << 0;
-    private static final int F_WARNINGS = 1 << 2;
-
     /* Treat warnings as errors. */
     private boolean rejectWarnings = false;
 
@@ -171,11 +169,11 @@
             messager.error(null, "main.out.of.memory");
             failed = true;
         } catch (Error ee) {
-            ee.printStackTrace();
+            ee.printStackTrace(System.err);
             messager.error(null, "main.fatal.error");
             failed = true;
         } catch (Exception ee) {
-            ee.printStackTrace();
+            ee.printStackTrace(System.err);
             messager.error(null, "main.fatal.exception");
             failed = true;
         } finally {
@@ -211,7 +209,7 @@
             messager.error(null, "main.cant.read", e.getMessage());
             exit();
         } catch (IOException e) {
-            e.printStackTrace();
+            e.printStackTrace(System.err);
             exit();
         }
 
@@ -225,7 +223,9 @@
         // options that may be set up below.
         Messager.preRegister(context,
                 messager.programName,
-                messager.errWriter, messager.warnWriter, messager.noticeWriter);
+                messager.getWriter(Log.WriterKind.ERROR),
+                messager.getWriter(Log.WriterKind.WARNING),
+                messager.getWriter(Log.WriterKind.NOTICE));
 
         Options compOpts = Options.instance(context);
         boolean docClasses = false;