1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/share/classes/com/sun/tools/javac/util/Log.java Wed Apr 27 01:34:52 2016 +0800 1.3 @@ -0,0 +1,738 @@ 1.4 +/* 1.5 + * Copyright (c) 1999, 2013, Oracle and/or its affiliates. All rights reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. Oracle designates this 1.11 + * particular file as subject to the "Classpath" exception as provided 1.12 + * by Oracle in the LICENSE file that accompanied this code. 1.13 + * 1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.17 + * version 2 for more details (a copy is included in the LICENSE file that 1.18 + * accompanied this code). 1.19 + * 1.20 + * You should have received a copy of the GNU General Public License version 1.21 + * 2 along with this work; if not, write to the Free Software Foundation, 1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.23 + * 1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.25 + * or visit www.oracle.com if you need additional information or have any 1.26 + * questions. 1.27 + */ 1.28 + 1.29 +package com.sun.tools.javac.util; 1.30 + 1.31 +import java.io.*; 1.32 +import java.util.Arrays; 1.33 +import java.util.EnumSet; 1.34 +import java.util.HashSet; 1.35 +import java.util.Queue; 1.36 +import java.util.Set; 1.37 +import javax.tools.DiagnosticListener; 1.38 +import javax.tools.JavaFileObject; 1.39 + 1.40 +import com.sun.tools.javac.api.DiagnosticFormatter; 1.41 +import com.sun.tools.javac.main.Main; 1.42 +import com.sun.tools.javac.main.Option; 1.43 +import com.sun.tools.javac.tree.EndPosTable; 1.44 +import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition; 1.45 +import com.sun.tools.javac.util.JCDiagnostic.DiagnosticType; 1.46 + 1.47 +import static com.sun.tools.javac.main.Option.*; 1.48 + 1.49 +/** A class for error logs. Reports errors and warnings, and 1.50 + * keeps track of error numbers and positions. 1.51 + * 1.52 + * <p><b>This is NOT part of any supported API. 1.53 + * If you write code that depends on this, you do so at your own risk. 1.54 + * This code and its internal interfaces are subject to change or 1.55 + * deletion without notice.</b> 1.56 + */ 1.57 +public class Log extends AbstractLog { 1.58 + /** The context key for the log. */ 1.59 + public static final Context.Key<Log> logKey 1.60 + = new Context.Key<Log>(); 1.61 + 1.62 + /** The context key for the output PrintWriter. */ 1.63 + public static final Context.Key<PrintWriter> outKey = 1.64 + new Context.Key<PrintWriter>(); 1.65 + 1.66 + /* TODO: Should unify this with prefix handling in JCDiagnostic.Factory. */ 1.67 + public enum PrefixKind { 1.68 + JAVAC("javac."), 1.69 + COMPILER_MISC("compiler.misc."); 1.70 + PrefixKind(String v) { 1.71 + value = v; 1.72 + } 1.73 + public String key(String k) { 1.74 + return value + k; 1.75 + } 1.76 + final String value; 1.77 + } 1.78 + 1.79 + /** 1.80 + * DiagnosticHandler's provide the initial handling for diagnostics. 1.81 + * When a diagnostic handler is created and has been initialized, it 1.82 + * should install itself as the current diagnostic handler. When a 1.83 + * client has finished using a handler, the client should call 1.84 + * {@code log.removeDiagnosticHandler();} 1.85 + * 1.86 + * Note that javax.tools.DiagnosticListener (if set) is called later in the 1.87 + * diagnostic pipeline. 1.88 + */ 1.89 + public static abstract class DiagnosticHandler { 1.90 + /** 1.91 + * The previously installed diagnostic handler. 1.92 + */ 1.93 + protected DiagnosticHandler prev; 1.94 + 1.95 + /** 1.96 + * Install this diagnostic handler as the current one, 1.97 + * recording the previous one. 1.98 + */ 1.99 + protected void install(Log log) { 1.100 + prev = log.diagnosticHandler; 1.101 + log.diagnosticHandler = this; 1.102 + } 1.103 + 1.104 + /** 1.105 + * Handle a diagnostic. 1.106 + */ 1.107 + public abstract void report(JCDiagnostic diag); 1.108 + } 1.109 + 1.110 + /** 1.111 + * A DiagnosticHandler that discards all diagnostics. 1.112 + */ 1.113 + public static class DiscardDiagnosticHandler extends DiagnosticHandler { 1.114 + public DiscardDiagnosticHandler(Log log) { 1.115 + install(log); 1.116 + } 1.117 + 1.118 + public void report(JCDiagnostic diag) { } 1.119 + } 1.120 + 1.121 + /** 1.122 + * A DiagnosticHandler that can defer some or all diagnostics, 1.123 + * by buffering them for later examination and/or reporting. 1.124 + * If a diagnostic is not deferred, or is subsequently reported 1.125 + * with reportAllDiagnostics(), it will be reported to the previously 1.126 + * active diagnostic handler. 1.127 + */ 1.128 + public static class DeferredDiagnosticHandler extends DiagnosticHandler { 1.129 + private Queue<JCDiagnostic> deferred = new ListBuffer<>(); 1.130 + private final Filter<JCDiagnostic> filter; 1.131 + 1.132 + public DeferredDiagnosticHandler(Log log) { 1.133 + this(log, null); 1.134 + } 1.135 + 1.136 + public DeferredDiagnosticHandler(Log log, Filter<JCDiagnostic> filter) { 1.137 + this.filter = filter; 1.138 + install(log); 1.139 + } 1.140 + 1.141 + public void report(JCDiagnostic diag) { 1.142 + if (!diag.isFlagSet(JCDiagnostic.DiagnosticFlag.NON_DEFERRABLE) && 1.143 + (filter == null || filter.accepts(diag))) { 1.144 + deferred.add(diag); 1.145 + } else { 1.146 + prev.report(diag); 1.147 + } 1.148 + } 1.149 + 1.150 + public Queue<JCDiagnostic> getDiagnostics() { 1.151 + return deferred; 1.152 + } 1.153 + 1.154 + /** Report all deferred diagnostics. */ 1.155 + public void reportDeferredDiagnostics() { 1.156 + reportDeferredDiagnostics(EnumSet.allOf(JCDiagnostic.Kind.class)); 1.157 + } 1.158 + 1.159 + /** Report selected deferred diagnostics. */ 1.160 + public void reportDeferredDiagnostics(Set<JCDiagnostic.Kind> kinds) { 1.161 + JCDiagnostic d; 1.162 + while ((d = deferred.poll()) != null) { 1.163 + if (kinds.contains(d.getKind())) 1.164 + prev.report(d); 1.165 + } 1.166 + deferred = null; // prevent accidental ongoing use 1.167 + } 1.168 + } 1.169 + 1.170 + public enum WriterKind { NOTICE, WARNING, ERROR }; 1.171 + 1.172 + protected PrintWriter errWriter; 1.173 + 1.174 + protected PrintWriter warnWriter; 1.175 + 1.176 + protected PrintWriter noticeWriter; 1.177 + 1.178 + /** The maximum number of errors/warnings that are reported. 1.179 + */ 1.180 + protected int MaxErrors; 1.181 + protected int MaxWarnings; 1.182 + 1.183 + /** Switch: prompt user on each error. 1.184 + */ 1.185 + public boolean promptOnError; 1.186 + 1.187 + /** Switch: emit warning messages. 1.188 + */ 1.189 + public boolean emitWarnings; 1.190 + 1.191 + /** Switch: suppress note messages. 1.192 + */ 1.193 + public boolean suppressNotes; 1.194 + 1.195 + /** Print stack trace on errors? 1.196 + */ 1.197 + public boolean dumpOnError; 1.198 + 1.199 + /** Print multiple errors for same source locations. 1.200 + */ 1.201 + public boolean multipleErrors; 1.202 + 1.203 + /** 1.204 + * Diagnostic listener, if provided through programmatic 1.205 + * interface to javac (JSR 199). 1.206 + */ 1.207 + protected DiagnosticListener<? super JavaFileObject> diagListener; 1.208 + 1.209 + /** 1.210 + * Formatter for diagnostics. 1.211 + */ 1.212 + private DiagnosticFormatter<JCDiagnostic> diagFormatter; 1.213 + 1.214 + /** 1.215 + * Keys for expected diagnostics. 1.216 + */ 1.217 + public Set<String> expectDiagKeys; 1.218 + 1.219 + /** 1.220 + * Set to true if a compressed diagnostic is reported 1.221 + */ 1.222 + public boolean compressedOutput; 1.223 + 1.224 + /** 1.225 + * JavacMessages object used for localization. 1.226 + */ 1.227 + private JavacMessages messages; 1.228 + 1.229 + /** 1.230 + * Handler for initial dispatch of diagnostics. 1.231 + */ 1.232 + private DiagnosticHandler diagnosticHandler; 1.233 + 1.234 + /** Construct a log with given I/O redirections. 1.235 + */ 1.236 + protected Log(Context context, PrintWriter errWriter, PrintWriter warnWriter, PrintWriter noticeWriter) { 1.237 + super(JCDiagnostic.Factory.instance(context)); 1.238 + context.put(logKey, this); 1.239 + this.errWriter = errWriter; 1.240 + this.warnWriter = warnWriter; 1.241 + this.noticeWriter = noticeWriter; 1.242 + 1.243 + @SuppressWarnings("unchecked") // FIXME 1.244 + DiagnosticListener<? super JavaFileObject> dl = 1.245 + context.get(DiagnosticListener.class); 1.246 + this.diagListener = dl; 1.247 + 1.248 + diagnosticHandler = new DefaultDiagnosticHandler(); 1.249 + 1.250 + messages = JavacMessages.instance(context); 1.251 + messages.add(Main.javacBundleName); 1.252 + 1.253 + final Options options = Options.instance(context); 1.254 + initOptions(options); 1.255 + options.addListener(new Runnable() { 1.256 + public void run() { 1.257 + initOptions(options); 1.258 + } 1.259 + }); 1.260 + } 1.261 + // where 1.262 + private void initOptions(Options options) { 1.263 + this.dumpOnError = options.isSet(DOE); 1.264 + this.promptOnError = options.isSet(PROMPT); 1.265 + this.emitWarnings = options.isUnset(XLINT_CUSTOM, "none"); 1.266 + this.suppressNotes = options.isSet("suppressNotes"); 1.267 + this.MaxErrors = getIntOption(options, XMAXERRS, getDefaultMaxErrors()); 1.268 + this.MaxWarnings = getIntOption(options, XMAXWARNS, getDefaultMaxWarnings()); 1.269 + 1.270 + boolean rawDiagnostics = options.isSet("rawDiagnostics"); 1.271 + this.diagFormatter = rawDiagnostics ? new RawDiagnosticFormatter(options) : 1.272 + new BasicDiagnosticFormatter(options, messages); 1.273 + 1.274 + String ek = options.get("expectKeys"); 1.275 + if (ek != null) 1.276 + expectDiagKeys = new HashSet<String>(Arrays.asList(ek.split(", *"))); 1.277 + } 1.278 + 1.279 + private int getIntOption(Options options, Option option, int defaultValue) { 1.280 + String s = options.get(option); 1.281 + try { 1.282 + if (s != null) { 1.283 + int n = Integer.parseInt(s); 1.284 + return (n <= 0 ? Integer.MAX_VALUE : n); 1.285 + } 1.286 + } catch (NumberFormatException e) { 1.287 + // silently ignore ill-formed numbers 1.288 + } 1.289 + return defaultValue; 1.290 + } 1.291 + 1.292 + /** Default value for -Xmaxerrs. 1.293 + */ 1.294 + protected int getDefaultMaxErrors() { 1.295 + return 100; 1.296 + } 1.297 + 1.298 + /** Default value for -Xmaxwarns. 1.299 + */ 1.300 + protected int getDefaultMaxWarnings() { 1.301 + return 100; 1.302 + } 1.303 + 1.304 + /** The default writer for diagnostics 1.305 + */ 1.306 + static PrintWriter defaultWriter(Context context) { 1.307 + PrintWriter result = context.get(outKey); 1.308 + if (result == null) 1.309 + context.put(outKey, result = new PrintWriter(System.err)); 1.310 + return result; 1.311 + } 1.312 + 1.313 + /** Construct a log with default settings. 1.314 + */ 1.315 + protected Log(Context context) { 1.316 + this(context, defaultWriter(context)); 1.317 + } 1.318 + 1.319 + /** Construct a log with all output redirected. 1.320 + */ 1.321 + protected Log(Context context, PrintWriter defaultWriter) { 1.322 + this(context, defaultWriter, defaultWriter, defaultWriter); 1.323 + } 1.324 + 1.325 + /** Get the Log instance for this context. */ 1.326 + public static Log instance(Context context) { 1.327 + Log instance = context.get(logKey); 1.328 + if (instance == null) 1.329 + instance = new Log(context); 1.330 + return instance; 1.331 + } 1.332 + 1.333 + /** The number of errors encountered so far. 1.334 + */ 1.335 + public int nerrors = 0; 1.336 + 1.337 + /** The number of warnings encountered so far. 1.338 + */ 1.339 + public int nwarnings = 0; 1.340 + 1.341 + /** A set of all errors generated so far. This is used to avoid printing an 1.342 + * error message more than once. For each error, a pair consisting of the 1.343 + * source file name and source code position of the error is added to the set. 1.344 + */ 1.345 + private Set<Pair<JavaFileObject, Integer>> recorded = new HashSet<Pair<JavaFileObject,Integer>>(); 1.346 + 1.347 + public boolean hasDiagnosticListener() { 1.348 + return diagListener != null; 1.349 + } 1.350 + 1.351 + public void setEndPosTable(JavaFileObject name, EndPosTable endPosTable) { 1.352 + name.getClass(); // null check 1.353 + getSource(name).setEndPosTable(endPosTable); 1.354 + } 1.355 + 1.356 + /** Return current sourcefile. 1.357 + */ 1.358 + public JavaFileObject currentSourceFile() { 1.359 + return source == null ? null : source.getFile(); 1.360 + } 1.361 + 1.362 + /** Get the current diagnostic formatter. 1.363 + */ 1.364 + public DiagnosticFormatter<JCDiagnostic> getDiagnosticFormatter() { 1.365 + return diagFormatter; 1.366 + } 1.367 + 1.368 + /** Set the current diagnostic formatter. 1.369 + */ 1.370 + public void setDiagnosticFormatter(DiagnosticFormatter<JCDiagnostic> diagFormatter) { 1.371 + this.diagFormatter = diagFormatter; 1.372 + } 1.373 + 1.374 + public PrintWriter getWriter(WriterKind kind) { 1.375 + switch (kind) { 1.376 + case NOTICE: return noticeWriter; 1.377 + case WARNING: return warnWriter; 1.378 + case ERROR: return errWriter; 1.379 + default: throw new IllegalArgumentException(); 1.380 + } 1.381 + } 1.382 + 1.383 + public void setWriter(WriterKind kind, PrintWriter pw) { 1.384 + pw.getClass(); 1.385 + switch (kind) { 1.386 + case NOTICE: noticeWriter = pw; break; 1.387 + case WARNING: warnWriter = pw; break; 1.388 + case ERROR: errWriter = pw; break; 1.389 + default: throw new IllegalArgumentException(); 1.390 + } 1.391 + } 1.392 + 1.393 + public void setWriters(PrintWriter pw) { 1.394 + pw.getClass(); 1.395 + noticeWriter = warnWriter = errWriter = pw; 1.396 + } 1.397 + 1.398 + /** 1.399 + * Propagate the previous log's information. 1.400 + */ 1.401 + public void initRound(Log other) { 1.402 + this.noticeWriter = other.noticeWriter; 1.403 + this.warnWriter = other.warnWriter; 1.404 + this.errWriter = other.errWriter; 1.405 + this.sourceMap = other.sourceMap; 1.406 + this.recorded = other.recorded; 1.407 + this.nerrors = other.nerrors; 1.408 + this.nwarnings = other.nwarnings; 1.409 + } 1.410 + 1.411 + /** 1.412 + * Replace the specified diagnostic handler with the 1.413 + * handler that was current at the time this handler was created. 1.414 + * The given handler must be the currently installed handler; 1.415 + * it must be specified explicitly for clarity and consistency checking. 1.416 + */ 1.417 + public void popDiagnosticHandler(DiagnosticHandler h) { 1.418 + Assert.check(diagnosticHandler == h); 1.419 + diagnosticHandler = h.prev; 1.420 + } 1.421 + 1.422 + /** Flush the logs 1.423 + */ 1.424 + public void flush() { 1.425 + errWriter.flush(); 1.426 + warnWriter.flush(); 1.427 + noticeWriter.flush(); 1.428 + } 1.429 + 1.430 + public void flush(WriterKind kind) { 1.431 + getWriter(kind).flush(); 1.432 + } 1.433 + 1.434 + /** Returns true if an error needs to be reported for a given 1.435 + * source name and pos. 1.436 + */ 1.437 + protected boolean shouldReport(JavaFileObject file, int pos) { 1.438 + if (multipleErrors || file == null) 1.439 + return true; 1.440 + 1.441 + Pair<JavaFileObject,Integer> coords = new Pair<JavaFileObject,Integer>(file, pos); 1.442 + boolean shouldReport = !recorded.contains(coords); 1.443 + if (shouldReport) 1.444 + recorded.add(coords); 1.445 + return shouldReport; 1.446 + } 1.447 + 1.448 + /** Prompt user after an error. 1.449 + */ 1.450 + public void prompt() { 1.451 + if (promptOnError) { 1.452 + System.err.println(localize("resume.abort")); 1.453 + try { 1.454 + while (true) { 1.455 + switch (System.in.read()) { 1.456 + case 'a': case 'A': 1.457 + System.exit(-1); 1.458 + return; 1.459 + case 'r': case 'R': 1.460 + return; 1.461 + case 'x': case 'X': 1.462 + throw new AssertionError("user abort"); 1.463 + default: 1.464 + } 1.465 + } 1.466 + } catch (IOException e) {} 1.467 + } 1.468 + } 1.469 + 1.470 + /** Print the faulty source code line and point to the error. 1.471 + * @param pos Buffer index of the error position, must be on current line 1.472 + */ 1.473 + private void printErrLine(int pos, PrintWriter writer) { 1.474 + String line = (source == null ? null : source.getLine(pos)); 1.475 + if (line == null) 1.476 + return; 1.477 + int col = source.getColumnNumber(pos, false); 1.478 + 1.479 + printRawLines(writer, line); 1.480 + for (int i = 0; i < col - 1; i++) { 1.481 + writer.print((line.charAt(i) == '\t') ? "\t" : " "); 1.482 + } 1.483 + writer.println("^"); 1.484 + writer.flush(); 1.485 + } 1.486 + 1.487 + public void printNewline() { 1.488 + noticeWriter.println(); 1.489 + } 1.490 + 1.491 + public void printNewline(WriterKind wk) { 1.492 + getWriter(wk).println(); 1.493 + } 1.494 + 1.495 + public void printLines(String key, Object... args) { 1.496 + printRawLines(noticeWriter, localize(key, args)); 1.497 + } 1.498 + 1.499 + public void printLines(PrefixKind pk, String key, Object... args) { 1.500 + printRawLines(noticeWriter, localize(pk, key, args)); 1.501 + } 1.502 + 1.503 + public void printLines(WriterKind wk, String key, Object... args) { 1.504 + printRawLines(getWriter(wk), localize(key, args)); 1.505 + } 1.506 + 1.507 + public void printLines(WriterKind wk, PrefixKind pk, String key, Object... args) { 1.508 + printRawLines(getWriter(wk), localize(pk, key, args)); 1.509 + } 1.510 + 1.511 + /** Print the text of a message, translating newlines appropriately 1.512 + * for the platform. 1.513 + */ 1.514 + public void printRawLines(String msg) { 1.515 + printRawLines(noticeWriter, msg); 1.516 + } 1.517 + 1.518 + /** Print the text of a message, translating newlines appropriately 1.519 + * for the platform. 1.520 + */ 1.521 + public void printRawLines(WriterKind kind, String msg) { 1.522 + printRawLines(getWriter(kind), msg); 1.523 + } 1.524 + 1.525 + /** Print the text of a message, translating newlines appropriately 1.526 + * for the platform. 1.527 + */ 1.528 + public static void printRawLines(PrintWriter writer, String msg) { 1.529 + int nl; 1.530 + while ((nl = msg.indexOf('\n')) != -1) { 1.531 + writer.println(msg.substring(0, nl)); 1.532 + msg = msg.substring(nl+1); 1.533 + } 1.534 + if (msg.length() != 0) writer.println(msg); 1.535 + } 1.536 + 1.537 + /** 1.538 + * Print the localized text of a "verbose" message to the 1.539 + * noticeWriter stream. 1.540 + */ 1.541 + public void printVerbose(String key, Object... args) { 1.542 + printRawLines(noticeWriter, localize("verbose." + key, args)); 1.543 + } 1.544 + 1.545 + protected void directError(String key, Object... args) { 1.546 + printRawLines(errWriter, localize(key, args)); 1.547 + errWriter.flush(); 1.548 + } 1.549 + 1.550 + /** Report a warning that cannot be suppressed. 1.551 + * @param pos The source position at which to report the warning. 1.552 + * @param key The key for the localized warning message. 1.553 + * @param args Fields of the warning message. 1.554 + */ 1.555 + public void strictWarning(DiagnosticPosition pos, String key, Object ... args) { 1.556 + writeDiagnostic(diags.warning(source, pos, key, args)); 1.557 + nwarnings++; 1.558 + } 1.559 + 1.560 + /** 1.561 + * Primary method to report a diagnostic. 1.562 + * @param diagnostic 1.563 + */ 1.564 + public void report(JCDiagnostic diagnostic) { 1.565 + diagnosticHandler.report(diagnostic); 1.566 + } 1.567 + 1.568 + /** 1.569 + * Common diagnostic handling. 1.570 + * The diagnostic is counted, and depending on the options and how many diagnostics have been 1.571 + * reported so far, the diagnostic may be handed off to writeDiagnostic. 1.572 + */ 1.573 + private class DefaultDiagnosticHandler extends DiagnosticHandler { 1.574 + public void report(JCDiagnostic diagnostic) { 1.575 + if (expectDiagKeys != null) 1.576 + expectDiagKeys.remove(diagnostic.getCode()); 1.577 + 1.578 + switch (diagnostic.getType()) { 1.579 + case FRAGMENT: 1.580 + throw new IllegalArgumentException(); 1.581 + 1.582 + case NOTE: 1.583 + // Print out notes only when we are permitted to report warnings 1.584 + // Notes are only generated at the end of a compilation, so should be small 1.585 + // in number. 1.586 + if ((emitWarnings || diagnostic.isMandatory()) && !suppressNotes) { 1.587 + writeDiagnostic(diagnostic); 1.588 + } 1.589 + break; 1.590 + 1.591 + case WARNING: 1.592 + if (emitWarnings || diagnostic.isMandatory()) { 1.593 + if (nwarnings < MaxWarnings) { 1.594 + writeDiagnostic(diagnostic); 1.595 + nwarnings++; 1.596 + } 1.597 + } 1.598 + break; 1.599 + 1.600 + case ERROR: 1.601 + if (nerrors < MaxErrors 1.602 + && shouldReport(diagnostic.getSource(), diagnostic.getIntPosition())) { 1.603 + writeDiagnostic(diagnostic); 1.604 + nerrors++; 1.605 + } 1.606 + break; 1.607 + } 1.608 + if (diagnostic.isFlagSet(JCDiagnostic.DiagnosticFlag.COMPRESSED)) { 1.609 + compressedOutput = true; 1.610 + } 1.611 + } 1.612 + } 1.613 + 1.614 + /** 1.615 + * Write out a diagnostic. 1.616 + */ 1.617 + protected void writeDiagnostic(JCDiagnostic diag) { 1.618 + if (diagListener != null) { 1.619 + diagListener.report(diag); 1.620 + return; 1.621 + } 1.622 + 1.623 + PrintWriter writer = getWriterForDiagnosticType(diag.getType()); 1.624 + 1.625 + printRawLines(writer, diagFormatter.format(diag, messages.getCurrentLocale())); 1.626 + 1.627 + if (promptOnError) { 1.628 + switch (diag.getType()) { 1.629 + case ERROR: 1.630 + case WARNING: 1.631 + prompt(); 1.632 + } 1.633 + } 1.634 + 1.635 + if (dumpOnError) 1.636 + new RuntimeException().printStackTrace(writer); 1.637 + 1.638 + writer.flush(); 1.639 + } 1.640 + 1.641 + @Deprecated 1.642 + protected PrintWriter getWriterForDiagnosticType(DiagnosticType dt) { 1.643 + switch (dt) { 1.644 + case FRAGMENT: 1.645 + throw new IllegalArgumentException(); 1.646 + 1.647 + case NOTE: 1.648 + return noticeWriter; 1.649 + 1.650 + case WARNING: 1.651 + return warnWriter; 1.652 + 1.653 + case ERROR: 1.654 + return errWriter; 1.655 + 1.656 + default: 1.657 + throw new Error(); 1.658 + } 1.659 + } 1.660 + 1.661 + /** Find a localized string in the resource bundle. 1.662 + * Because this method is static, it ignores the locale. 1.663 + * Use localize(key, args) when possible. 1.664 + * @param key The key for the localized string. 1.665 + * @param args Fields to substitute into the string. 1.666 + */ 1.667 + public static String getLocalizedString(String key, Object ... args) { 1.668 + return JavacMessages.getDefaultLocalizedString(PrefixKind.COMPILER_MISC.key(key), args); 1.669 + } 1.670 + 1.671 + /** Find a localized string in the resource bundle. 1.672 + * @param key The key for the localized string. 1.673 + * @param args Fields to substitute into the string. 1.674 + */ 1.675 + public String localize(String key, Object... args) { 1.676 + return localize(PrefixKind.COMPILER_MISC, key, args); 1.677 + } 1.678 + 1.679 + /** Find a localized string in the resource bundle. 1.680 + * @param key The key for the localized string. 1.681 + * @param args Fields to substitute into the string. 1.682 + */ 1.683 + public String localize(PrefixKind pk, String key, Object... args) { 1.684 + if (useRawMessages) 1.685 + return pk.key(key); 1.686 + else 1.687 + return messages.getLocalizedString(pk.key(key), args); 1.688 + } 1.689 + // where 1.690 + // backdoor hook for testing, should transition to use -XDrawDiagnostics 1.691 + private static boolean useRawMessages = false; 1.692 + 1.693 +/*************************************************************************** 1.694 + * raw error messages without internationalization; used for experimentation 1.695 + * and quick prototyping 1.696 + ***************************************************************************/ 1.697 + 1.698 + /** print an error or warning message: 1.699 + */ 1.700 + private void printRawError(int pos, String msg) { 1.701 + if (source == null || pos == Position.NOPOS) { 1.702 + printRawLines(errWriter, "error: " + msg); 1.703 + } else { 1.704 + int line = source.getLineNumber(pos); 1.705 + JavaFileObject file = source.getFile(); 1.706 + if (file != null) 1.707 + printRawLines(errWriter, 1.708 + file.getName() + ":" + 1.709 + line + ": " + msg); 1.710 + printErrLine(pos, errWriter); 1.711 + } 1.712 + errWriter.flush(); 1.713 + } 1.714 + 1.715 + /** report an error: 1.716 + */ 1.717 + public void rawError(int pos, String msg) { 1.718 + if (nerrors < MaxErrors && shouldReport(currentSourceFile(), pos)) { 1.719 + printRawError(pos, msg); 1.720 + prompt(); 1.721 + nerrors++; 1.722 + } 1.723 + errWriter.flush(); 1.724 + } 1.725 + 1.726 + /** report a warning: 1.727 + */ 1.728 + public void rawWarning(int pos, String msg) { 1.729 + if (nwarnings < MaxWarnings && emitWarnings) { 1.730 + printRawError(pos, "warning: " + msg); 1.731 + } 1.732 + prompt(); 1.733 + nwarnings++; 1.734 + errWriter.flush(); 1.735 + } 1.736 + 1.737 + public static String format(String fmt, Object... args) { 1.738 + return String.format((java.util.Locale)null, fmt, args); 1.739 + } 1.740 + 1.741 +}