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

Thu, 15 Nov 2012 23:07:24 -0800

author
jjg
date
Thu, 15 Nov 2012 23:07:24 -0800
changeset 1413
bdcef2ef52d2
parent 1406
2901c7b5339e
child 1521
71f35e4b93a5
permissions
-rw-r--r--

6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
Reviewed-by: darcy

duke@1 1 /*
jjg@1280 2 * Copyright (c) 1999, 2012, 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@1406 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@1159 388 public void setWriters(Log other) {
jjg@1159 389 this.noticeWriter = other.noticeWriter;
jjg@1159 390 this.warnWriter = other.warnWriter;
jjg@1159 391 this.errWriter = other.errWriter;
jjg@1159 392 }
jjg@1159 393
jjg@1323 394 public void setSourceMap(Log other) {
jjg@1323 395 this.sourceMap = other.sourceMap;
jjg@1323 396 }
jjg@1323 397
jjg@1406 398 /**
jjg@1406 399 * Replace the specified diagnostic handler with the
jjg@1406 400 * handler that was current at the time this handler was created.
jjg@1406 401 * The given handler must be the currently installed handler;
jjg@1406 402 * it must be specified explicitly for clarity and consistency checking.
jjg@1406 403 */
jjg@1406 404 public void popDiagnosticHandler(DiagnosticHandler h) {
jjg@1406 405 Assert.check(diagnosticHandler == h);
jjg@1406 406 diagnosticHandler = h.prev;
jjg@1406 407 }
jjg@1406 408
duke@1 409 /** Flush the logs
duke@1 410 */
duke@1 411 public void flush() {
duke@1 412 errWriter.flush();
duke@1 413 warnWriter.flush();
duke@1 414 noticeWriter.flush();
duke@1 415 }
duke@1 416
jjg@1135 417 public void flush(WriterKind kind) {
jjg@1135 418 getWriter(kind).flush();
jjg@1135 419 }
jjg@1135 420
duke@1 421 /** Returns true if an error needs to be reported for a given
duke@1 422 * source name and pos.
duke@1 423 */
duke@1 424 protected boolean shouldReport(JavaFileObject file, int pos) {
duke@1 425 if (multipleErrors || file == null)
duke@1 426 return true;
duke@1 427
duke@1 428 Pair<JavaFileObject,Integer> coords = new Pair<JavaFileObject,Integer>(file, pos);
duke@1 429 boolean shouldReport = !recorded.contains(coords);
duke@1 430 if (shouldReport)
duke@1 431 recorded.add(coords);
duke@1 432 return shouldReport;
duke@1 433 }
duke@1 434
duke@1 435 /** Prompt user after an error.
duke@1 436 */
duke@1 437 public void prompt() {
duke@1 438 if (promptOnError) {
jjg@604 439 System.err.println(localize("resume.abort"));
duke@1 440 try {
duke@1 441 while (true) {
duke@1 442 switch (System.in.read()) {
duke@1 443 case 'a': case 'A':
duke@1 444 System.exit(-1);
duke@1 445 return;
duke@1 446 case 'r': case 'R':
duke@1 447 return;
duke@1 448 case 'x': case 'X':
duke@1 449 throw new AssertionError("user abort");
duke@1 450 default:
duke@1 451 }
duke@1 452 }
duke@1 453 } catch (IOException e) {}
duke@1 454 }
duke@1 455 }
duke@1 456
duke@1 457 /** Print the faulty source code line and point to the error.
duke@1 458 * @param pos Buffer index of the error position, must be on current line
duke@1 459 */
duke@1 460 private void printErrLine(int pos, PrintWriter writer) {
jjg@73 461 String line = (source == null ? null : source.getLine(pos));
jjg@73 462 if (line == null)
duke@1 463 return;
mcimadamore@100 464 int col = source.getColumnNumber(pos, false);
duke@1 465
jjg@1136 466 printRawLines(writer, line);
jjg@73 467 for (int i = 0; i < col - 1; i++) {
jjg@73 468 writer.print((line.charAt(i) == '\t') ? "\t" : " ");
duke@1 469 }
duke@1 470 writer.println("^");
duke@1 471 writer.flush();
duke@1 472 }
duke@1 473
jjg@1136 474 public void printNewline() {
jjg@1136 475 noticeWriter.println();
jjg@1136 476 }
jjg@1136 477
jjg@1136 478 public void printNewline(WriterKind wk) {
jjg@1136 479 getWriter(wk).println();
jjg@1136 480 }
jjg@1136 481
jjg@1136 482 public void printLines(String key, Object... args) {
jjg@1136 483 printRawLines(noticeWriter, localize(key, args));
jjg@1136 484 }
jjg@1136 485
jjg@1136 486 public void printLines(PrefixKind pk, String key, Object... args) {
jjg@1136 487 printRawLines(noticeWriter, localize(pk, key, args));
jjg@1136 488 }
jjg@1136 489
jjg@1136 490 public void printLines(WriterKind wk, String key, Object... args) {
jjg@1136 491 printRawLines(getWriter(wk), localize(key, args));
jjg@1136 492 }
jjg@1136 493
jjg@1136 494 public void printLines(WriterKind wk, PrefixKind pk, String key, Object... args) {
jjg@1136 495 printRawLines(getWriter(wk), localize(pk, key, args));
jjg@1135 496 }
jjg@1135 497
jjg@1135 498 /** Print the text of a message, translating newlines appropriately
jjg@1135 499 * for the platform.
jjg@1135 500 */
jjg@1136 501 public void printRawLines(String msg) {
jjg@1136 502 printRawLines(noticeWriter, msg);
jjg@1136 503 }
jjg@1136 504
jjg@1136 505 /** Print the text of a message, translating newlines appropriately
jjg@1136 506 * for the platform.
jjg@1136 507 */
jjg@1136 508 public void printRawLines(WriterKind kind, String msg) {
jjg@1136 509 printRawLines(getWriter(kind), msg);
jjg@1136 510 }
jjg@1136 511
jjg@1136 512 /** Print the text of a message, translating newlines appropriately
jjg@1136 513 * for the platform.
jjg@1136 514 */
jjg@1136 515 public static void printRawLines(PrintWriter writer, String msg) {
duke@1 516 int nl;
duke@1 517 while ((nl = msg.indexOf('\n')) != -1) {
duke@1 518 writer.println(msg.substring(0, nl));
duke@1 519 msg = msg.substring(nl+1);
duke@1 520 }
duke@1 521 if (msg.length() != 0) writer.println(msg);
duke@1 522 }
duke@1 523
jjg@909 524 /**
jjg@909 525 * Print the localized text of a "verbose" message to the
jjg@909 526 * noticeWriter stream.
jjg@909 527 */
jjg@909 528 public void printVerbose(String key, Object... args) {
jjg@1136 529 printRawLines(noticeWriter, localize("verbose." + key, args));
jjg@909 530 }
jjg@909 531
jjg@73 532 protected void directError(String key, Object... args) {
jjg@1136 533 printRawLines(errWriter, localize(key, args));
jjg@73 534 errWriter.flush();
duke@1 535 }
duke@1 536
duke@1 537 /** Report a warning that cannot be suppressed.
duke@1 538 * @param pos The source position at which to report the warning.
duke@1 539 * @param key The key for the localized warning message.
duke@1 540 * @param args Fields of the warning message.
duke@1 541 */
duke@1 542 public void strictWarning(DiagnosticPosition pos, String key, Object ... args) {
duke@1 543 writeDiagnostic(diags.warning(source, pos, key, args));
duke@1 544 nwarnings++;
duke@1 545 }
duke@1 546
jjg@1406 547 /**
jjg@1406 548 * Primary method to report a diagnostic.
jjg@1406 549 * @param diagnostic
jjg@1406 550 */
jjg@1406 551 public void report(JCDiagnostic diagnostic) {
jjg@1406 552 diagnosticHandler.report(diagnostic);
jjg@1406 553 }
jjg@664 554
duke@1 555 /**
duke@1 556 * Common diagnostic handling.
duke@1 557 * The diagnostic is counted, and depending on the options and how many diagnostics have been
duke@1 558 * reported so far, the diagnostic may be handed off to writeDiagnostic.
duke@1 559 */
jjg@1406 560 private class DefaultDiagnosticHandler extends DiagnosticHandler {
jjg@1406 561 public void report(JCDiagnostic diagnostic) {
jjg@1406 562 if (expectDiagKeys != null)
jjg@1406 563 expectDiagKeys.remove(diagnostic.getCode());
jjg@664 564
jjg@1406 565 switch (diagnostic.getType()) {
jjg@1406 566 case FRAGMENT:
jjg@1406 567 throw new IllegalArgumentException();
jjg@194 568
jjg@1406 569 case NOTE:
jjg@1406 570 // Print out notes only when we are permitted to report warnings
jjg@1406 571 // Notes are only generated at the end of a compilation, so should be small
jjg@1406 572 // in number.
jjg@1406 573 if ((emitWarnings || diagnostic.isMandatory()) && !suppressNotes) {
jjg@1406 574 writeDiagnostic(diagnostic);
jjg@1406 575 }
jjg@1406 576 break;
duke@1 577
jjg@1406 578 case WARNING:
jjg@1406 579 if (emitWarnings || diagnostic.isMandatory()) {
jjg@1406 580 if (nwarnings < MaxWarnings) {
jjg@1406 581 writeDiagnostic(diagnostic);
jjg@1406 582 nwarnings++;
jjg@1406 583 }
jjg@1406 584 }
jjg@1406 585 break;
jjg@1406 586
jjg@1406 587 case ERROR:
jjg@1406 588 if (nerrors < MaxErrors
jjg@1406 589 && shouldReport(diagnostic.getSource(), diagnostic.getIntPosition())) {
jjg@1406 590 writeDiagnostic(diagnostic);
jjg@1406 591 nerrors++;
jjg@1406 592 }
jjg@1406 593 break;
duke@1 594 }
duke@1 595 }
duke@1 596 }
duke@1 597
duke@1 598 /**
duke@1 599 * Write out a diagnostic.
duke@1 600 */
duke@1 601 protected void writeDiagnostic(JCDiagnostic diag) {
duke@1 602 if (diagListener != null) {
jjg@946 603 diagListener.report(diag);
jjg@946 604 return;
duke@1 605 }
duke@1 606
duke@1 607 PrintWriter writer = getWriterForDiagnosticType(diag.getType());
duke@1 608
jjg@1136 609 printRawLines(writer, diagFormatter.format(diag, messages.getCurrentLocale()));
duke@1 610
duke@1 611 if (promptOnError) {
duke@1 612 switch (diag.getType()) {
duke@1 613 case ERROR:
duke@1 614 case WARNING:
duke@1 615 prompt();
duke@1 616 }
duke@1 617 }
duke@1 618
duke@1 619 if (dumpOnError)
duke@1 620 new RuntimeException().printStackTrace(writer);
duke@1 621
duke@1 622 writer.flush();
duke@1 623 }
duke@1 624
duke@1 625 @Deprecated
duke@1 626 protected PrintWriter getWriterForDiagnosticType(DiagnosticType dt) {
duke@1 627 switch (dt) {
duke@1 628 case FRAGMENT:
duke@1 629 throw new IllegalArgumentException();
duke@1 630
duke@1 631 case NOTE:
duke@1 632 return noticeWriter;
duke@1 633
duke@1 634 case WARNING:
duke@1 635 return warnWriter;
duke@1 636
duke@1 637 case ERROR:
duke@1 638 return errWriter;
duke@1 639
duke@1 640 default:
duke@1 641 throw new Error();
duke@1 642 }
duke@1 643 }
duke@1 644
duke@1 645 /** Find a localized string in the resource bundle.
jjg@604 646 * Because this method is static, it ignores the locale.
jjg@604 647 * Use localize(key, args) when possible.
duke@1 648 * @param key The key for the localized string.
duke@1 649 * @param args Fields to substitute into the string.
duke@1 650 */
duke@1 651 public static String getLocalizedString(String key, Object ... args) {
jjg@1136 652 return JavacMessages.getDefaultLocalizedString(PrefixKind.COMPILER_MISC.key(key), args);
duke@1 653 }
duke@1 654
jjg@604 655 /** Find a localized string in the resource bundle.
jjg@604 656 * @param key The key for the localized string.
jjg@604 657 * @param args Fields to substitute into the string.
jjg@604 658 */
jjg@604 659 public String localize(String key, Object... args) {
jjg@1136 660 return localize(PrefixKind.COMPILER_MISC, key, args);
jjg@604 661 }
jjg@604 662
jjg@1136 663 /** Find a localized string in the resource bundle.
jjg@1136 664 * @param key The key for the localized string.
jjg@1136 665 * @param args Fields to substitute into the string.
jjg@1136 666 */
jjg@1136 667 public String localize(PrefixKind pk, String key, Object... args) {
jjg@1136 668 if (useRawMessages)
jjg@1136 669 return pk.key(key);
jjg@1136 670 else
jjg@1136 671 return messages.getLocalizedString(pk.key(key), args);
jjg@1136 672 }
jjg@1136 673 // where
jjg@1136 674 // backdoor hook for testing, should transition to use -XDrawDiagnostics
jjg@1136 675 private static boolean useRawMessages = false;
jjg@1136 676
duke@1 677 /***************************************************************************
duke@1 678 * raw error messages without internationalization; used for experimentation
duke@1 679 * and quick prototyping
duke@1 680 ***************************************************************************/
duke@1 681
jjg@73 682 /** print an error or warning message:
jjg@73 683 */
duke@1 684 private void printRawError(int pos, String msg) {
jjg@73 685 if (source == null || pos == Position.NOPOS) {
jjg@1136 686 printRawLines(errWriter, "error: " + msg);
duke@1 687 } else {
jjg@73 688 int line = source.getLineNumber(pos);
mcimadamore@168 689 JavaFileObject file = source.getFile();
duke@1 690 if (file != null)
jjg@1136 691 printRawLines(errWriter,
jjg@415 692 file.getName() + ":" +
duke@1 693 line + ": " + msg);
duke@1 694 printErrLine(pos, errWriter);
duke@1 695 }
duke@1 696 errWriter.flush();
duke@1 697 }
duke@1 698
jjg@73 699 /** report an error:
jjg@73 700 */
duke@1 701 public void rawError(int pos, String msg) {
mcimadamore@168 702 if (nerrors < MaxErrors && shouldReport(currentSourceFile(), pos)) {
duke@1 703 printRawError(pos, msg);
duke@1 704 prompt();
duke@1 705 nerrors++;
duke@1 706 }
duke@1 707 errWriter.flush();
duke@1 708 }
duke@1 709
jjg@73 710 /** report a warning:
jjg@73 711 */
duke@1 712 public void rawWarning(int pos, String msg) {
duke@1 713 if (nwarnings < MaxWarnings && emitWarnings) {
duke@1 714 printRawError(pos, "warning: " + msg);
duke@1 715 }
duke@1 716 prompt();
duke@1 717 nwarnings++;
duke@1 718 errWriter.flush();
duke@1 719 }
duke@1 720
duke@1 721 public static String format(String fmt, Object... args) {
duke@1 722 return String.format((java.util.Locale)null, fmt, args);
duke@1 723 }
duke@1 724
duke@1 725 }

mercurial