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

Fri, 04 Jul 2008 15:06:27 -0700

author
tbell
date
Fri, 04 Jul 2008 15:06:27 -0700
changeset 62
07c916ecfc71
parent 60
29d2485c1085
parent 54
eaf608c64fec
child 73
1cf29847eb6e
permissions
-rw-r--r--

Merge

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

mercurial