Tue, 08 Nov 2011 17:06:08 -0800
7108668: allow Log to be initialized and used earlier
Reviewed-by: mcimadamore
1.1 --- a/src/share/classes/com/sun/tools/apt/comp/Apt.java Tue Nov 08 11:51:05 2011 -0800 1.2 +++ b/src/share/classes/com/sun/tools/apt/comp/Apt.java Tue Nov 08 17:06:08 2011 -0800 1.3 @@ -1,5 +1,5 @@ 1.4 /* 1.5 - * Copyright (c) 2004, 2010, Oracle and/or its affiliates. All rights reserved. 1.6 + * Copyright (c) 2004, 2011, Oracle and/or its affiliates. All rights reserved. 1.7 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.8 * 1.9 * This code is free software; you can redistribute it and/or modify it 1.10 @@ -214,7 +214,7 @@ 1.11 AnnotationProcessorFactory providedFactory, 1.12 java.util.Set<Class<? extends AnnotationProcessorFactory> > productiveFactories) { 1.13 Bark bark = Bark.instance(context); 1.14 - java.io.PrintWriter out = bark.warnWriter; 1.15 + java.io.PrintWriter out = bark.getWriter(Log.WriterKind.WARNING); 1.16 Options options = Options.instance(context); 1.17 1.18 Collection<TypeDeclaration> spectypedecls = new LinkedHashSet<TypeDeclaration>();
2.1 --- a/src/share/classes/com/sun/tools/javac/api/JavacTool.java Tue Nov 08 11:51:05 2011 -0800 2.2 +++ b/src/share/classes/com/sun/tools/javac/api/JavacTool.java Tue Nov 08 17:06:08 2011 -0800 2.3 @@ -250,6 +250,8 @@ 2.4 throw new IllegalArgumentException(flag); 2.5 } 2.6 } 2.7 + 2.8 + optionTable.notifyListeners(); 2.9 } 2.10 2.11 public int run(InputStream in, OutputStream out, OutputStream err, String... arguments) {
3.1 --- a/src/share/classes/com/sun/tools/javac/jvm/ClassWriter.java Tue Nov 08 11:51:05 2011 -0800 3.2 +++ b/src/share/classes/com/sun/tools/javac/jvm/ClassWriter.java Tue Nov 08 17:06:08 2011 -0800 3.3 @@ -892,8 +892,9 @@ 3.4 if ((flags & INTERFACE) != 0) flags |= ABSTRACT; // Interfaces are always ABSTRACT 3.5 if (inner.name.isEmpty()) flags &= ~FINAL; // Anonymous class: unset FINAL flag 3.6 if (dumpInnerClassModifiers) { 3.7 - log.errWriter.println("INNERCLASS " + inner.name); 3.8 - log.errWriter.println("---" + flagNames(flags)); 3.9 + PrintWriter pw = log.getWriter(Log.WriterKind.ERROR); 3.10 + pw.println("INNERCLASS " + inner.name); 3.11 + pw.println("---" + flagNames(flags)); 3.12 } 3.13 databuf.appendChar(pool.get(inner)); 3.14 databuf.appendChar( 3.15 @@ -911,8 +912,9 @@ 3.16 int flags = adjustFlags(v.flags()); 3.17 databuf.appendChar(flags); 3.18 if (dumpFieldModifiers) { 3.19 - log.errWriter.println("FIELD " + fieldName(v)); 3.20 - log.errWriter.println("---" + flagNames(v.flags())); 3.21 + PrintWriter pw = log.getWriter(Log.WriterKind.ERROR); 3.22 + pw.println("FIELD " + fieldName(v)); 3.23 + pw.println("---" + flagNames(v.flags())); 3.24 } 3.25 databuf.appendChar(pool.put(fieldName(v))); 3.26 databuf.appendChar(pool.put(typeSig(v.erasure(types)))); 3.27 @@ -934,8 +936,9 @@ 3.28 int flags = adjustFlags(m.flags()); 3.29 databuf.appendChar(flags); 3.30 if (dumpMethodModifiers) { 3.31 - log.errWriter.println("METHOD " + fieldName(m)); 3.32 - log.errWriter.println("---" + flagNames(m.flags())); 3.33 + PrintWriter pw = log.getWriter(Log.WriterKind.ERROR); 3.34 + pw.println("METHOD " + fieldName(m)); 3.35 + pw.println("---" + flagNames(m.flags())); 3.36 } 3.37 databuf.appendChar(pool.put(fieldName(m))); 3.38 databuf.appendChar(pool.put(typeSig(m.externalType(types)))); 3.39 @@ -1483,9 +1486,10 @@ 3.40 if ((flags & INTERFACE) == 0) flags |= ACC_SUPER; 3.41 if (c.isInner() && c.name.isEmpty()) flags &= ~FINAL; 3.42 if (dumpClassModifiers) { 3.43 - log.errWriter.println(); 3.44 - log.errWriter.println("CLASSFILE " + c.getQualifiedName()); 3.45 - log.errWriter.println("---" + flagNames(flags)); 3.46 + PrintWriter pw = log.getWriter(Log.WriterKind.ERROR); 3.47 + pw.println(); 3.48 + pw.println("CLASSFILE " + c.getQualifiedName()); 3.49 + pw.println("---" + flagNames(flags)); 3.50 } 3.51 databuf.appendChar(flags); 3.52
4.1 --- a/src/share/classes/com/sun/tools/javac/main/JavaCompiler.java Tue Nov 08 11:51:05 2011 -0800 4.2 +++ b/src/share/classes/com/sun/tools/javac/main/JavaCompiler.java Tue Nov 08 17:06:08 2011 -0800 4.3 @@ -1602,7 +1602,7 @@ 4.4 } 4.5 4.6 protected void printNote(String lines) { 4.7 - Log.printLines(log.noticeWriter, lines); 4.8 + log.printLines(Log.WriterKind.NOTICE, lines); 4.9 } 4.10 4.11 /** Print numbers of errors and warnings. 4.12 @@ -1615,7 +1615,7 @@ 4.13 else 4.14 key = "count." + kind + ".plural"; 4.15 log.printErrLines(key, String.valueOf(count)); 4.16 - log.errWriter.flush(); 4.17 + log.flush(Log.WriterKind.ERROR); 4.18 } 4.19 } 4.20
5.1 --- a/src/share/classes/com/sun/tools/javac/main/Main.java Tue Nov 08 11:51:05 2011 -0800 5.2 +++ b/src/share/classes/com/sun/tools/javac/main/Main.java Tue Nov 08 17:06:08 2011 -0800 5.3 @@ -67,6 +67,10 @@ 5.4 */ 5.5 PrintWriter out; 5.6 5.7 + /** The log to use for diagnostic output. 5.8 + */ 5.9 + Log log; 5.10 + 5.11 /** 5.12 * If true, certain errors will cause an exception, such as command line 5.13 * arg errors, or exceptions in user provided code. 5.14 @@ -98,6 +102,7 @@ 5.15 5.16 public void setOut(PrintWriter out) { 5.17 Main.this.out = out; 5.18 + Main.this.log.setWriters(out); 5.19 } 5.20 5.21 public void error(String key, Object... args) { 5.22 @@ -307,6 +312,8 @@ 5.23 showClass(showClass); 5.24 } 5.25 5.26 + options.notifyListeners(); 5.27 + 5.28 return filenames; 5.29 } 5.30 // where 5.31 @@ -352,6 +359,9 @@ 5.32 List<JavaFileObject> fileObjects, 5.33 Iterable<? extends Processor> processors) 5.34 { 5.35 + context.put(Log.outKey, out); 5.36 + log = Log.instance(context); 5.37 + 5.38 if (options == null) 5.39 options = Options.instance(context); // creates a new one 5.40 5.41 @@ -398,12 +408,11 @@ 5.42 5.43 boolean forceStdOut = options.isSet("stdout"); 5.44 if (forceStdOut) { 5.45 - out.flush(); 5.46 + log.flush(); 5.47 out = new PrintWriter(System.out, true); 5.48 + log.setWriters(out); 5.49 } 5.50 5.51 - context.put(Log.outKey, out); 5.52 - 5.53 // allow System property in following line as a Mustang legacy 5.54 boolean batchMode = (options.isUnset("nonBatchMode") 5.55 && System.getProperty("nonBatchMode") == null); 5.56 @@ -415,8 +424,6 @@ 5.57 comp = JavaCompiler.instance(context); 5.58 if (comp == null) return Result.SYSERR; 5.59 5.60 - Log log = Log.instance(context); 5.61 - 5.62 if (!files.isEmpty()) { 5.63 // add filenames to fileObjects 5.64 comp = JavaCompiler.instance(context); 5.65 @@ -433,10 +440,10 @@ 5.66 5.67 if (log.expectDiagKeys != null) { 5.68 if (log.expectDiagKeys.isEmpty()) { 5.69 - Log.printLines(log.noticeWriter, "all expected diagnostics found"); 5.70 + log.printLines(Log.WriterKind.NOTICE, "all expected diagnostics found"); 5.71 return Result.OK; 5.72 } else { 5.73 - Log.printLines(log.noticeWriter, "expected diagnostic keys not found: " + log.expectDiagKeys); 5.74 + log.printLines(Log.WriterKind.NOTICE, "expected diagnostic keys not found: " + log.expectDiagKeys); 5.75 return Result.ERROR; 5.76 } 5.77 }
6.1 --- a/src/share/classes/com/sun/tools/javac/util/JCDiagnostic.java Tue Nov 08 11:51:05 2011 -0800 6.2 +++ b/src/share/classes/com/sun/tools/javac/util/JCDiagnostic.java Tue Nov 08 17:06:08 2011 -0800 6.3 @@ -1,5 +1,5 @@ 6.4 /* 6.5 - * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved. 6.6 + * Copyright (c) 2003, 2011, Oracle and/or its affiliates. All rights reserved. 6.7 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 6.8 * 6.9 * This code is free software; you can redistribute it and/or modify it 6.10 @@ -70,7 +70,16 @@ 6.11 this(JavacMessages.instance(context), "compiler"); 6.12 context.put(diagnosticFactoryKey, this); 6.13 6.14 - Options options = Options.instance(context); 6.15 + final Options options = Options.instance(context); 6.16 + initOptions(options); 6.17 + options.addListener(new Runnable() { 6.18 + public void run() { 6.19 + initOptions(options); 6.20 + } 6.21 + }); 6.22 + } 6.23 + 6.24 + private void initOptions(Options options) { 6.25 if (options.isSet("onlySyntaxErrorsUnrecoverable")) 6.26 defaultErrorFlags.add(DiagnosticFlag.RECOVERABLE); 6.27 }
7.1 --- a/src/share/classes/com/sun/tools/javac/util/JavacMessages.java Tue Nov 08 11:51:05 2011 -0800 7.2 +++ b/src/share/classes/com/sun/tools/javac/util/JavacMessages.java Tue Nov 08 17:06:08 2011 -0800 7.3 @@ -82,14 +82,14 @@ 7.4 } 7.5 7.6 /** Creates a JavacMessages object. 7.7 - * @param bundleName the name to identify the resource buundle of localized messages. 7.8 + * @param bundleName the name to identify the resource bundle of localized messages. 7.9 */ 7.10 public JavacMessages(String bundleName) throws MissingResourceException { 7.11 this(bundleName, null); 7.12 } 7.13 7.14 /** Creates a JavacMessages object. 7.15 - * @param bundleName the name to identify the resource buundle of localized messages. 7.16 + * @param bundleName the name to identify the resource bundle of localized messages. 7.17 */ 7.18 public JavacMessages(String bundleName, Locale locale) throws MissingResourceException { 7.19 bundleNames = List.nil();
8.1 --- a/src/share/classes/com/sun/tools/javac/util/Log.java Tue Nov 08 11:51:05 2011 -0800 8.2 +++ b/src/share/classes/com/sun/tools/javac/util/Log.java Tue Nov 08 17:06:08 2011 -0800 8.3 @@ -60,19 +60,18 @@ 8.4 public static final Context.Key<PrintWriter> outKey = 8.5 new Context.Key<PrintWriter>(); 8.6 8.7 - //@Deprecated 8.8 - public final PrintWriter errWriter; 8.9 + public enum WriterKind { NOTICE, WARNING, ERROR }; 8.10 8.11 - //@Deprecated 8.12 - public final PrintWriter warnWriter; 8.13 + protected PrintWriter errWriter; 8.14 8.15 - //@Deprecated 8.16 - public final PrintWriter noticeWriter; 8.17 + protected PrintWriter warnWriter; 8.18 + 8.19 + protected PrintWriter noticeWriter; 8.20 8.21 /** The maximum number of errors/warnings that are reported. 8.22 */ 8.23 - public final int MaxErrors; 8.24 - public final int MaxWarnings; 8.25 + protected int MaxErrors; 8.26 + protected int MaxWarnings; 8.27 8.28 /** Switch: prompt user on each error. 8.29 */ 8.30 @@ -131,28 +130,39 @@ 8.31 this.warnWriter = warnWriter; 8.32 this.noticeWriter = noticeWriter; 8.33 8.34 - Options options = Options.instance(context); 8.35 - this.dumpOnError = options.isSet(DOE); 8.36 - this.promptOnError = options.isSet(PROMPT); 8.37 - this.emitWarnings = options.isUnset(XLINT_CUSTOM, "none"); 8.38 - this.suppressNotes = options.isSet("suppressNotes"); 8.39 - this.MaxErrors = getIntOption(options, XMAXERRS, getDefaultMaxErrors()); 8.40 - this.MaxWarnings = getIntOption(options, XMAXWARNS, getDefaultMaxWarnings()); 8.41 - 8.42 - boolean rawDiagnostics = options.isSet("rawDiagnostics"); 8.43 - messages = JavacMessages.instance(context); 8.44 - this.diagFormatter = rawDiagnostics ? new RawDiagnosticFormatter(options) : 8.45 - new BasicDiagnosticFormatter(options, messages); 8.46 @SuppressWarnings("unchecked") // FIXME 8.47 DiagnosticListener<? super JavaFileObject> dl = 8.48 context.get(DiagnosticListener.class); 8.49 this.diagListener = dl; 8.50 8.51 - String ek = options.get("expectKeys"); 8.52 - if (ek != null) 8.53 - expectDiagKeys = new HashSet<String>(Arrays.asList(ek.split(", *"))); 8.54 + messages = JavacMessages.instance(context); 8.55 + 8.56 + final Options options = Options.instance(context); 8.57 + initOptions(options); 8.58 + options.addListener(new Runnable() { 8.59 + public void run() { 8.60 + initOptions(options); 8.61 + } 8.62 + }); 8.63 } 8.64 // where 8.65 + private void initOptions(Options options) { 8.66 + this.dumpOnError = options.isSet(DOE); 8.67 + this.promptOnError = options.isSet(PROMPT); 8.68 + this.emitWarnings = options.isUnset(XLINT_CUSTOM, "none"); 8.69 + this.suppressNotes = options.isSet("suppressNotes"); 8.70 + this.MaxErrors = getIntOption(options, XMAXERRS, getDefaultMaxErrors()); 8.71 + this.MaxWarnings = getIntOption(options, XMAXWARNS, getDefaultMaxWarnings()); 8.72 + 8.73 + boolean rawDiagnostics = options.isSet("rawDiagnostics"); 8.74 + this.diagFormatter = rawDiagnostics ? new RawDiagnosticFormatter(options) : 8.75 + new BasicDiagnosticFormatter(options, messages); 8.76 + 8.77 + String ek = options.get("expectKeys"); 8.78 + if (ek != null) 8.79 + expectDiagKeys = new HashSet<String>(Arrays.asList(ek.split(", *"))); 8.80 + } 8.81 + 8.82 private int getIntOption(Options options, OptionName optionName, int defaultValue) { 8.83 String s = options.get(optionName); 8.84 try { 8.85 @@ -180,7 +190,7 @@ 8.86 8.87 /** The default writer for diagnostics 8.88 */ 8.89 - static final PrintWriter defaultWriter(Context context) { 8.90 + static PrintWriter defaultWriter(Context context) { 8.91 PrintWriter result = context.get(outKey); 8.92 if (result == null) 8.93 context.put(outKey, result = new PrintWriter(System.err)); 8.94 @@ -248,6 +258,30 @@ 8.95 this.diagFormatter = diagFormatter; 8.96 } 8.97 8.98 + public PrintWriter getWriter(WriterKind kind) { 8.99 + switch (kind) { 8.100 + case NOTICE: return noticeWriter; 8.101 + case WARNING: return warnWriter; 8.102 + case ERROR: return errWriter; 8.103 + default: throw new IllegalArgumentException(); 8.104 + } 8.105 + } 8.106 + 8.107 + public void setWriter(WriterKind kind, PrintWriter pw) { 8.108 + pw.getClass(); 8.109 + switch (kind) { 8.110 + case NOTICE: noticeWriter = pw; break; 8.111 + case WARNING: warnWriter = pw; break; 8.112 + case ERROR: errWriter = pw; break; 8.113 + default: throw new IllegalArgumentException(); 8.114 + } 8.115 + } 8.116 + 8.117 + public void setWriters(PrintWriter pw) { 8.118 + pw.getClass(); 8.119 + noticeWriter = warnWriter = errWriter = pw; 8.120 + } 8.121 + 8.122 /** Flush the logs 8.123 */ 8.124 public void flush() { 8.125 @@ -256,6 +290,10 @@ 8.126 noticeWriter.flush(); 8.127 } 8.128 8.129 + public void flush(WriterKind kind) { 8.130 + getWriter(kind).flush(); 8.131 + } 8.132 + 8.133 /** Returns true if an error needs to be reported for a given 8.134 * source name and pos. 8.135 */ 8.136 @@ -313,6 +351,13 @@ 8.137 /** Print the text of a message, translating newlines appropriately 8.138 * for the platform. 8.139 */ 8.140 + public void printLines(WriterKind kind, String msg) { 8.141 + printLines(getWriter(kind), msg); 8.142 + } 8.143 + 8.144 + /** Print the text of a message, translating newlines appropriately 8.145 + * for the platform. 8.146 + */ 8.147 public static void printLines(PrintWriter writer, String msg) { 8.148 int nl; 8.149 while ((nl = msg.indexOf('\n')) != -1) {
9.1 --- a/src/share/classes/com/sun/tools/javac/util/Options.java Tue Nov 08 11:51:05 2011 -0800 9.2 +++ b/src/share/classes/com/sun/tools/javac/util/Options.java Tue Nov 08 17:06:08 2011 -0800 9.3 @@ -157,6 +157,19 @@ 9.4 return values.size(); 9.5 } 9.6 9.7 + // light-weight notification mechanism 9.8 + 9.9 + private List<Runnable> listeners = List.nil(); 9.10 + 9.11 + public void addListener(Runnable listener) { 9.12 + listeners = listeners.prepend(listener); 9.13 + } 9.14 + 9.15 + public void notifyListeners() { 9.16 + for (Runnable r: listeners) 9.17 + r.run(); 9.18 + } 9.19 + 9.20 /** Check for a lint suboption. */ 9.21 public boolean lint(String s) { 9.22 // return true if either the specific option is enabled, or
10.1 --- a/src/share/classes/com/sun/tools/javadoc/Start.java Tue Nov 08 11:51:05 2011 -0800 10.2 +++ b/src/share/classes/com/sun/tools/javadoc/Start.java Tue Nov 08 17:06:08 2011 -0800 10.3 @@ -31,6 +31,7 @@ 10.4 import com.sun.tools.javac.util.Context; 10.5 import com.sun.tools.javac.util.List; 10.6 import com.sun.tools.javac.util.ListBuffer; 10.7 +import com.sun.tools.javac.util.Log; 10.8 import com.sun.tools.javac.util.Options; 10.9 10.10 import java.io.IOException; 10.11 @@ -76,9 +77,6 @@ 10.12 10.13 private DocletInvoker docletInvoker; 10.14 10.15 - private static final int F_VERBOSE = 1 << 0; 10.16 - private static final int F_WARNINGS = 1 << 2; 10.17 - 10.18 /* Treat warnings as errors. */ 10.19 private boolean rejectWarnings = false; 10.20 10.21 @@ -171,11 +169,11 @@ 10.22 messager.error(null, "main.out.of.memory"); 10.23 failed = true; 10.24 } catch (Error ee) { 10.25 - ee.printStackTrace(); 10.26 + ee.printStackTrace(System.err); 10.27 messager.error(null, "main.fatal.error"); 10.28 failed = true; 10.29 } catch (Exception ee) { 10.30 - ee.printStackTrace(); 10.31 + ee.printStackTrace(System.err); 10.32 messager.error(null, "main.fatal.exception"); 10.33 failed = true; 10.34 } finally { 10.35 @@ -211,7 +209,7 @@ 10.36 messager.error(null, "main.cant.read", e.getMessage()); 10.37 exit(); 10.38 } catch (IOException e) { 10.39 - e.printStackTrace(); 10.40 + e.printStackTrace(System.err); 10.41 exit(); 10.42 } 10.43 10.44 @@ -225,7 +223,9 @@ 10.45 // options that may be set up below. 10.46 Messager.preRegister(context, 10.47 messager.programName, 10.48 - messager.errWriter, messager.warnWriter, messager.noticeWriter); 10.49 + messager.getWriter(Log.WriterKind.ERROR), 10.50 + messager.getWriter(Log.WriterKind.WARNING), 10.51 + messager.getWriter(Log.WriterKind.NOTICE)); 10.52 10.53 Options compOpts = Options.instance(context); 10.54 boolean docClasses = false;