src/share/classes/com/sun/tools/javac/util/Log.java

Sun, 17 Feb 2013 16:44:55 -0500

author
dholmes
date
Sun, 17 Feb 2013 16:44:55 -0500
changeset 1571
af8417e590f4
parent 1521
71f35e4b93a5
child 1613
d2a98dde7ecc
permissions
-rw-r--r--

Merge

duke@1 1 /*
jjg@1521 2 * Copyright (c) 1999, 2013, Oracle and/or its affiliates. All rights reserved.
duke@1 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
duke@1 4 *
duke@1 5 * This code is free software; you can redistribute it and/or modify it
duke@1 6 * under the terms of the GNU General Public License version 2 only, as
ohair@554 7 * published by the Free Software Foundation. Oracle designates this
duke@1 8 * particular file as subject to the "Classpath" exception as provided
ohair@554 9 * by Oracle in the LICENSE file that accompanied this code.
duke@1 10 *
duke@1 11 * This code is distributed in the hope that it will be useful, but WITHOUT
duke@1 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
duke@1 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
duke@1 14 * version 2 for more details (a copy is included in the LICENSE file that
duke@1 15 * accompanied this code).
duke@1 16 *
duke@1 17 * You should have received a copy of the GNU General Public License version
duke@1 18 * 2 along with this work; if not, write to the Free Software Foundation,
duke@1 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
duke@1 20 *
ohair@554 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ohair@554 22 * or visit www.oracle.com if you need additional information or have any
ohair@554 23 * questions.
duke@1 24 */
duke@1 25
duke@1 26 package com.sun.tools.javac.util;
duke@1 27
duke@1 28 import java.io.*;
jjg@194 29 import java.util.Arrays;
jjg@664 30 import java.util.EnumSet;
duke@1 31 import java.util.HashSet;
jjg@664 32 import java.util.Queue;
duke@1 33 import java.util.Set;
duke@1 34 import javax.tools.DiagnosticListener;
duke@1 35 import javax.tools.JavaFileObject;
jjg@50 36
jjg@700 37 import com.sun.tools.javac.api.DiagnosticFormatter;
jjg@1157 38 import com.sun.tools.javac.main.Main;
jjg@1157 39 import com.sun.tools.javac.main.Option;
jjg@1280 40 import com.sun.tools.javac.tree.EndPosTable;
duke@1 41 import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition;
duke@1 42 import com.sun.tools.javac.util.JCDiagnostic.DiagnosticType;
duke@1 43
jjg@1157 44 import static com.sun.tools.javac.main.Option.*;
jjg@700 45
duke@1 46 /** A class for error logs. Reports errors and warnings, and
duke@1 47 * keeps track of error numbers and positions.
duke@1 48 *
jjg@581 49 * <p><b>This is NOT part of any supported API.
jjg@581 50 * If you write code that depends on this, you do so at your own risk.
duke@1 51 * This code and its internal interfaces are subject to change or
duke@1 52 * deletion without notice.</b>
duke@1 53 */
jjg@73 54 public class Log extends AbstractLog {
duke@1 55 /** The context key for the log. */
duke@1 56 public static final Context.Key<Log> logKey
duke@1 57 = new Context.Key<Log>();
duke@1 58
duke@1 59 /** The context key for the output PrintWriter. */
duke@1 60 public static final Context.Key<PrintWriter> outKey =
duke@1 61 new Context.Key<PrintWriter>();
duke@1 62
jjg@1136 63 /* TODO: Should unify this with prefix handling in JCDiagnostic.Factory. */
jjg@1136 64 public enum PrefixKind {
jjg@1136 65 JAVAC("javac."),
jjg@1136 66 COMPILER_MISC("compiler.misc.");
jjg@1136 67 PrefixKind(String v) {
jjg@1136 68 value = v;
jjg@1136 69 }
jjg@1136 70 public String key(String k) {
jjg@1136 71 return value + k;
jjg@1136 72 }
jjg@1136 73 final String value;
jjg@1136 74 }
jjg@1136 75
jjg@1406 76 /**
jjg@1406 77 * DiagnosticHandler's provide the initial handling for diagnostics.
jjg@1406 78 * When a diagnostic handler is created and has been initialized, it
jjg@1406 79 * should install itself as the current diagnostic handler. When a
jjg@1406 80 * client has finished using a handler, the client should call
jjg@1406 81 * {@code log.removeDiagnosticHandler();}
jjg@1406 82 *
jjg@1406 83 * Note that javax.tools.DiagnosticListener (if set) is called later in the
jjg@1406 84 * diagnostic pipeline.
jjg@1406 85 */
jjg@1406 86 public static abstract class DiagnosticHandler {
jjg@1406 87 /**
jjg@1406 88 * The previously installed diagnostic handler.
jjg@1406 89 */
jjg@1406 90 protected DiagnosticHandler prev;
jjg@1406 91
jjg@1406 92 /**
jjg@1406 93 * Install this diagnostic handler as the current one,
jjg@1406 94 * recording the previous one.
jjg@1406 95 */
jjg@1406 96 protected void install(Log log) {
jjg@1406 97 prev = log.diagnosticHandler;
jjg@1406 98 log.diagnosticHandler = this;
jjg@1406 99 }
jjg@1406 100
jjg@1406 101 /**
jjg@1406 102 * Handle a diagnostic.
jjg@1406 103 */
jjg@1406 104 public abstract void report(JCDiagnostic diag);
jjg@1406 105 }
jjg@1406 106
jjg@1406 107 /**
jjg@1406 108 * A DiagnosticHandler that discards all diagnostics.
jjg@1406 109 */
jjg@1406 110 public static class DiscardDiagnosticHandler extends DiagnosticHandler {
jjg@1406 111 public DiscardDiagnosticHandler(Log log) {
jjg@1406 112 install(log);
jjg@1406 113 }
jjg@1406 114
jjg@1406 115 public void report(JCDiagnostic diag) { }
jjg@1406 116 }
jjg@1406 117
jjg@1406 118 /**
jjg@1406 119 * A DiagnosticHandler that can defer some or all diagnostics,
jjg@1406 120 * by buffering them for later examination and/or reporting.
jjg@1406 121 * If a diagnostic is not deferred, or is subsequently reported
jjg@1406 122 * with reportAllDiagnostics(), it will be reported to the previously
jjg@1406 123 * active diagnostic handler.
jjg@1406 124 */
jjg@1406 125 public static class DeferredDiagnosticHandler extends DiagnosticHandler {
jjg@1406 126 private Queue<JCDiagnostic> deferred = ListBuffer.lb();
jjg@1406 127 private final Filter<JCDiagnostic> filter;
jjg@1406 128
jjg@1406 129 public DeferredDiagnosticHandler(Log log) {
jjg@1406 130 this(log, null);
jjg@1406 131 }
jjg@1406 132
jjg@1406 133 public DeferredDiagnosticHandler(Log log, Filter<JCDiagnostic> filter) {
jjg@1406 134 this.filter = filter;
jjg@1406 135 install(log);
jjg@1406 136 }
jjg@1406 137
jjg@1406 138 public void report(JCDiagnostic diag) {
jjg@1406 139 if (filter == null || filter.accepts(diag))
jjg@1406 140 deferred.add(diag);
jjg@1406 141 else
jjg@1406 142 prev.report(diag);
jjg@1406 143 }
jjg@1406 144
jjg@1406 145 public Queue<JCDiagnostic> getDiagnostics() {
jjg@1406 146 return deferred;
jjg@1406 147 }
jjg@1406 148
jjg@1406 149 /** Report all deferred diagnostics. */
jjg@1406 150 public void reportDeferredDiagnostics() {
jjg@1406 151 reportDeferredDiagnostics(EnumSet.allOf(JCDiagnostic.Kind.class));
jjg@1406 152 }
jjg@1406 153
jjg@1406 154 /** Report selected deferred diagnostics. */
jjg@1406 155 public void reportDeferredDiagnostics(Set<JCDiagnostic.Kind> kinds) {
jjg@1406 156 JCDiagnostic d;
jjg@1406 157 while ((d = deferred.poll()) != null) {
jjg@1406 158 if (kinds.contains(d.getKind()))
jjg@1406 159 prev.report(d);
jjg@1406 160 }
jjg@1406 161 deferred = null; // prevent accidental ongoing use
jjg@1406 162 }
jjg@1406 163 }
jjg@1406 164
jjg@1135 165 public enum WriterKind { NOTICE, WARNING, ERROR };
duke@1 166
jjg@1135 167 protected PrintWriter errWriter;
duke@1 168
jjg@1135 169 protected PrintWriter warnWriter;
jjg@1135 170
jjg@1135 171 protected PrintWriter noticeWriter;
duke@1 172
duke@1 173 /** The maximum number of errors/warnings that are reported.
duke@1 174 */
jjg@1135 175 protected int MaxErrors;
jjg@1135 176 protected int MaxWarnings;
duke@1 177
duke@1 178 /** Switch: prompt user on each error.
duke@1 179 */
duke@1 180 public boolean promptOnError;
duke@1 181
duke@1 182 /** Switch: emit warning messages.
duke@1 183 */
duke@1 184 public boolean emitWarnings;
duke@1 185
jjg@376 186 /** Switch: suppress note messages.
jjg@376 187 */
jjg@376 188 public boolean suppressNotes;
jjg@376 189
duke@1 190 /** Print stack trace on errors?
duke@1 191 */
duke@1 192 public boolean dumpOnError;
duke@1 193
duke@1 194 /** Print multiple errors for same source locations.
duke@1 195 */
duke@1 196 public boolean multipleErrors;
duke@1 197
duke@1 198 /**
duke@1 199 * Diagnostic listener, if provided through programmatic
duke@1 200 * interface to javac (JSR 199).
duke@1 201 */
duke@1 202 protected DiagnosticListener<? super JavaFileObject> diagListener;
jjg@73 203
duke@1 204 /**
mcimadamore@221 205 * Formatter for diagnostics.
duke@1 206 */
mcimadamore@83 207 private DiagnosticFormatter<JCDiagnostic> diagFormatter;
duke@1 208
mcimadamore@136 209 /**
mcimadamore@221 210 * Keys for expected diagnostics.
jjg@194 211 */
jjg@194 212 public Set<String> expectDiagKeys;
jjg@194 213
jjg@194 214 /**
mcimadamore@221 215 * JavacMessages object used for localization.
mcimadamore@136 216 */
mcimadamore@136 217 private JavacMessages messages;
mcimadamore@136 218
jjg@664 219 /**
jjg@1521 220 * Handler for initial dispatch of diagnostics.
jjg@664 221 */
jjg@1406 222 private DiagnosticHandler diagnosticHandler;
jjg@664 223
duke@1 224 /** Construct a log with given I/O redirections.
duke@1 225 */
duke@1 226 protected Log(Context context, PrintWriter errWriter, PrintWriter warnWriter, PrintWriter noticeWriter) {
jjg@73 227 super(JCDiagnostic.Factory.instance(context));
duke@1 228 context.put(logKey, this);
duke@1 229 this.errWriter = errWriter;
duke@1 230 this.warnWriter = warnWriter;
duke@1 231 this.noticeWriter = noticeWriter;
duke@1 232
duke@1 233 @SuppressWarnings("unchecked") // FIXME
jjg@194 234 DiagnosticListener<? super JavaFileObject> dl =
duke@1 235 context.get(DiagnosticListener.class);
jjg@194 236 this.diagListener = dl;
jjg@194 237
jjg@1406 238 diagnosticHandler = new DefaultDiagnosticHandler();
jjg@1406 239
jjg@1135 240 messages = JavacMessages.instance(context);
jjg@1136 241 messages.add(Main.javacBundleName);
jjg@1135 242
jjg@1135 243 final Options options = Options.instance(context);
jjg@1135 244 initOptions(options);
jjg@1135 245 options.addListener(new Runnable() {
jjg@1135 246 public void run() {
jjg@1135 247 initOptions(options);
jjg@1135 248 }
jjg@1135 249 });
duke@1 250 }
duke@1 251 // where
jjg@1135 252 private void initOptions(Options options) {
jjg@1135 253 this.dumpOnError = options.isSet(DOE);
jjg@1135 254 this.promptOnError = options.isSet(PROMPT);
jjg@1135 255 this.emitWarnings = options.isUnset(XLINT_CUSTOM, "none");
jjg@1135 256 this.suppressNotes = options.isSet("suppressNotes");
jjg@1135 257 this.MaxErrors = getIntOption(options, XMAXERRS, getDefaultMaxErrors());
jjg@1135 258 this.MaxWarnings = getIntOption(options, XMAXWARNS, getDefaultMaxWarnings());
jjg@1135 259
jjg@1135 260 boolean rawDiagnostics = options.isSet("rawDiagnostics");
jjg@1135 261 this.diagFormatter = rawDiagnostics ? new RawDiagnosticFormatter(options) :
jjg@1135 262 new BasicDiagnosticFormatter(options, messages);
jjg@1135 263
jjg@1135 264 String ek = options.get("expectKeys");
jjg@1135 265 if (ek != null)
jjg@1135 266 expectDiagKeys = new HashSet<String>(Arrays.asList(ek.split(", *")));
jjg@1135 267 }
jjg@1135 268
jjg@1157 269 private int getIntOption(Options options, Option option, int defaultValue) {
jjg@1157 270 String s = options.get(option);
duke@1 271 try {
jjg@464 272 if (s != null) {
jjg@464 273 int n = Integer.parseInt(s);
jjg@464 274 return (n <= 0 ? Integer.MAX_VALUE : n);
jjg@464 275 }
duke@1 276 } catch (NumberFormatException e) {
duke@1 277 // silently ignore ill-formed numbers
duke@1 278 }
duke@1 279 return defaultValue;
duke@1 280 }
duke@1 281
jjg@584 282 /** Default value for -Xmaxerrs.
jjg@584 283 */
jjg@584 284 protected int getDefaultMaxErrors() {
jjg@584 285 return 100;
jjg@584 286 }
jjg@584 287
jjg@584 288 /** Default value for -Xmaxwarns.
jjg@584 289 */
jjg@584 290 protected int getDefaultMaxWarnings() {
jjg@584 291 return 100;
jjg@584 292 }
jjg@584 293
duke@1 294 /** The default writer for diagnostics
duke@1 295 */
jjg@1135 296 static PrintWriter defaultWriter(Context context) {
duke@1 297 PrintWriter result = context.get(outKey);
duke@1 298 if (result == null)
duke@1 299 context.put(outKey, result = new PrintWriter(System.err));
duke@1 300 return result;
duke@1 301 }
duke@1 302
duke@1 303 /** Construct a log with default settings.
duke@1 304 */
duke@1 305 protected Log(Context context) {
duke@1 306 this(context, defaultWriter(context));
duke@1 307 }
duke@1 308
duke@1 309 /** Construct a log with all output redirected.
duke@1 310 */
duke@1 311 protected Log(Context context, PrintWriter defaultWriter) {
duke@1 312 this(context, defaultWriter, defaultWriter, defaultWriter);
duke@1 313 }
duke@1 314
duke@1 315 /** Get the Log instance for this context. */
duke@1 316 public static Log instance(Context context) {
duke@1 317 Log instance = context.get(logKey);
duke@1 318 if (instance == null)
duke@1 319 instance = new Log(context);
duke@1 320 return instance;
duke@1 321 }
duke@1 322
duke@1 323 /** The number of errors encountered so far.
duke@1 324 */
duke@1 325 public int nerrors = 0;
duke@1 326
duke@1 327 /** The number of warnings encountered so far.
duke@1 328 */
duke@1 329 public int nwarnings = 0;
duke@1 330
duke@1 331 /** A set of all errors generated so far. This is used to avoid printing an
duke@1 332 * error message more than once. For each error, a pair consisting of the
duke@1 333 * source file name and source code position of the error is added to the set.
duke@1 334 */
duke@1 335 private Set<Pair<JavaFileObject, Integer>> recorded = new HashSet<Pair<JavaFileObject,Integer>>();
duke@1 336
duke@1 337 public boolean hasDiagnosticListener() {
duke@1 338 return diagListener != null;
duke@1 339 }
duke@1 340
ksrini@1138 341 public void setEndPosTable(JavaFileObject name, EndPosTable endPosTable) {
jjg@73 342 name.getClass(); // null check
ksrini@1138 343 getSource(name).setEndPosTable(endPosTable);
duke@1 344 }
duke@1 345
mcimadamore@168 346 /** Return current sourcefile.
duke@1 347 */
mcimadamore@168 348 public JavaFileObject currentSourceFile() {
duke@1 349 return source == null ? null : source.getFile();
duke@1 350 }
duke@1 351
mcimadamore@221 352 /** Get the current diagnostic formatter.
mcimadamore@221 353 */
mcimadamore@221 354 public DiagnosticFormatter<JCDiagnostic> getDiagnosticFormatter() {
mcimadamore@221 355 return diagFormatter;
mcimadamore@221 356 }
mcimadamore@221 357
mcimadamore@221 358 /** Set the current diagnostic formatter.
mcimadamore@221 359 */
mcimadamore@221 360 public void setDiagnosticFormatter(DiagnosticFormatter<JCDiagnostic> diagFormatter) {
mcimadamore@221 361 this.diagFormatter = diagFormatter;
mcimadamore@221 362 }
mcimadamore@221 363
jjg@1135 364 public PrintWriter getWriter(WriterKind kind) {
jjg@1135 365 switch (kind) {
jjg@1135 366 case NOTICE: return noticeWriter;
jjg@1135 367 case WARNING: return warnWriter;
jjg@1135 368 case ERROR: return errWriter;
jjg@1135 369 default: throw new IllegalArgumentException();
jjg@1135 370 }
jjg@1135 371 }
jjg@1135 372
jjg@1135 373 public void setWriter(WriterKind kind, PrintWriter pw) {
jjg@1135 374 pw.getClass();
jjg@1135 375 switch (kind) {
jjg@1135 376 case NOTICE: noticeWriter = pw; break;
jjg@1135 377 case WARNING: warnWriter = pw; break;
jjg@1135 378 case ERROR: errWriter = pw; break;
jjg@1135 379 default: throw new IllegalArgumentException();
jjg@1135 380 }
jjg@1135 381 }
jjg@1135 382
jjg@1135 383 public void setWriters(PrintWriter pw) {
jjg@1135 384 pw.getClass();
jjg@1135 385 noticeWriter = warnWriter = errWriter = pw;
jjg@1135 386 }
jjg@1135 387
jjg@1521 388 /**
jjg@1521 389 * Propagate the previous log's information.
jjg@1521 390 */
jjg@1521 391 public void initRound(Log other) {
jjg@1159 392 this.noticeWriter = other.noticeWriter;
jjg@1159 393 this.warnWriter = other.warnWriter;
jjg@1159 394 this.errWriter = other.errWriter;
jjg@1323 395 this.sourceMap = other.sourceMap;
jjg@1521 396 this.recorded = other.recorded;
jjg@1521 397 this.nerrors = other.nerrors;
jjg@1521 398 this.nwarnings = other.nwarnings;
jjg@1323 399 }
jjg@1323 400
jjg@1406 401 /**
jjg@1406 402 * Replace the specified diagnostic handler with the
jjg@1406 403 * handler that was current at the time this handler was created.
jjg@1406 404 * The given handler must be the currently installed handler;
jjg@1406 405 * it must be specified explicitly for clarity and consistency checking.
jjg@1406 406 */
jjg@1406 407 public void popDiagnosticHandler(DiagnosticHandler h) {
jjg@1406 408 Assert.check(diagnosticHandler == h);
jjg@1406 409 diagnosticHandler = h.prev;
jjg@1406 410 }
jjg@1406 411
duke@1 412 /** Flush the logs
duke@1 413 */
duke@1 414 public void flush() {
duke@1 415 errWriter.flush();
duke@1 416 warnWriter.flush();
duke@1 417 noticeWriter.flush();
duke@1 418 }
duke@1 419
jjg@1135 420 public void flush(WriterKind kind) {
jjg@1135 421 getWriter(kind).flush();
jjg@1135 422 }
jjg@1135 423
duke@1 424 /** Returns true if an error needs to be reported for a given
duke@1 425 * source name and pos.
duke@1 426 */
duke@1 427 protected boolean shouldReport(JavaFileObject file, int pos) {
duke@1 428 if (multipleErrors || file == null)
duke@1 429 return true;
duke@1 430
duke@1 431 Pair<JavaFileObject,Integer> coords = new Pair<JavaFileObject,Integer>(file, pos);
duke@1 432 boolean shouldReport = !recorded.contains(coords);
duke@1 433 if (shouldReport)
duke@1 434 recorded.add(coords);
duke@1 435 return shouldReport;
duke@1 436 }
duke@1 437
duke@1 438 /** Prompt user after an error.
duke@1 439 */
duke@1 440 public void prompt() {
duke@1 441 if (promptOnError) {
jjg@604 442 System.err.println(localize("resume.abort"));
duke@1 443 try {
duke@1 444 while (true) {
duke@1 445 switch (System.in.read()) {
duke@1 446 case 'a': case 'A':
duke@1 447 System.exit(-1);
duke@1 448 return;
duke@1 449 case 'r': case 'R':
duke@1 450 return;
duke@1 451 case 'x': case 'X':
duke@1 452 throw new AssertionError("user abort");
duke@1 453 default:
duke@1 454 }
duke@1 455 }
duke@1 456 } catch (IOException e) {}
duke@1 457 }
duke@1 458 }
duke@1 459
duke@1 460 /** Print the faulty source code line and point to the error.
duke@1 461 * @param pos Buffer index of the error position, must be on current line
duke@1 462 */
duke@1 463 private void printErrLine(int pos, PrintWriter writer) {
jjg@73 464 String line = (source == null ? null : source.getLine(pos));
jjg@73 465 if (line == null)
duke@1 466 return;
mcimadamore@100 467 int col = source.getColumnNumber(pos, false);
duke@1 468
jjg@1136 469 printRawLines(writer, line);
jjg@73 470 for (int i = 0; i < col - 1; i++) {
jjg@73 471 writer.print((line.charAt(i) == '\t') ? "\t" : " ");
duke@1 472 }
duke@1 473 writer.println("^");
duke@1 474 writer.flush();
duke@1 475 }
duke@1 476
jjg@1136 477 public void printNewline() {
jjg@1136 478 noticeWriter.println();
jjg@1136 479 }
jjg@1136 480
jjg@1136 481 public void printNewline(WriterKind wk) {
jjg@1136 482 getWriter(wk).println();
jjg@1136 483 }
jjg@1136 484
jjg@1136 485 public void printLines(String key, Object... args) {
jjg@1136 486 printRawLines(noticeWriter, localize(key, args));
jjg@1136 487 }
jjg@1136 488
jjg@1136 489 public void printLines(PrefixKind pk, String key, Object... args) {
jjg@1136 490 printRawLines(noticeWriter, localize(pk, key, args));
jjg@1136 491 }
jjg@1136 492
jjg@1136 493 public void printLines(WriterKind wk, String key, Object... args) {
jjg@1136 494 printRawLines(getWriter(wk), localize(key, args));
jjg@1136 495 }
jjg@1136 496
jjg@1136 497 public void printLines(WriterKind wk, PrefixKind pk, String key, Object... args) {
jjg@1136 498 printRawLines(getWriter(wk), localize(pk, key, args));
jjg@1135 499 }
jjg@1135 500
jjg@1135 501 /** Print the text of a message, translating newlines appropriately
jjg@1135 502 * for the platform.
jjg@1135 503 */
jjg@1136 504 public void printRawLines(String msg) {
jjg@1136 505 printRawLines(noticeWriter, msg);
jjg@1136 506 }
jjg@1136 507
jjg@1136 508 /** Print the text of a message, translating newlines appropriately
jjg@1136 509 * for the platform.
jjg@1136 510 */
jjg@1136 511 public void printRawLines(WriterKind kind, String msg) {
jjg@1136 512 printRawLines(getWriter(kind), msg);
jjg@1136 513 }
jjg@1136 514
jjg@1136 515 /** Print the text of a message, translating newlines appropriately
jjg@1136 516 * for the platform.
jjg@1136 517 */
jjg@1136 518 public static void printRawLines(PrintWriter writer, String msg) {
duke@1 519 int nl;
duke@1 520 while ((nl = msg.indexOf('\n')) != -1) {
duke@1 521 writer.println(msg.substring(0, nl));
duke@1 522 msg = msg.substring(nl+1);
duke@1 523 }
duke@1 524 if (msg.length() != 0) writer.println(msg);
duke@1 525 }
duke@1 526
jjg@909 527 /**
jjg@909 528 * Print the localized text of a "verbose" message to the
jjg@909 529 * noticeWriter stream.
jjg@909 530 */
jjg@909 531 public void printVerbose(String key, Object... args) {
jjg@1136 532 printRawLines(noticeWriter, localize("verbose." + key, args));
jjg@909 533 }
jjg@909 534
jjg@73 535 protected void directError(String key, Object... args) {
jjg@1136 536 printRawLines(errWriter, localize(key, args));
jjg@73 537 errWriter.flush();
duke@1 538 }
duke@1 539
duke@1 540 /** Report a warning that cannot be suppressed.
duke@1 541 * @param pos The source position at which to report the warning.
duke@1 542 * @param key The key for the localized warning message.
duke@1 543 * @param args Fields of the warning message.
duke@1 544 */
duke@1 545 public void strictWarning(DiagnosticPosition pos, String key, Object ... args) {
duke@1 546 writeDiagnostic(diags.warning(source, pos, key, args));
duke@1 547 nwarnings++;
duke@1 548 }
duke@1 549
jjg@1406 550 /**
jjg@1406 551 * Primary method to report a diagnostic.
jjg@1406 552 * @param diagnostic
jjg@1406 553 */
jjg@1406 554 public void report(JCDiagnostic diagnostic) {
jjg@1406 555 diagnosticHandler.report(diagnostic);
jjg@1406 556 }
jjg@664 557
duke@1 558 /**
duke@1 559 * Common diagnostic handling.
duke@1 560 * The diagnostic is counted, and depending on the options and how many diagnostics have been
duke@1 561 * reported so far, the diagnostic may be handed off to writeDiagnostic.
duke@1 562 */
jjg@1406 563 private class DefaultDiagnosticHandler extends DiagnosticHandler {
jjg@1406 564 public void report(JCDiagnostic diagnostic) {
jjg@1406 565 if (expectDiagKeys != null)
jjg@1406 566 expectDiagKeys.remove(diagnostic.getCode());
jjg@664 567
jjg@1406 568 switch (diagnostic.getType()) {
jjg@1406 569 case FRAGMENT:
jjg@1406 570 throw new IllegalArgumentException();
jjg@194 571
jjg@1406 572 case NOTE:
jjg@1406 573 // Print out notes only when we are permitted to report warnings
jjg@1406 574 // Notes are only generated at the end of a compilation, so should be small
jjg@1406 575 // in number.
jjg@1406 576 if ((emitWarnings || diagnostic.isMandatory()) && !suppressNotes) {
jjg@1406 577 writeDiagnostic(diagnostic);
jjg@1406 578 }
jjg@1406 579 break;
duke@1 580
jjg@1406 581 case WARNING:
jjg@1406 582 if (emitWarnings || diagnostic.isMandatory()) {
jjg@1406 583 if (nwarnings < MaxWarnings) {
jjg@1406 584 writeDiagnostic(diagnostic);
jjg@1406 585 nwarnings++;
jjg@1406 586 }
jjg@1406 587 }
jjg@1406 588 break;
jjg@1406 589
jjg@1406 590 case ERROR:
jjg@1406 591 if (nerrors < MaxErrors
jjg@1406 592 && shouldReport(diagnostic.getSource(), diagnostic.getIntPosition())) {
jjg@1406 593 writeDiagnostic(diagnostic);
jjg@1406 594 nerrors++;
jjg@1406 595 }
jjg@1406 596 break;
duke@1 597 }
duke@1 598 }
duke@1 599 }
duke@1 600
duke@1 601 /**
duke@1 602 * Write out a diagnostic.
duke@1 603 */
duke@1 604 protected void writeDiagnostic(JCDiagnostic diag) {
duke@1 605 if (diagListener != null) {
jjg@946 606 diagListener.report(diag);
jjg@946 607 return;
duke@1 608 }
duke@1 609
duke@1 610 PrintWriter writer = getWriterForDiagnosticType(diag.getType());
duke@1 611
jjg@1136 612 printRawLines(writer, diagFormatter.format(diag, messages.getCurrentLocale()));
duke@1 613
duke@1 614 if (promptOnError) {
duke@1 615 switch (diag.getType()) {
duke@1 616 case ERROR:
duke@1 617 case WARNING:
duke@1 618 prompt();
duke@1 619 }
duke@1 620 }
duke@1 621
duke@1 622 if (dumpOnError)
duke@1 623 new RuntimeException().printStackTrace(writer);
duke@1 624
duke@1 625 writer.flush();
duke@1 626 }
duke@1 627
duke@1 628 @Deprecated
duke@1 629 protected PrintWriter getWriterForDiagnosticType(DiagnosticType dt) {
duke@1 630 switch (dt) {
duke@1 631 case FRAGMENT:
duke@1 632 throw new IllegalArgumentException();
duke@1 633
duke@1 634 case NOTE:
duke@1 635 return noticeWriter;
duke@1 636
duke@1 637 case WARNING:
duke@1 638 return warnWriter;
duke@1 639
duke@1 640 case ERROR:
duke@1 641 return errWriter;
duke@1 642
duke@1 643 default:
duke@1 644 throw new Error();
duke@1 645 }
duke@1 646 }
duke@1 647
duke@1 648 /** Find a localized string in the resource bundle.
jjg@604 649 * Because this method is static, it ignores the locale.
jjg@604 650 * Use localize(key, args) when possible.
duke@1 651 * @param key The key for the localized string.
duke@1 652 * @param args Fields to substitute into the string.
duke@1 653 */
duke@1 654 public static String getLocalizedString(String key, Object ... args) {
jjg@1136 655 return JavacMessages.getDefaultLocalizedString(PrefixKind.COMPILER_MISC.key(key), args);
duke@1 656 }
duke@1 657
jjg@604 658 /** Find a localized string in the resource bundle.
jjg@604 659 * @param key The key for the localized string.
jjg@604 660 * @param args Fields to substitute into the string.
jjg@604 661 */
jjg@604 662 public String localize(String key, Object... args) {
jjg@1136 663 return localize(PrefixKind.COMPILER_MISC, key, args);
jjg@604 664 }
jjg@604 665
jjg@1136 666 /** Find a localized string in the resource bundle.
jjg@1136 667 * @param key The key for the localized string.
jjg@1136 668 * @param args Fields to substitute into the string.
jjg@1136 669 */
jjg@1136 670 public String localize(PrefixKind pk, String key, Object... args) {
jjg@1136 671 if (useRawMessages)
jjg@1136 672 return pk.key(key);
jjg@1136 673 else
jjg@1136 674 return messages.getLocalizedString(pk.key(key), args);
jjg@1136 675 }
jjg@1136 676 // where
jjg@1136 677 // backdoor hook for testing, should transition to use -XDrawDiagnostics
jjg@1136 678 private static boolean useRawMessages = false;
jjg@1136 679
duke@1 680 /***************************************************************************
duke@1 681 * raw error messages without internationalization; used for experimentation
duke@1 682 * and quick prototyping
duke@1 683 ***************************************************************************/
duke@1 684
jjg@73 685 /** print an error or warning message:
jjg@73 686 */
duke@1 687 private void printRawError(int pos, String msg) {
jjg@73 688 if (source == null || pos == Position.NOPOS) {
jjg@1136 689 printRawLines(errWriter, "error: " + msg);
duke@1 690 } else {
jjg@73 691 int line = source.getLineNumber(pos);
mcimadamore@168 692 JavaFileObject file = source.getFile();
duke@1 693 if (file != null)
jjg@1136 694 printRawLines(errWriter,
jjg@415 695 file.getName() + ":" +
duke@1 696 line + ": " + msg);
duke@1 697 printErrLine(pos, errWriter);
duke@1 698 }
duke@1 699 errWriter.flush();
duke@1 700 }
duke@1 701
jjg@73 702 /** report an error:
jjg@73 703 */
duke@1 704 public void rawError(int pos, String msg) {
mcimadamore@168 705 if (nerrors < MaxErrors && shouldReport(currentSourceFile(), pos)) {
duke@1 706 printRawError(pos, msg);
duke@1 707 prompt();
duke@1 708 nerrors++;
duke@1 709 }
duke@1 710 errWriter.flush();
duke@1 711 }
duke@1 712
jjg@73 713 /** report a warning:
jjg@73 714 */
duke@1 715 public void rawWarning(int pos, String msg) {
duke@1 716 if (nwarnings < MaxWarnings && emitWarnings) {
duke@1 717 printRawError(pos, "warning: " + msg);
duke@1 718 }
duke@1 719 prompt();
duke@1 720 nwarnings++;
duke@1 721 errWriter.flush();
duke@1 722 }
duke@1 723
duke@1 724 public static String format(String fmt, Object... args) {
duke@1 725 return String.format((java.util.Locale)null, fmt, args);
duke@1 726 }
duke@1 727
duke@1 728 }

mercurial