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