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

Thu, 06 Mar 2008 10:25:04 -0800

author
jjg
date
Thu, 06 Mar 2008 10:25:04 -0800
changeset 10
508c01999047
parent 1
9a66ca7c79fa
child 50
b9bcea8bbe24
permissions
-rw-r--r--

6668802: javac handles diagnostics for last line badly, if line not terminated by newline
Summary: use CharBuffer.limit(), not the length of the backing array
Reviewed-by: mcimadamore

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
jjg@10 206 /** The length of useful data in buf
jjg@10 207 */
jjg@10 208 private int bufLen = 0;
jjg@10 209
duke@1 210 /** The position in the buffer at which last error was reported
duke@1 211 */
duke@1 212 private int bp;
duke@1 213
duke@1 214 /** number of the current source line; first line is 1
duke@1 215 */
duke@1 216 private int line;
duke@1 217
duke@1 218 /** buffer index of the first character of the current source line
duke@1 219 */
duke@1 220 private int lineStart;
duke@1 221
duke@1 222 public boolean hasDiagnosticListener() {
duke@1 223 return diagListener != null;
duke@1 224 }
duke@1 225
duke@1 226 public void setEndPosTable(JavaFileObject name, Map<JCTree, Integer> table) {
duke@1 227 if (endPosTables == null)
duke@1 228 endPosTables = new HashMap<JavaFileObject, Map<JCTree, Integer>>();
duke@1 229 endPosTables.put(name, table);
duke@1 230 }
duke@1 231
duke@1 232 /** Re-assign source, returning previous setting.
duke@1 233 */
duke@1 234 public JavaFileObject useSource(final JavaFileObject name) {
duke@1 235 JavaFileObject prev = currentSource();
duke@1 236 if (name != prev) {
duke@1 237 source = new JCDiagnostic.DiagnosticSource() {
duke@1 238 public JavaFileObject getFile() {
duke@1 239 return name;
duke@1 240 }
duke@1 241 public CharSequence getName() {
duke@1 242 return JavacFileManager.getJavacBaseFileName(getFile());
duke@1 243 }
duke@1 244 public int getLineNumber(int pos) {
duke@1 245 return Log.this.getLineNumber(pos);
duke@1 246 }
duke@1 247 public int getColumnNumber(int pos) {
duke@1 248 return Log.this.getColumnNumber(pos);
duke@1 249 }
duke@1 250 public Map<JCTree, Integer> getEndPosTable() {
duke@1 251 return (endPosTables == null ? null : endPosTables.get(name));
duke@1 252 }
duke@1 253 };
duke@1 254 buf = null;
duke@1 255 }
duke@1 256 return prev;
duke@1 257 }
duke@1 258
duke@1 259 /** Re-assign source buffer for existing source name.
duke@1 260 */
duke@1 261 protected void setBuf(char[] newBuf) {
duke@1 262 buf = newBuf;
jjg@10 263 bufLen = buf.length;
duke@1 264 bp = 0;
duke@1 265 lineStart = 0;
duke@1 266 line = 1;
duke@1 267 }
duke@1 268
duke@1 269 protected char[] getBuf() {
duke@1 270 return buf;
duke@1 271 }
duke@1 272
duke@1 273 /** Return current source name.
duke@1 274 */
duke@1 275 public JavaFileObject currentSource() {
duke@1 276 return source == null ? null : source.getFile();
duke@1 277 }
duke@1 278
duke@1 279 /** Flush the logs
duke@1 280 */
duke@1 281 public void flush() {
duke@1 282 errWriter.flush();
duke@1 283 warnWriter.flush();
duke@1 284 noticeWriter.flush();
duke@1 285 }
duke@1 286
duke@1 287 /** Returns true if an error needs to be reported for a given
duke@1 288 * source name and pos.
duke@1 289 */
duke@1 290 protected boolean shouldReport(JavaFileObject file, int pos) {
duke@1 291 if (multipleErrors || file == null)
duke@1 292 return true;
duke@1 293
duke@1 294 Pair<JavaFileObject,Integer> coords = new Pair<JavaFileObject,Integer>(file, pos);
duke@1 295 boolean shouldReport = !recorded.contains(coords);
duke@1 296 if (shouldReport)
duke@1 297 recorded.add(coords);
duke@1 298 return shouldReport;
duke@1 299 }
duke@1 300
duke@1 301 /** Prompt user after an error.
duke@1 302 */
duke@1 303 public void prompt() {
duke@1 304 if (promptOnError) {
duke@1 305 System.err.println(getLocalizedString("resume.abort"));
duke@1 306 char ch;
duke@1 307 try {
duke@1 308 while (true) {
duke@1 309 switch (System.in.read()) {
duke@1 310 case 'a': case 'A':
duke@1 311 System.exit(-1);
duke@1 312 return;
duke@1 313 case 'r': case 'R':
duke@1 314 return;
duke@1 315 case 'x': case 'X':
duke@1 316 throw new AssertionError("user abort");
duke@1 317 default:
duke@1 318 }
duke@1 319 }
duke@1 320 } catch (IOException e) {}
duke@1 321 }
duke@1 322 }
duke@1 323
duke@1 324 /** Print the faulty source code line and point to the error.
duke@1 325 * @param pos Buffer index of the error position, must be on current line
duke@1 326 */
duke@1 327 private void printErrLine(int pos, PrintWriter writer) {
duke@1 328 if (!findLine(pos))
duke@1 329 return;
duke@1 330
duke@1 331 int lineEnd = lineStart;
jjg@10 332 while (lineEnd < bufLen && buf[lineEnd] != CR && buf[lineEnd] != LF)
duke@1 333 lineEnd++;
duke@1 334 if (lineEnd - lineStart == 0)
duke@1 335 return;
duke@1 336 printLines(writer, new String(buf, lineStart, lineEnd - lineStart));
duke@1 337 for (bp = lineStart; bp < pos; bp++) {
duke@1 338 writer.print((buf[bp] == '\t') ? "\t" : " ");
duke@1 339 }
duke@1 340 writer.println("^");
duke@1 341 writer.flush();
duke@1 342 }
duke@1 343
jjg@10 344 protected void initBuf(JavaFileObject fileObject) throws IOException {
duke@1 345 CharSequence cs = fileObject.getCharContent(true);
duke@1 346 if (cs instanceof CharBuffer) {
jjg@10 347 CharBuffer cb = (CharBuffer) cs;
jjg@10 348 buf = JavacFileManager.toArray(cb);
jjg@10 349 bufLen = cb.limit();
duke@1 350 } else {
jjg@10 351 buf = cs.toString().toCharArray();
jjg@10 352 bufLen = buf.length;
duke@1 353 }
duke@1 354 }
duke@1 355
duke@1 356 /** Find the line in the buffer that contains the current position
duke@1 357 * @param pos Character offset into the buffer
duke@1 358 */
duke@1 359 private boolean findLine(int pos) {
duke@1 360 if (pos == Position.NOPOS || currentSource() == null)
duke@1 361 return false;
duke@1 362 try {
duke@1 363 if (buf == null) {
jjg@10 364 initBuf(currentSource());
duke@1 365 lineStart = 0;
duke@1 366 line = 1;
duke@1 367 } else if (lineStart > pos) { // messages don't come in order
duke@1 368 lineStart = 0;
duke@1 369 line = 1;
duke@1 370 }
duke@1 371 bp = lineStart;
jjg@10 372 while (bp < bufLen && bp < pos) {
duke@1 373 switch (buf[bp++]) {
duke@1 374 case CR:
jjg@10 375 if (bp < bufLen && buf[bp] == LF) bp++;
duke@1 376 line++;
duke@1 377 lineStart = bp;
duke@1 378 break;
duke@1 379 case LF:
duke@1 380 line++;
duke@1 381 lineStart = bp;
duke@1 382 break;
duke@1 383 }
duke@1 384 }
jjg@10 385 return bp <= bufLen;
duke@1 386 } catch (IOException e) {
duke@1 387 //e.printStackTrace();
duke@1 388 // FIXME: include e.getLocalizedMessage() in error message
duke@1 389 printLines(errWriter, getLocalizedString("source.unavailable"));
duke@1 390 errWriter.flush();
duke@1 391 buf = new char[0];
duke@1 392 }
duke@1 393 return false;
duke@1 394 }
duke@1 395
duke@1 396 /** Print the text of a message, translating newlines appropriately
duke@1 397 * for the platform.
duke@1 398 */
duke@1 399 public static void printLines(PrintWriter writer, String msg) {
duke@1 400 int nl;
duke@1 401 while ((nl = msg.indexOf('\n')) != -1) {
duke@1 402 writer.println(msg.substring(0, nl));
duke@1 403 msg = msg.substring(nl+1);
duke@1 404 }
duke@1 405 if (msg.length() != 0) writer.println(msg);
duke@1 406 }
duke@1 407
duke@1 408 /** Report an error, unless another error was already reported at same
duke@1 409 * source position.
duke@1 410 * @param key The key for the localized error message.
duke@1 411 * @param args Fields of the error message.
duke@1 412 */
duke@1 413 public void error(String key, Object ... args) {
duke@1 414 report(diags.error(source, null, key, args));
duke@1 415 }
duke@1 416
duke@1 417 /** Report an error, unless another error was already reported at same
duke@1 418 * source position.
duke@1 419 * @param pos The source position at which to report the error.
duke@1 420 * @param key The key for the localized error message.
duke@1 421 * @param args Fields of the error message.
duke@1 422 */
duke@1 423 public void error(DiagnosticPosition pos, String key, Object ... args) {
duke@1 424 report(diags.error(source, pos, key, args));
duke@1 425 }
duke@1 426
duke@1 427 /** Report an error, unless another error was already reported at same
duke@1 428 * source position.
duke@1 429 * @param pos The source position at which to report the error.
duke@1 430 * @param key The key for the localized error message.
duke@1 431 * @param args Fields of the error message.
duke@1 432 */
duke@1 433 public void error(int pos, String key, Object ... args) {
duke@1 434 report(diags.error(source, wrap(pos), key, args));
duke@1 435 }
duke@1 436
duke@1 437 /** Report a warning, unless suppressed by the -nowarn option or the
duke@1 438 * maximum number of warnings has been reached.
duke@1 439 * @param pos The source position at which to report the warning.
duke@1 440 * @param key The key for the localized warning message.
duke@1 441 * @param args Fields of the warning message.
duke@1 442 */
duke@1 443 public void warning(String key, Object ... args) {
duke@1 444 report(diags.warning(source, null, key, args));
duke@1 445 }
duke@1 446
duke@1 447 /** Report a warning, unless suppressed by the -nowarn option or the
duke@1 448 * maximum number of warnings has been reached.
duke@1 449 * @param pos The source position at which to report the warning.
duke@1 450 * @param key The key for the localized warning message.
duke@1 451 * @param args Fields of the warning message.
duke@1 452 */
duke@1 453 public void warning(DiagnosticPosition pos, String key, Object ... args) {
duke@1 454 report(diags.warning(source, pos, key, args));
duke@1 455 }
duke@1 456
duke@1 457 /** Report a warning, unless suppressed by the -nowarn option or the
duke@1 458 * maximum number of warnings has been reached.
duke@1 459 * @param pos The source position at which to report the warning.
duke@1 460 * @param key The key for the localized warning message.
duke@1 461 * @param args Fields of the warning message.
duke@1 462 */
duke@1 463 public void warning(int pos, String key, Object ... args) {
duke@1 464 report(diags.warning(source, wrap(pos), key, args));
duke@1 465 }
duke@1 466
duke@1 467 /** Report a warning.
duke@1 468 * @param pos The source position at which to report the warning.
duke@1 469 * @param key The key for the localized warning message.
duke@1 470 * @param args Fields of the warning message.
duke@1 471 */
duke@1 472 public void mandatoryWarning(DiagnosticPosition pos, String key, Object ... args) {
duke@1 473 if (enforceMandatoryWarnings)
duke@1 474 report(diags.mandatoryWarning(source, pos, key, args));
duke@1 475 else
duke@1 476 report(diags.warning(source, pos, key, args));
duke@1 477 }
duke@1 478
duke@1 479 /** Report a warning that cannot be suppressed.
duke@1 480 * @param pos The source position at which to report the warning.
duke@1 481 * @param key The key for the localized warning message.
duke@1 482 * @param args Fields of the warning message.
duke@1 483 */
duke@1 484 public void strictWarning(DiagnosticPosition pos, String key, Object ... args) {
duke@1 485 writeDiagnostic(diags.warning(source, pos, key, args));
duke@1 486 nwarnings++;
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(String key, Object ... args) {
duke@1 494 report(diags.note(source, null, 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(DiagnosticPosition pos, String key, Object ... args) {
duke@1 502 report(diags.note(source, 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 note(int pos, String key, Object ... args) {
duke@1 510 report(diags.note(source, wrap(pos), key, args));
duke@1 511 }
duke@1 512
duke@1 513 /** Provide a non-fatal notification, unless suppressed by the -nowarn option.
duke@1 514 * @param key The key for the localized notification message.
duke@1 515 * @param args Fields of the notification message.
duke@1 516 */
duke@1 517 public void mandatoryNote(final JavaFileObject file, String key, Object ... args) {
duke@1 518 JCDiagnostic.DiagnosticSource wrapper = null;
duke@1 519 if (file != null) {
duke@1 520 wrapper = new JCDiagnostic.DiagnosticSource() {
duke@1 521 public JavaFileObject getFile() {
duke@1 522 return file;
duke@1 523 }
duke@1 524 public CharSequence getName() {
duke@1 525 return JavacFileManager.getJavacBaseFileName(getFile());
duke@1 526 }
duke@1 527 public int getLineNumber(int pos) {
duke@1 528 return Log.this.getLineNumber(pos);
duke@1 529 }
duke@1 530 public int getColumnNumber(int pos) {
duke@1 531 return Log.this.getColumnNumber(pos);
duke@1 532 }
duke@1 533 public Map<JCTree, Integer> getEndPosTable() {
duke@1 534 return (endPosTables == null ? null : endPosTables.get(file));
duke@1 535 }
duke@1 536 };
duke@1 537 }
duke@1 538 if (enforceMandatoryWarnings)
duke@1 539 report(diags.mandatoryNote(wrapper, key, args));
duke@1 540 else
duke@1 541 report(diags.note(wrapper, null, key, args));
duke@1 542 }
duke@1 543
duke@1 544 private DiagnosticPosition wrap(int pos) {
duke@1 545 return (pos == Position.NOPOS ? null : new SimpleDiagnosticPosition(pos));
duke@1 546 }
duke@1 547
duke@1 548 /**
duke@1 549 * Common diagnostic handling.
duke@1 550 * The diagnostic is counted, and depending on the options and how many diagnostics have been
duke@1 551 * reported so far, the diagnostic may be handed off to writeDiagnostic.
duke@1 552 */
duke@1 553 public void report(JCDiagnostic diagnostic) {
duke@1 554 switch (diagnostic.getType()) {
duke@1 555 case FRAGMENT:
duke@1 556 throw new IllegalArgumentException();
duke@1 557
duke@1 558 case NOTE:
duke@1 559 // Print out notes only when we are permitted to report warnings
duke@1 560 // Notes are only generated at the end of a compilation, so should be small
duke@1 561 // in number.
duke@1 562 if (emitWarnings || diagnostic.isMandatory()) {
duke@1 563 writeDiagnostic(diagnostic);
duke@1 564 }
duke@1 565 break;
duke@1 566
duke@1 567 case WARNING:
duke@1 568 if (emitWarnings || diagnostic.isMandatory()) {
duke@1 569 if (nwarnings < MaxWarnings) {
duke@1 570 writeDiagnostic(diagnostic);
duke@1 571 nwarnings++;
duke@1 572 }
duke@1 573 }
duke@1 574 break;
duke@1 575
duke@1 576 case ERROR:
duke@1 577 if (nerrors < MaxErrors
duke@1 578 && shouldReport(diagnostic.getSource(), diagnostic.getIntPosition())) {
duke@1 579 writeDiagnostic(diagnostic);
duke@1 580 nerrors++;
duke@1 581 }
duke@1 582 break;
duke@1 583 }
duke@1 584 }
duke@1 585
duke@1 586 /**
duke@1 587 * Write out a diagnostic.
duke@1 588 */
duke@1 589 protected void writeDiagnostic(JCDiagnostic diag) {
duke@1 590 if (diagListener != null) {
duke@1 591 try {
duke@1 592 diagListener.report(diag);
duke@1 593 return;
duke@1 594 }
duke@1 595 catch (Throwable t) {
duke@1 596 throw new ClientCodeException(t);
duke@1 597 }
duke@1 598 }
duke@1 599
duke@1 600 PrintWriter writer = getWriterForDiagnosticType(diag.getType());
duke@1 601
duke@1 602 printLines(writer, diagFormatter.format(diag));
duke@1 603 if (showSourceLine) {
duke@1 604 int pos = diag.getIntPosition();
duke@1 605 if (pos != Position.NOPOS) {
duke@1 606 JavaFileObject prev = useSource(diag.getSource());
duke@1 607 printErrLine(pos, writer);
duke@1 608 useSource(prev);
duke@1 609 }
duke@1 610 }
duke@1 611
duke@1 612 if (promptOnError) {
duke@1 613 switch (diag.getType()) {
duke@1 614 case ERROR:
duke@1 615 case WARNING:
duke@1 616 prompt();
duke@1 617 }
duke@1 618 }
duke@1 619
duke@1 620 if (dumpOnError)
duke@1 621 new RuntimeException().printStackTrace(writer);
duke@1 622
duke@1 623 writer.flush();
duke@1 624 }
duke@1 625
duke@1 626 @Deprecated
duke@1 627 protected PrintWriter getWriterForDiagnosticType(DiagnosticType dt) {
duke@1 628 switch (dt) {
duke@1 629 case FRAGMENT:
duke@1 630 throw new IllegalArgumentException();
duke@1 631
duke@1 632 case NOTE:
duke@1 633 return noticeWriter;
duke@1 634
duke@1 635 case WARNING:
duke@1 636 return warnWriter;
duke@1 637
duke@1 638 case ERROR:
duke@1 639 return errWriter;
duke@1 640
duke@1 641 default:
duke@1 642 throw new Error();
duke@1 643 }
duke@1 644 }
duke@1 645
duke@1 646 /** Find a localized string in the resource bundle.
duke@1 647 * @param key The key for the localized string.
duke@1 648 * @param args Fields to substitute into the string.
duke@1 649 */
duke@1 650 public static String getLocalizedString(String key, Object ... args) {
duke@1 651 return Messages.getDefaultLocalizedString("compiler.misc." + key, args);
duke@1 652 }
duke@1 653
duke@1 654 /***************************************************************************
duke@1 655 * raw error messages without internationalization; used for experimentation
duke@1 656 * and quick prototyping
duke@1 657 ***************************************************************************/
duke@1 658
duke@1 659 /** print an error or warning message:
duke@1 660 */
duke@1 661 private void printRawError(int pos, String msg) {
duke@1 662 if (!findLine(pos)) {
duke@1 663 printLines(errWriter, "error: " + msg);
duke@1 664 } else {
duke@1 665 JavaFileObject file = currentSource();
duke@1 666 if (file != null)
duke@1 667 printLines(errWriter,
duke@1 668 JavacFileManager.getJavacFileName(file) + ":" +
duke@1 669 line + ": " + msg);
duke@1 670 printErrLine(pos, errWriter);
duke@1 671 }
duke@1 672 errWriter.flush();
duke@1 673 }
duke@1 674
duke@1 675 /** report an error:
duke@1 676 */
duke@1 677 public void rawError(int pos, String msg) {
duke@1 678 if (nerrors < MaxErrors && shouldReport(currentSource(), pos)) {
duke@1 679 printRawError(pos, msg);
duke@1 680 prompt();
duke@1 681 nerrors++;
duke@1 682 }
duke@1 683 errWriter.flush();
duke@1 684 }
duke@1 685
duke@1 686 /** report a warning:
duke@1 687 */
duke@1 688 public void rawWarning(int pos, String msg) {
duke@1 689 if (nwarnings < MaxWarnings && emitWarnings) {
duke@1 690 printRawError(pos, "warning: " + msg);
duke@1 691 }
duke@1 692 prompt();
duke@1 693 nwarnings++;
duke@1 694 errWriter.flush();
duke@1 695 }
duke@1 696
duke@1 697 /** Return the one-based line number associated with a given pos
duke@1 698 * for the current source file. Zero is returned if no line exists
duke@1 699 * for the given position.
duke@1 700 */
duke@1 701 protected int getLineNumber(int pos) {
duke@1 702 if (findLine(pos))
duke@1 703 return line;
duke@1 704 return 0;
duke@1 705 }
duke@1 706
duke@1 707 /** Return the one-based column number associated with a given pos
duke@1 708 * for the current source file. Zero is returned if no column exists
duke@1 709 * for the given position.
duke@1 710 */
duke@1 711 protected int getColumnNumber(int pos) {
duke@1 712 if (findLine(pos)) {
duke@1 713 int column = 0;
duke@1 714 for (bp = lineStart; bp < pos; bp++) {
jjg@10 715 if (bp >= bufLen)
duke@1 716 return 0;
duke@1 717 if (buf[bp] == '\t')
duke@1 718 column = (column / TabInc * TabInc) + TabInc;
duke@1 719 else
duke@1 720 column++;
duke@1 721 }
duke@1 722 return column + 1; // positions are one-based
duke@1 723 }
duke@1 724 return 0;
duke@1 725 }
duke@1 726
duke@1 727 public static String format(String fmt, Object... args) {
duke@1 728 return String.format((java.util.Locale)null, fmt, args);
duke@1 729 }
duke@1 730
duke@1 731 }

mercurial