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

Sat, 01 Dec 2007 00:00:00 +0000

author
duke
date
Sat, 01 Dec 2007 00:00:00 +0000
changeset 1
9a66ca7c79fa
child 10
508c01999047
permissions
-rw-r--r--

Initial load

duke@1 1 /*
duke@1 2 * Copyright 1999-2006 Sun Microsystems, Inc. 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
duke@1 7 * published by the Free Software Foundation. Sun designates this
duke@1 8 * particular file as subject to the "Classpath" exception as provided
duke@1 9 * by Sun 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 *
duke@1 21 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
duke@1 22 * CA 95054 USA or visit www.sun.com if you need additional information or
duke@1 23 * have any 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.*;
duke@1 29 import java.nio.CharBuffer;
duke@1 30 import java.util.HashMap;
duke@1 31 import java.util.HashSet;
duke@1 32 import java.util.Map;
duke@1 33 import java.util.Set;
duke@1 34 import javax.tools.DiagnosticListener;
duke@1 35 import javax.tools.JavaFileObject;
duke@1 36 import com.sun.tools.javac.code.Source;
duke@1 37 import com.sun.tools.javac.tree.JCTree;
duke@1 38 import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition;
duke@1 39 import com.sun.tools.javac.util.JCDiagnostic.DiagnosticType;
duke@1 40 import com.sun.tools.javac.util.JCDiagnostic.SimpleDiagnosticPosition;
duke@1 41 import static com.sun.tools.javac.util.LayoutCharacters.*;
duke@1 42
duke@1 43 /** A class for error logs. Reports errors and warnings, and
duke@1 44 * keeps track of error numbers and positions.
duke@1 45 *
duke@1 46 * <p><b>This is NOT part of any API supported by Sun Microsystems. If
duke@1 47 * you write code that depends on this, you do so at your own risk.
duke@1 48 * This code and its internal interfaces are subject to change or
duke@1 49 * deletion without notice.</b>
duke@1 50 */
duke@1 51 public class Log {
duke@1 52 /** The context key for the log. */
duke@1 53 public static final Context.Key<Log> logKey
duke@1 54 = new Context.Key<Log>();
duke@1 55
duke@1 56 /** The context key for the output PrintWriter. */
duke@1 57 public static final Context.Key<PrintWriter> outKey =
duke@1 58 new Context.Key<PrintWriter>();
duke@1 59
duke@1 60 //@Deprecated
duke@1 61 public final PrintWriter errWriter;
duke@1 62
duke@1 63 //@Deprecated
duke@1 64 public final PrintWriter warnWriter;
duke@1 65
duke@1 66 //@Deprecated
duke@1 67 public final PrintWriter noticeWriter;
duke@1 68
duke@1 69 /** The maximum number of errors/warnings that are reported.
duke@1 70 */
duke@1 71 public final int MaxErrors;
duke@1 72 public final int MaxWarnings;
duke@1 73
duke@1 74 /** Whether or not to display the line of source containing a diagnostic.
duke@1 75 */
duke@1 76 private final boolean showSourceLine;
duke@1 77
duke@1 78 /** Switch: prompt user on each error.
duke@1 79 */
duke@1 80 public boolean promptOnError;
duke@1 81
duke@1 82 /** Switch: emit warning messages.
duke@1 83 */
duke@1 84 public boolean emitWarnings;
duke@1 85
duke@1 86 /** Enforce mandatory warnings.
duke@1 87 */
duke@1 88 private boolean enforceMandatoryWarnings;
duke@1 89
duke@1 90 /** Print stack trace on errors?
duke@1 91 */
duke@1 92 public boolean dumpOnError;
duke@1 93
duke@1 94 /** Print multiple errors for same source locations.
duke@1 95 */
duke@1 96 public boolean multipleErrors;
duke@1 97
duke@1 98 /**
duke@1 99 * Diagnostic listener, if provided through programmatic
duke@1 100 * interface to javac (JSR 199).
duke@1 101 */
duke@1 102 protected DiagnosticListener<? super JavaFileObject> diagListener;
duke@1 103 /**
duke@1 104 * Formatter for diagnostics
duke@1 105 */
duke@1 106 private DiagnosticFormatter diagFormatter;
duke@1 107
duke@1 108 /**
duke@1 109 * Factory for diagnostics
duke@1 110 */
duke@1 111 private JCDiagnostic.Factory diags;
duke@1 112
duke@1 113
duke@1 114 /** Construct a log with given I/O redirections.
duke@1 115 */
duke@1 116 @Deprecated
duke@1 117 protected Log(Context context, PrintWriter errWriter, PrintWriter warnWriter, PrintWriter noticeWriter) {
duke@1 118 context.put(logKey, this);
duke@1 119 this.errWriter = errWriter;
duke@1 120 this.warnWriter = warnWriter;
duke@1 121 this.noticeWriter = noticeWriter;
duke@1 122
duke@1 123 this.diags = JCDiagnostic.Factory.instance(context);
duke@1 124
duke@1 125 Options options = Options.instance(context);
duke@1 126 this.dumpOnError = options.get("-doe") != null;
duke@1 127 this.promptOnError = options.get("-prompt") != null;
duke@1 128 this.emitWarnings = options.get("-Xlint:none") == null;
duke@1 129 this.MaxErrors = getIntOption(options, "-Xmaxerrs", 100);
duke@1 130 this.MaxWarnings = getIntOption(options, "-Xmaxwarns", 100);
duke@1 131 this.showSourceLine = options.get("rawDiagnostics") == null;
duke@1 132
duke@1 133 this.diagFormatter = DiagnosticFormatter.instance(context);
duke@1 134 @SuppressWarnings("unchecked") // FIXME
duke@1 135 DiagnosticListener<? super JavaFileObject> diagListener =
duke@1 136 context.get(DiagnosticListener.class);
duke@1 137 this.diagListener = diagListener;
duke@1 138
duke@1 139 Source source = Source.instance(context);
duke@1 140 this.enforceMandatoryWarnings = source.enforceMandatoryWarnings();
duke@1 141 }
duke@1 142 // where
duke@1 143 private int getIntOption(Options options, String optionName, int defaultValue) {
duke@1 144 String s = options.get(optionName);
duke@1 145 try {
duke@1 146 if (s != null) return Integer.parseInt(s);
duke@1 147 } catch (NumberFormatException e) {
duke@1 148 // silently ignore ill-formed numbers
duke@1 149 }
duke@1 150 return defaultValue;
duke@1 151 }
duke@1 152
duke@1 153 /** The default writer for diagnostics
duke@1 154 */
duke@1 155 static final PrintWriter defaultWriter(Context context) {
duke@1 156 PrintWriter result = context.get(outKey);
duke@1 157 if (result == null)
duke@1 158 context.put(outKey, result = new PrintWriter(System.err));
duke@1 159 return result;
duke@1 160 }
duke@1 161
duke@1 162 /** Construct a log with default settings.
duke@1 163 */
duke@1 164 protected Log(Context context) {
duke@1 165 this(context, defaultWriter(context));
duke@1 166 }
duke@1 167
duke@1 168 /** Construct a log with all output redirected.
duke@1 169 */
duke@1 170 protected Log(Context context, PrintWriter defaultWriter) {
duke@1 171 this(context, defaultWriter, defaultWriter, defaultWriter);
duke@1 172 }
duke@1 173
duke@1 174 /** Get the Log instance for this context. */
duke@1 175 public static Log instance(Context context) {
duke@1 176 Log instance = context.get(logKey);
duke@1 177 if (instance == null)
duke@1 178 instance = new Log(context);
duke@1 179 return instance;
duke@1 180 }
duke@1 181
duke@1 182 /** The file that's currently translated.
duke@1 183 */
duke@1 184 protected JCDiagnostic.DiagnosticSource source;
duke@1 185
duke@1 186 /** The number of errors encountered so far.
duke@1 187 */
duke@1 188 public int nerrors = 0;
duke@1 189
duke@1 190 /** The number of warnings encountered so far.
duke@1 191 */
duke@1 192 public int nwarnings = 0;
duke@1 193
duke@1 194 /** A set of all errors generated so far. This is used to avoid printing an
duke@1 195 * error message more than once. For each error, a pair consisting of the
duke@1 196 * source file name and source code position of the error is added to the set.
duke@1 197 */
duke@1 198 private Set<Pair<JavaFileObject, Integer>> recorded = new HashSet<Pair<JavaFileObject,Integer>>();
duke@1 199
duke@1 200 private Map<JavaFileObject, Map<JCTree, Integer>> endPosTables;
duke@1 201
duke@1 202 /** The buffer containing the file that's currently translated.
duke@1 203 */
duke@1 204 private char[] buf = null;
duke@1 205
duke@1 206 /** The position in the buffer at which last error was reported
duke@1 207 */
duke@1 208 private int bp;
duke@1 209
duke@1 210 /** number of the current source line; first line is 1
duke@1 211 */
duke@1 212 private int line;
duke@1 213
duke@1 214 /** buffer index of the first character of the current source line
duke@1 215 */
duke@1 216 private int lineStart;
duke@1 217
duke@1 218 public boolean hasDiagnosticListener() {
duke@1 219 return diagListener != null;
duke@1 220 }
duke@1 221
duke@1 222 public void setEndPosTable(JavaFileObject name, Map<JCTree, Integer> table) {
duke@1 223 if (endPosTables == null)
duke@1 224 endPosTables = new HashMap<JavaFileObject, Map<JCTree, Integer>>();
duke@1 225 endPosTables.put(name, table);
duke@1 226 }
duke@1 227
duke@1 228 /** Re-assign source, returning previous setting.
duke@1 229 */
duke@1 230 public JavaFileObject useSource(final JavaFileObject name) {
duke@1 231 JavaFileObject prev = currentSource();
duke@1 232 if (name != prev) {
duke@1 233 source = new JCDiagnostic.DiagnosticSource() {
duke@1 234 public JavaFileObject getFile() {
duke@1 235 return name;
duke@1 236 }
duke@1 237 public CharSequence getName() {
duke@1 238 return JavacFileManager.getJavacBaseFileName(getFile());
duke@1 239 }
duke@1 240 public int getLineNumber(int pos) {
duke@1 241 return Log.this.getLineNumber(pos);
duke@1 242 }
duke@1 243 public int getColumnNumber(int pos) {
duke@1 244 return Log.this.getColumnNumber(pos);
duke@1 245 }
duke@1 246 public Map<JCTree, Integer> getEndPosTable() {
duke@1 247 return (endPosTables == null ? null : endPosTables.get(name));
duke@1 248 }
duke@1 249 };
duke@1 250 buf = null;
duke@1 251 }
duke@1 252 return prev;
duke@1 253 }
duke@1 254
duke@1 255 /** Re-assign source buffer for existing source name.
duke@1 256 */
duke@1 257 protected void setBuf(char[] newBuf) {
duke@1 258 buf = newBuf;
duke@1 259 bp = 0;
duke@1 260 lineStart = 0;
duke@1 261 line = 1;
duke@1 262 }
duke@1 263
duke@1 264 protected char[] getBuf() {
duke@1 265 return buf;
duke@1 266 }
duke@1 267
duke@1 268 /** Return current source name.
duke@1 269 */
duke@1 270 public JavaFileObject currentSource() {
duke@1 271 return source == null ? null : source.getFile();
duke@1 272 }
duke@1 273
duke@1 274 /** Flush the logs
duke@1 275 */
duke@1 276 public void flush() {
duke@1 277 errWriter.flush();
duke@1 278 warnWriter.flush();
duke@1 279 noticeWriter.flush();
duke@1 280 }
duke@1 281
duke@1 282 /** Returns true if an error needs to be reported for a given
duke@1 283 * source name and pos.
duke@1 284 */
duke@1 285 protected boolean shouldReport(JavaFileObject file, int pos) {
duke@1 286 if (multipleErrors || file == null)
duke@1 287 return true;
duke@1 288
duke@1 289 Pair<JavaFileObject,Integer> coords = new Pair<JavaFileObject,Integer>(file, pos);
duke@1 290 boolean shouldReport = !recorded.contains(coords);
duke@1 291 if (shouldReport)
duke@1 292 recorded.add(coords);
duke@1 293 return shouldReport;
duke@1 294 }
duke@1 295
duke@1 296 /** Prompt user after an error.
duke@1 297 */
duke@1 298 public void prompt() {
duke@1 299 if (promptOnError) {
duke@1 300 System.err.println(getLocalizedString("resume.abort"));
duke@1 301 char ch;
duke@1 302 try {
duke@1 303 while (true) {
duke@1 304 switch (System.in.read()) {
duke@1 305 case 'a': case 'A':
duke@1 306 System.exit(-1);
duke@1 307 return;
duke@1 308 case 'r': case 'R':
duke@1 309 return;
duke@1 310 case 'x': case 'X':
duke@1 311 throw new AssertionError("user abort");
duke@1 312 default:
duke@1 313 }
duke@1 314 }
duke@1 315 } catch (IOException e) {}
duke@1 316 }
duke@1 317 }
duke@1 318
duke@1 319 /** Print the faulty source code line and point to the error.
duke@1 320 * @param pos Buffer index of the error position, must be on current line
duke@1 321 */
duke@1 322 private void printErrLine(int pos, PrintWriter writer) {
duke@1 323 if (!findLine(pos))
duke@1 324 return;
duke@1 325
duke@1 326 int lineEnd = lineStart;
duke@1 327 while (lineEnd < buf.length && buf[lineEnd] != CR && buf[lineEnd] != LF)
duke@1 328 lineEnd++;
duke@1 329 if (lineEnd - lineStart == 0)
duke@1 330 return;
duke@1 331 printLines(writer, new String(buf, lineStart, lineEnd - lineStart));
duke@1 332 for (bp = lineStart; bp < pos; bp++) {
duke@1 333 writer.print((buf[bp] == '\t') ? "\t" : " ");
duke@1 334 }
duke@1 335 writer.println("^");
duke@1 336 writer.flush();
duke@1 337 }
duke@1 338
duke@1 339 protected static char[] getCharContent(JavaFileObject fileObject) throws IOException {
duke@1 340 CharSequence cs = fileObject.getCharContent(true);
duke@1 341 if (cs instanceof CharBuffer) {
duke@1 342 return JavacFileManager.toArray((CharBuffer)cs);
duke@1 343 } else {
duke@1 344 return cs.toString().toCharArray();
duke@1 345 }
duke@1 346 }
duke@1 347
duke@1 348 /** Find the line in the buffer that contains the current position
duke@1 349 * @param pos Character offset into the buffer
duke@1 350 */
duke@1 351 private boolean findLine(int pos) {
duke@1 352 if (pos == Position.NOPOS || currentSource() == null)
duke@1 353 return false;
duke@1 354 try {
duke@1 355 if (buf == null) {
duke@1 356 buf = getCharContent(currentSource());
duke@1 357 lineStart = 0;
duke@1 358 line = 1;
duke@1 359 } else if (lineStart > pos) { // messages don't come in order
duke@1 360 lineStart = 0;
duke@1 361 line = 1;
duke@1 362 }
duke@1 363 bp = lineStart;
duke@1 364 while (bp < buf.length && bp < pos) {
duke@1 365 switch (buf[bp++]) {
duke@1 366 case CR:
duke@1 367 if (bp < buf.length && buf[bp] == LF) bp++;
duke@1 368 line++;
duke@1 369 lineStart = bp;
duke@1 370 break;
duke@1 371 case LF:
duke@1 372 line++;
duke@1 373 lineStart = bp;
duke@1 374 break;
duke@1 375 }
duke@1 376 }
duke@1 377 return bp <= buf.length;
duke@1 378 } catch (IOException e) {
duke@1 379 //e.printStackTrace();
duke@1 380 // FIXME: include e.getLocalizedMessage() in error message
duke@1 381 printLines(errWriter, getLocalizedString("source.unavailable"));
duke@1 382 errWriter.flush();
duke@1 383 buf = new char[0];
duke@1 384 }
duke@1 385 return false;
duke@1 386 }
duke@1 387
duke@1 388 /** Print the text of a message, translating newlines appropriately
duke@1 389 * for the platform.
duke@1 390 */
duke@1 391 public static void printLines(PrintWriter writer, String msg) {
duke@1 392 int nl;
duke@1 393 while ((nl = msg.indexOf('\n')) != -1) {
duke@1 394 writer.println(msg.substring(0, nl));
duke@1 395 msg = msg.substring(nl+1);
duke@1 396 }
duke@1 397 if (msg.length() != 0) writer.println(msg);
duke@1 398 }
duke@1 399
duke@1 400 /** Report an error, unless another error was already reported at same
duke@1 401 * source position.
duke@1 402 * @param key The key for the localized error message.
duke@1 403 * @param args Fields of the error message.
duke@1 404 */
duke@1 405 public void error(String key, Object ... args) {
duke@1 406 report(diags.error(source, null, key, args));
duke@1 407 }
duke@1 408
duke@1 409 /** Report an error, unless another error was already reported at same
duke@1 410 * source position.
duke@1 411 * @param pos The source position at which to report the error.
duke@1 412 * @param key The key for the localized error message.
duke@1 413 * @param args Fields of the error message.
duke@1 414 */
duke@1 415 public void error(DiagnosticPosition pos, String key, Object ... args) {
duke@1 416 report(diags.error(source, pos, key, args));
duke@1 417 }
duke@1 418
duke@1 419 /** Report an error, unless another error was already reported at same
duke@1 420 * source position.
duke@1 421 * @param pos The source position at which to report the error.
duke@1 422 * @param key The key for the localized error message.
duke@1 423 * @param args Fields of the error message.
duke@1 424 */
duke@1 425 public void error(int pos, String key, Object ... args) {
duke@1 426 report(diags.error(source, wrap(pos), key, args));
duke@1 427 }
duke@1 428
duke@1 429 /** Report a warning, unless suppressed by the -nowarn option or the
duke@1 430 * maximum number of warnings has been reached.
duke@1 431 * @param pos The source position at which to report the warning.
duke@1 432 * @param key The key for the localized warning message.
duke@1 433 * @param args Fields of the warning message.
duke@1 434 */
duke@1 435 public void warning(String key, Object ... args) {
duke@1 436 report(diags.warning(source, null, key, args));
duke@1 437 }
duke@1 438
duke@1 439 /** Report a warning, unless suppressed by the -nowarn option or the
duke@1 440 * maximum number of warnings has been reached.
duke@1 441 * @param pos The source position at which to report the warning.
duke@1 442 * @param key The key for the localized warning message.
duke@1 443 * @param args Fields of the warning message.
duke@1 444 */
duke@1 445 public void warning(DiagnosticPosition pos, String key, Object ... args) {
duke@1 446 report(diags.warning(source, pos, key, args));
duke@1 447 }
duke@1 448
duke@1 449 /** Report a warning, unless suppressed by the -nowarn option or the
duke@1 450 * maximum number of warnings has been reached.
duke@1 451 * @param pos The source position at which to report the warning.
duke@1 452 * @param key The key for the localized warning message.
duke@1 453 * @param args Fields of the warning message.
duke@1 454 */
duke@1 455 public void warning(int pos, String key, Object ... args) {
duke@1 456 report(diags.warning(source, wrap(pos), key, args));
duke@1 457 }
duke@1 458
duke@1 459 /** Report a warning.
duke@1 460 * @param pos The source position at which to report the warning.
duke@1 461 * @param key The key for the localized warning message.
duke@1 462 * @param args Fields of the warning message.
duke@1 463 */
duke@1 464 public void mandatoryWarning(DiagnosticPosition pos, String key, Object ... args) {
duke@1 465 if (enforceMandatoryWarnings)
duke@1 466 report(diags.mandatoryWarning(source, pos, key, args));
duke@1 467 else
duke@1 468 report(diags.warning(source, pos, key, args));
duke@1 469 }
duke@1 470
duke@1 471 /** Report a warning that cannot be suppressed.
duke@1 472 * @param pos The source position at which to report the warning.
duke@1 473 * @param key The key for the localized warning message.
duke@1 474 * @param args Fields of the warning message.
duke@1 475 */
duke@1 476 public void strictWarning(DiagnosticPosition pos, String key, Object ... args) {
duke@1 477 writeDiagnostic(diags.warning(source, pos, key, args));
duke@1 478 nwarnings++;
duke@1 479 }
duke@1 480
duke@1 481 /** Provide a non-fatal notification, unless suppressed by the -nowarn option.
duke@1 482 * @param key The key for the localized notification message.
duke@1 483 * @param args Fields of the notification message.
duke@1 484 */
duke@1 485 public void note(String key, Object ... args) {
duke@1 486 report(diags.note(source, null, key, args));
duke@1 487 }
duke@1 488
duke@1 489 /** Provide a non-fatal notification, unless suppressed by the -nowarn option.
duke@1 490 * @param key The key for the localized notification message.
duke@1 491 * @param args Fields of the notification message.
duke@1 492 */
duke@1 493 public void note(DiagnosticPosition pos, String key, Object ... args) {
duke@1 494 report(diags.note(source, pos, key, args));
duke@1 495 }
duke@1 496
duke@1 497 /** Provide a non-fatal notification, unless suppressed by the -nowarn option.
duke@1 498 * @param key The key for the localized notification message.
duke@1 499 * @param args Fields of the notification message.
duke@1 500 */
duke@1 501 public void note(int pos, String key, Object ... args) {
duke@1 502 report(diags.note(source, wrap(pos), key, args));
duke@1 503 }
duke@1 504
duke@1 505 /** Provide a non-fatal notification, unless suppressed by the -nowarn option.
duke@1 506 * @param key The key for the localized notification message.
duke@1 507 * @param args Fields of the notification message.
duke@1 508 */
duke@1 509 public void mandatoryNote(final JavaFileObject file, String key, Object ... args) {
duke@1 510 JCDiagnostic.DiagnosticSource wrapper = null;
duke@1 511 if (file != null) {
duke@1 512 wrapper = new JCDiagnostic.DiagnosticSource() {
duke@1 513 public JavaFileObject getFile() {
duke@1 514 return file;
duke@1 515 }
duke@1 516 public CharSequence getName() {
duke@1 517 return JavacFileManager.getJavacBaseFileName(getFile());
duke@1 518 }
duke@1 519 public int getLineNumber(int pos) {
duke@1 520 return Log.this.getLineNumber(pos);
duke@1 521 }
duke@1 522 public int getColumnNumber(int pos) {
duke@1 523 return Log.this.getColumnNumber(pos);
duke@1 524 }
duke@1 525 public Map<JCTree, Integer> getEndPosTable() {
duke@1 526 return (endPosTables == null ? null : endPosTables.get(file));
duke@1 527 }
duke@1 528 };
duke@1 529 }
duke@1 530 if (enforceMandatoryWarnings)
duke@1 531 report(diags.mandatoryNote(wrapper, key, args));
duke@1 532 else
duke@1 533 report(diags.note(wrapper, null, key, args));
duke@1 534 }
duke@1 535
duke@1 536 private DiagnosticPosition wrap(int pos) {
duke@1 537 return (pos == Position.NOPOS ? null : new SimpleDiagnosticPosition(pos));
duke@1 538 }
duke@1 539
duke@1 540 /**
duke@1 541 * Common diagnostic handling.
duke@1 542 * The diagnostic is counted, and depending on the options and how many diagnostics have been
duke@1 543 * reported so far, the diagnostic may be handed off to writeDiagnostic.
duke@1 544 */
duke@1 545 public void report(JCDiagnostic diagnostic) {
duke@1 546 switch (diagnostic.getType()) {
duke@1 547 case FRAGMENT:
duke@1 548 throw new IllegalArgumentException();
duke@1 549
duke@1 550 case NOTE:
duke@1 551 // Print out notes only when we are permitted to report warnings
duke@1 552 // Notes are only generated at the end of a compilation, so should be small
duke@1 553 // in number.
duke@1 554 if (emitWarnings || diagnostic.isMandatory()) {
duke@1 555 writeDiagnostic(diagnostic);
duke@1 556 }
duke@1 557 break;
duke@1 558
duke@1 559 case WARNING:
duke@1 560 if (emitWarnings || diagnostic.isMandatory()) {
duke@1 561 if (nwarnings < MaxWarnings) {
duke@1 562 writeDiagnostic(diagnostic);
duke@1 563 nwarnings++;
duke@1 564 }
duke@1 565 }
duke@1 566 break;
duke@1 567
duke@1 568 case ERROR:
duke@1 569 if (nerrors < MaxErrors
duke@1 570 && shouldReport(diagnostic.getSource(), diagnostic.getIntPosition())) {
duke@1 571 writeDiagnostic(diagnostic);
duke@1 572 nerrors++;
duke@1 573 }
duke@1 574 break;
duke@1 575 }
duke@1 576 }
duke@1 577
duke@1 578 /**
duke@1 579 * Write out a diagnostic.
duke@1 580 */
duke@1 581 protected void writeDiagnostic(JCDiagnostic diag) {
duke@1 582 if (diagListener != null) {
duke@1 583 try {
duke@1 584 diagListener.report(diag);
duke@1 585 return;
duke@1 586 }
duke@1 587 catch (Throwable t) {
duke@1 588 throw new ClientCodeException(t);
duke@1 589 }
duke@1 590 }
duke@1 591
duke@1 592 PrintWriter writer = getWriterForDiagnosticType(diag.getType());
duke@1 593
duke@1 594 printLines(writer, diagFormatter.format(diag));
duke@1 595 if (showSourceLine) {
duke@1 596 int pos = diag.getIntPosition();
duke@1 597 if (pos != Position.NOPOS) {
duke@1 598 JavaFileObject prev = useSource(diag.getSource());
duke@1 599 printErrLine(pos, writer);
duke@1 600 useSource(prev);
duke@1 601 }
duke@1 602 }
duke@1 603
duke@1 604 if (promptOnError) {
duke@1 605 switch (diag.getType()) {
duke@1 606 case ERROR:
duke@1 607 case WARNING:
duke@1 608 prompt();
duke@1 609 }
duke@1 610 }
duke@1 611
duke@1 612 if (dumpOnError)
duke@1 613 new RuntimeException().printStackTrace(writer);
duke@1 614
duke@1 615 writer.flush();
duke@1 616 }
duke@1 617
duke@1 618 @Deprecated
duke@1 619 protected PrintWriter getWriterForDiagnosticType(DiagnosticType dt) {
duke@1 620 switch (dt) {
duke@1 621 case FRAGMENT:
duke@1 622 throw new IllegalArgumentException();
duke@1 623
duke@1 624 case NOTE:
duke@1 625 return noticeWriter;
duke@1 626
duke@1 627 case WARNING:
duke@1 628 return warnWriter;
duke@1 629
duke@1 630 case ERROR:
duke@1 631 return errWriter;
duke@1 632
duke@1 633 default:
duke@1 634 throw new Error();
duke@1 635 }
duke@1 636 }
duke@1 637
duke@1 638 /** Find a localized string in the resource bundle.
duke@1 639 * @param key The key for the localized string.
duke@1 640 * @param args Fields to substitute into the string.
duke@1 641 */
duke@1 642 public static String getLocalizedString(String key, Object ... args) {
duke@1 643 return Messages.getDefaultLocalizedString("compiler.misc." + key, args);
duke@1 644 }
duke@1 645
duke@1 646 /***************************************************************************
duke@1 647 * raw error messages without internationalization; used for experimentation
duke@1 648 * and quick prototyping
duke@1 649 ***************************************************************************/
duke@1 650
duke@1 651 /** print an error or warning message:
duke@1 652 */
duke@1 653 private void printRawError(int pos, String msg) {
duke@1 654 if (!findLine(pos)) {
duke@1 655 printLines(errWriter, "error: " + msg);
duke@1 656 } else {
duke@1 657 JavaFileObject file = currentSource();
duke@1 658 if (file != null)
duke@1 659 printLines(errWriter,
duke@1 660 JavacFileManager.getJavacFileName(file) + ":" +
duke@1 661 line + ": " + msg);
duke@1 662 printErrLine(pos, errWriter);
duke@1 663 }
duke@1 664 errWriter.flush();
duke@1 665 }
duke@1 666
duke@1 667 /** report an error:
duke@1 668 */
duke@1 669 public void rawError(int pos, String msg) {
duke@1 670 if (nerrors < MaxErrors && shouldReport(currentSource(), pos)) {
duke@1 671 printRawError(pos, msg);
duke@1 672 prompt();
duke@1 673 nerrors++;
duke@1 674 }
duke@1 675 errWriter.flush();
duke@1 676 }
duke@1 677
duke@1 678 /** report a warning:
duke@1 679 */
duke@1 680 public void rawWarning(int pos, String msg) {
duke@1 681 if (nwarnings < MaxWarnings && emitWarnings) {
duke@1 682 printRawError(pos, "warning: " + msg);
duke@1 683 }
duke@1 684 prompt();
duke@1 685 nwarnings++;
duke@1 686 errWriter.flush();
duke@1 687 }
duke@1 688
duke@1 689 /** Return the one-based line number associated with a given pos
duke@1 690 * for the current source file. Zero is returned if no line exists
duke@1 691 * for the given position.
duke@1 692 */
duke@1 693 protected int getLineNumber(int pos) {
duke@1 694 if (findLine(pos))
duke@1 695 return line;
duke@1 696 return 0;
duke@1 697 }
duke@1 698
duke@1 699 /** Return the one-based column number associated with a given pos
duke@1 700 * for the current source file. Zero is returned if no column exists
duke@1 701 * for the given position.
duke@1 702 */
duke@1 703 protected int getColumnNumber(int pos) {
duke@1 704 if (findLine(pos)) {
duke@1 705 int column = 0;
duke@1 706 for (bp = lineStart; bp < pos; bp++) {
duke@1 707 if (bp >= buf.length)
duke@1 708 return 0;
duke@1 709 if (buf[bp] == '\t')
duke@1 710 column = (column / TabInc * TabInc) + TabInc;
duke@1 711 else
duke@1 712 column++;
duke@1 713 }
duke@1 714 return column + 1; // positions are one-based
duke@1 715 }
duke@1 716 return 0;
duke@1 717 }
duke@1 718
duke@1 719 public static String format(String fmt, Object... args) {
duke@1 720 return String.format((java.util.Locale)null, fmt, args);
duke@1 721 }
duke@1 722
duke@1 723 }

mercurial