提交 ecb72958 编写于 作者: J jjg

7108668: allow Log to be initialized and used earlier

Reviewed-by: mcimadamore
上级 b0ad1aa2
/*
* 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 @@ public class Apt extends ListBuffer<Env<AttrContext>> {
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>();
......
......@@ -250,6 +250,8 @@ public final class JavacTool implements JavaCompiler {
throw new IllegalArgumentException(flag);
}
}
optionTable.notifyListeners();
}
public int run(InputStream in, OutputStream out, OutputStream err, String... arguments) {
......
......@@ -892,8 +892,9 @@ public class ClassWriter extends ClassFile {
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 @@ public class ClassWriter extends ClassFile {
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 @@ public class ClassWriter extends ClassFile {
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 @@ public class ClassWriter extends ClassFile {
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);
......
......@@ -1602,7 +1602,7 @@ public class JavaCompiler implements ClassReader.SourceCompleter {
}
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 @@ public class JavaCompiler implements ClassReader.SourceCompleter {
else
key = "count." + kind + ".plural";
log.printErrLines(key, String.valueOf(count));
log.errWriter.flush();
log.flush(Log.WriterKind.ERROR);
}
}
......
......@@ -67,6 +67,10 @@ public class Main {
*/
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 class Main {
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 @@ public class Main {
showClass(showClass);
}
options.notifyListeners();
return filenames;
}
// where
......@@ -352,6 +359,9 @@ public class Main {
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 @@ public class Main {
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 @@ public class Main {
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 @@ public class Main {
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;
}
}
......
/*
* 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 @@ public class JCDiagnostic implements Diagnostic<JavaFileObject> {
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);
}
......
......@@ -82,14 +82,14 @@ public class JavacMessages implements Messages {
}
/** 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();
......
......@@ -60,19 +60,18 @@ public class Log extends AbstractLog {
public static final Context.Key<PrintWriter> outKey =
new Context.Key<PrintWriter>();
//@Deprecated
public final PrintWriter errWriter;
public enum WriterKind { NOTICE, WARNING, ERROR };
//@Deprecated
public final PrintWriter warnWriter;
protected PrintWriter errWriter;
//@Deprecated
public final PrintWriter noticeWriter;
protected PrintWriter warnWriter;
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 @@ public class Log extends AbstractLog {
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 @@ public class Log extends AbstractLog {
/** 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 @@ public class Log extends AbstractLog {
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 @@ public class Log extends AbstractLog {
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.
*/
......@@ -310,6 +348,13 @@ public class Log extends AbstractLog {
writer.flush();
}
/** 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.
*/
......
......@@ -157,6 +157,19 @@ public class Options {
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
......
......@@ -31,6 +31,7 @@ import com.sun.tools.javac.main.CommandLine;
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 @@ class Start {
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 @@ class Start {
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 @@ class Start {
messager.error(null, "main.cant.read", e.getMessage());
exit();
} catch (IOException e) {
e.printStackTrace();
e.printStackTrace(System.err);
exit();
}
......@@ -225,7 +223,9 @@ class Start {
// 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;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册