Tue, 28 Dec 2010 15:54:52 -0800
6962318: Update copyright year
Reviewed-by: xdono
duke@1 | 1 | /* |
ohair@798 | 2 | * Copyright (c) 1999, 2010, Oracle and/or its affiliates. All rights reserved. |
duke@1 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
duke@1 | 4 | * |
duke@1 | 5 | * This code is free software; you can redistribute it and/or modify it |
duke@1 | 6 | * under the terms of the GNU General Public License version 2 only, as |
ohair@554 | 7 | * published by the Free Software Foundation. Oracle designates this |
duke@1 | 8 | * particular file as subject to the "Classpath" exception as provided |
ohair@554 | 9 | * by Oracle in the LICENSE file that accompanied this code. |
duke@1 | 10 | * |
duke@1 | 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
duke@1 | 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
duke@1 | 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
duke@1 | 14 | * version 2 for more details (a copy is included in the LICENSE file that |
duke@1 | 15 | * accompanied this code). |
duke@1 | 16 | * |
duke@1 | 17 | * You should have received a copy of the GNU General Public License version |
duke@1 | 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
duke@1 | 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
duke@1 | 20 | * |
ohair@554 | 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
ohair@554 | 22 | * or visit www.oracle.com if you need additional information or have any |
ohair@554 | 23 | * questions. |
duke@1 | 24 | */ |
duke@1 | 25 | |
duke@1 | 26 | package com.sun.tools.javac.util; |
duke@1 | 27 | |
duke@1 | 28 | import java.io.*; |
jjg@194 | 29 | import java.util.Arrays; |
jjg@664 | 30 | import java.util.EnumSet; |
duke@1 | 31 | import java.util.HashSet; |
duke@1 | 32 | import java.util.Map; |
jjg@664 | 33 | import java.util.Queue; |
duke@1 | 34 | import java.util.Set; |
duke@1 | 35 | import javax.tools.DiagnosticListener; |
duke@1 | 36 | import javax.tools.JavaFileObject; |
jjg@50 | 37 | |
jjg@700 | 38 | import com.sun.tools.javac.api.DiagnosticFormatter; |
jjg@700 | 39 | import com.sun.tools.javac.main.OptionName; |
duke@1 | 40 | import com.sun.tools.javac.tree.JCTree; |
duke@1 | 41 | import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition; |
duke@1 | 42 | import com.sun.tools.javac.util.JCDiagnostic.DiagnosticType; |
duke@1 | 43 | |
jjg@700 | 44 | import static com.sun.tools.javac.main.OptionName.*; |
jjg@700 | 45 | |
duke@1 | 46 | /** A class for error logs. Reports errors and warnings, and |
duke@1 | 47 | * keeps track of error numbers and positions. |
duke@1 | 48 | * |
jjg@581 | 49 | * <p><b>This is NOT part of any supported API. |
jjg@581 | 50 | * If you write code that depends on this, you do so at your own risk. |
duke@1 | 51 | * This code and its internal interfaces are subject to change or |
duke@1 | 52 | * deletion without notice.</b> |
duke@1 | 53 | */ |
jjg@73 | 54 | public class Log extends AbstractLog { |
duke@1 | 55 | /** The context key for the log. */ |
duke@1 | 56 | public static final Context.Key<Log> logKey |
duke@1 | 57 | = new Context.Key<Log>(); |
duke@1 | 58 | |
duke@1 | 59 | /** The context key for the output PrintWriter. */ |
duke@1 | 60 | public static final Context.Key<PrintWriter> outKey = |
duke@1 | 61 | new Context.Key<PrintWriter>(); |
duke@1 | 62 | |
duke@1 | 63 | //@Deprecated |
duke@1 | 64 | public final PrintWriter errWriter; |
duke@1 | 65 | |
duke@1 | 66 | //@Deprecated |
duke@1 | 67 | public final PrintWriter warnWriter; |
duke@1 | 68 | |
duke@1 | 69 | //@Deprecated |
duke@1 | 70 | public final PrintWriter noticeWriter; |
duke@1 | 71 | |
duke@1 | 72 | /** The maximum number of errors/warnings that are reported. |
duke@1 | 73 | */ |
duke@1 | 74 | public final int MaxErrors; |
duke@1 | 75 | public final int MaxWarnings; |
duke@1 | 76 | |
duke@1 | 77 | /** Switch: prompt user on each error. |
duke@1 | 78 | */ |
duke@1 | 79 | public boolean promptOnError; |
duke@1 | 80 | |
duke@1 | 81 | /** Switch: emit warning messages. |
duke@1 | 82 | */ |
duke@1 | 83 | public boolean emitWarnings; |
duke@1 | 84 | |
jjg@376 | 85 | /** Switch: suppress note messages. |
jjg@376 | 86 | */ |
jjg@376 | 87 | public boolean suppressNotes; |
jjg@376 | 88 | |
duke@1 | 89 | /** Print stack trace on errors? |
duke@1 | 90 | */ |
duke@1 | 91 | public boolean dumpOnError; |
duke@1 | 92 | |
duke@1 | 93 | /** Print multiple errors for same source locations. |
duke@1 | 94 | */ |
duke@1 | 95 | public boolean multipleErrors; |
duke@1 | 96 | |
duke@1 | 97 | /** |
duke@1 | 98 | * Diagnostic listener, if provided through programmatic |
duke@1 | 99 | * interface to javac (JSR 199). |
duke@1 | 100 | */ |
duke@1 | 101 | protected DiagnosticListener<? super JavaFileObject> diagListener; |
jjg@73 | 102 | |
duke@1 | 103 | /** |
mcimadamore@221 | 104 | * Formatter for diagnostics. |
duke@1 | 105 | */ |
mcimadamore@83 | 106 | private DiagnosticFormatter<JCDiagnostic> diagFormatter; |
duke@1 | 107 | |
mcimadamore@136 | 108 | /** |
mcimadamore@221 | 109 | * Keys for expected diagnostics. |
jjg@194 | 110 | */ |
jjg@194 | 111 | public Set<String> expectDiagKeys; |
jjg@194 | 112 | |
jjg@194 | 113 | /** |
mcimadamore@221 | 114 | * JavacMessages object used for localization. |
mcimadamore@136 | 115 | */ |
mcimadamore@136 | 116 | private JavacMessages messages; |
mcimadamore@136 | 117 | |
jjg@664 | 118 | /** |
jjg@664 | 119 | * Deferred diagnostics |
jjg@664 | 120 | */ |
jjg@664 | 121 | public boolean deferDiagnostics; |
jjg@664 | 122 | public Queue<JCDiagnostic> deferredDiagnostics = new ListBuffer<JCDiagnostic>(); |
jjg@664 | 123 | |
duke@1 | 124 | /** Construct a log with given I/O redirections. |
duke@1 | 125 | */ |
duke@1 | 126 | @Deprecated |
duke@1 | 127 | protected Log(Context context, PrintWriter errWriter, PrintWriter warnWriter, PrintWriter noticeWriter) { |
jjg@73 | 128 | super(JCDiagnostic.Factory.instance(context)); |
duke@1 | 129 | context.put(logKey, this); |
duke@1 | 130 | this.errWriter = errWriter; |
duke@1 | 131 | this.warnWriter = warnWriter; |
duke@1 | 132 | this.noticeWriter = noticeWriter; |
duke@1 | 133 | |
duke@1 | 134 | Options options = Options.instance(context); |
jjg@700 | 135 | this.dumpOnError = options.isSet(DOE); |
jjg@700 | 136 | this.promptOnError = options.isSet(PROMPT); |
jjg@700 | 137 | this.emitWarnings = options.isUnset(XLINT_CUSTOM, "none"); |
jjg@700 | 138 | this.suppressNotes = options.isSet("suppressNotes"); |
jjg@700 | 139 | this.MaxErrors = getIntOption(options, XMAXERRS, getDefaultMaxErrors()); |
jjg@700 | 140 | this.MaxWarnings = getIntOption(options, XMAXWARNS, getDefaultMaxWarnings()); |
duke@1 | 141 | |
jjg@700 | 142 | boolean rawDiagnostics = options.isSet("rawDiagnostics"); |
mcimadamore@136 | 143 | messages = JavacMessages.instance(context); |
mcimadamore@137 | 144 | this.diagFormatter = rawDiagnostics ? new RawDiagnosticFormatter(options) : |
mcimadamore@136 | 145 | new BasicDiagnosticFormatter(options, messages); |
duke@1 | 146 | @SuppressWarnings("unchecked") // FIXME |
jjg@194 | 147 | DiagnosticListener<? super JavaFileObject> dl = |
duke@1 | 148 | context.get(DiagnosticListener.class); |
jjg@194 | 149 | this.diagListener = dl; |
jjg@194 | 150 | |
jjg@194 | 151 | String ek = options.get("expectKeys"); |
jjg@194 | 152 | if (ek != null) |
jjg@194 | 153 | expectDiagKeys = new HashSet<String>(Arrays.asList(ek.split(", *"))); |
duke@1 | 154 | } |
duke@1 | 155 | // where |
jjg@700 | 156 | private int getIntOption(Options options, OptionName optionName, int defaultValue) { |
duke@1 | 157 | String s = options.get(optionName); |
duke@1 | 158 | try { |
jjg@464 | 159 | if (s != null) { |
jjg@464 | 160 | int n = Integer.parseInt(s); |
jjg@464 | 161 | return (n <= 0 ? Integer.MAX_VALUE : n); |
jjg@464 | 162 | } |
duke@1 | 163 | } catch (NumberFormatException e) { |
duke@1 | 164 | // silently ignore ill-formed numbers |
duke@1 | 165 | } |
duke@1 | 166 | return defaultValue; |
duke@1 | 167 | } |
duke@1 | 168 | |
jjg@584 | 169 | /** Default value for -Xmaxerrs. |
jjg@584 | 170 | */ |
jjg@584 | 171 | protected int getDefaultMaxErrors() { |
jjg@584 | 172 | return 100; |
jjg@584 | 173 | } |
jjg@584 | 174 | |
jjg@584 | 175 | /** Default value for -Xmaxwarns. |
jjg@584 | 176 | */ |
jjg@584 | 177 | protected int getDefaultMaxWarnings() { |
jjg@584 | 178 | return 100; |
jjg@584 | 179 | } |
jjg@584 | 180 | |
duke@1 | 181 | /** The default writer for diagnostics |
duke@1 | 182 | */ |
duke@1 | 183 | static final PrintWriter defaultWriter(Context context) { |
duke@1 | 184 | PrintWriter result = context.get(outKey); |
duke@1 | 185 | if (result == null) |
duke@1 | 186 | context.put(outKey, result = new PrintWriter(System.err)); |
duke@1 | 187 | return result; |
duke@1 | 188 | } |
duke@1 | 189 | |
duke@1 | 190 | /** Construct a log with default settings. |
duke@1 | 191 | */ |
duke@1 | 192 | protected Log(Context context) { |
duke@1 | 193 | this(context, defaultWriter(context)); |
duke@1 | 194 | } |
duke@1 | 195 | |
duke@1 | 196 | /** Construct a log with all output redirected. |
duke@1 | 197 | */ |
duke@1 | 198 | protected Log(Context context, PrintWriter defaultWriter) { |
duke@1 | 199 | this(context, defaultWriter, defaultWriter, defaultWriter); |
duke@1 | 200 | } |
duke@1 | 201 | |
duke@1 | 202 | /** Get the Log instance for this context. */ |
duke@1 | 203 | public static Log instance(Context context) { |
duke@1 | 204 | Log instance = context.get(logKey); |
duke@1 | 205 | if (instance == null) |
duke@1 | 206 | instance = new Log(context); |
duke@1 | 207 | return instance; |
duke@1 | 208 | } |
duke@1 | 209 | |
duke@1 | 210 | /** The number of errors encountered so far. |
duke@1 | 211 | */ |
duke@1 | 212 | public int nerrors = 0; |
duke@1 | 213 | |
duke@1 | 214 | /** The number of warnings encountered so far. |
duke@1 | 215 | */ |
duke@1 | 216 | public int nwarnings = 0; |
duke@1 | 217 | |
duke@1 | 218 | /** A set of all errors generated so far. This is used to avoid printing an |
duke@1 | 219 | * error message more than once. For each error, a pair consisting of the |
duke@1 | 220 | * source file name and source code position of the error is added to the set. |
duke@1 | 221 | */ |
duke@1 | 222 | private Set<Pair<JavaFileObject, Integer>> recorded = new HashSet<Pair<JavaFileObject,Integer>>(); |
duke@1 | 223 | |
duke@1 | 224 | public boolean hasDiagnosticListener() { |
duke@1 | 225 | return diagListener != null; |
duke@1 | 226 | } |
duke@1 | 227 | |
duke@1 | 228 | public void setEndPosTable(JavaFileObject name, Map<JCTree, Integer> table) { |
jjg@73 | 229 | name.getClass(); // null check |
jjg@73 | 230 | getSource(name).setEndPosTable(table); |
duke@1 | 231 | } |
duke@1 | 232 | |
mcimadamore@168 | 233 | /** Return current sourcefile. |
duke@1 | 234 | */ |
mcimadamore@168 | 235 | public JavaFileObject currentSourceFile() { |
duke@1 | 236 | return source == null ? null : source.getFile(); |
duke@1 | 237 | } |
duke@1 | 238 | |
mcimadamore@221 | 239 | /** Get the current diagnostic formatter. |
mcimadamore@221 | 240 | */ |
mcimadamore@221 | 241 | public DiagnosticFormatter<JCDiagnostic> getDiagnosticFormatter() { |
mcimadamore@221 | 242 | return diagFormatter; |
mcimadamore@221 | 243 | } |
mcimadamore@221 | 244 | |
mcimadamore@221 | 245 | /** Set the current diagnostic formatter. |
mcimadamore@221 | 246 | */ |
mcimadamore@221 | 247 | public void setDiagnosticFormatter(DiagnosticFormatter<JCDiagnostic> diagFormatter) { |
mcimadamore@221 | 248 | this.diagFormatter = diagFormatter; |
mcimadamore@221 | 249 | } |
mcimadamore@221 | 250 | |
duke@1 | 251 | /** Flush the logs |
duke@1 | 252 | */ |
duke@1 | 253 | public void flush() { |
duke@1 | 254 | errWriter.flush(); |
duke@1 | 255 | warnWriter.flush(); |
duke@1 | 256 | noticeWriter.flush(); |
duke@1 | 257 | } |
duke@1 | 258 | |
duke@1 | 259 | /** Returns true if an error needs to be reported for a given |
duke@1 | 260 | * source name and pos. |
duke@1 | 261 | */ |
duke@1 | 262 | protected boolean shouldReport(JavaFileObject file, int pos) { |
duke@1 | 263 | if (multipleErrors || file == null) |
duke@1 | 264 | return true; |
duke@1 | 265 | |
duke@1 | 266 | Pair<JavaFileObject,Integer> coords = new Pair<JavaFileObject,Integer>(file, pos); |
duke@1 | 267 | boolean shouldReport = !recorded.contains(coords); |
duke@1 | 268 | if (shouldReport) |
duke@1 | 269 | recorded.add(coords); |
duke@1 | 270 | return shouldReport; |
duke@1 | 271 | } |
duke@1 | 272 | |
duke@1 | 273 | /** Prompt user after an error. |
duke@1 | 274 | */ |
duke@1 | 275 | public void prompt() { |
duke@1 | 276 | if (promptOnError) { |
jjg@604 | 277 | System.err.println(localize("resume.abort")); |
duke@1 | 278 | char ch; |
duke@1 | 279 | try { |
duke@1 | 280 | while (true) { |
duke@1 | 281 | switch (System.in.read()) { |
duke@1 | 282 | case 'a': case 'A': |
duke@1 | 283 | System.exit(-1); |
duke@1 | 284 | return; |
duke@1 | 285 | case 'r': case 'R': |
duke@1 | 286 | return; |
duke@1 | 287 | case 'x': case 'X': |
duke@1 | 288 | throw new AssertionError("user abort"); |
duke@1 | 289 | default: |
duke@1 | 290 | } |
duke@1 | 291 | } |
duke@1 | 292 | } catch (IOException e) {} |
duke@1 | 293 | } |
duke@1 | 294 | } |
duke@1 | 295 | |
duke@1 | 296 | /** Print the faulty source code line and point to the error. |
duke@1 | 297 | * @param pos Buffer index of the error position, must be on current line |
duke@1 | 298 | */ |
duke@1 | 299 | private void printErrLine(int pos, PrintWriter writer) { |
jjg@73 | 300 | String line = (source == null ? null : source.getLine(pos)); |
jjg@73 | 301 | if (line == null) |
duke@1 | 302 | return; |
mcimadamore@100 | 303 | int col = source.getColumnNumber(pos, false); |
duke@1 | 304 | |
jjg@73 | 305 | printLines(writer, line); |
jjg@73 | 306 | for (int i = 0; i < col - 1; i++) { |
jjg@73 | 307 | writer.print((line.charAt(i) == '\t') ? "\t" : " "); |
duke@1 | 308 | } |
duke@1 | 309 | writer.println("^"); |
duke@1 | 310 | writer.flush(); |
duke@1 | 311 | } |
duke@1 | 312 | |
duke@1 | 313 | /** Print the text of a message, translating newlines appropriately |
duke@1 | 314 | * for the platform. |
duke@1 | 315 | */ |
duke@1 | 316 | public static void printLines(PrintWriter writer, String msg) { |
duke@1 | 317 | int nl; |
duke@1 | 318 | while ((nl = msg.indexOf('\n')) != -1) { |
duke@1 | 319 | writer.println(msg.substring(0, nl)); |
duke@1 | 320 | msg = msg.substring(nl+1); |
duke@1 | 321 | } |
duke@1 | 322 | if (msg.length() != 0) writer.println(msg); |
duke@1 | 323 | } |
duke@1 | 324 | |
jjg@604 | 325 | /** Print the text of a message to the errWriter stream, |
jjg@604 | 326 | * translating newlines appropriately for the platform. |
jjg@604 | 327 | */ |
jjg@604 | 328 | public void printErrLines(String key, Object... args) { |
jjg@604 | 329 | printLines(errWriter, localize(key, args)); |
jjg@604 | 330 | } |
jjg@604 | 331 | |
jjg@604 | 332 | |
jjg@604 | 333 | /** Print the text of a message to the noticeWriter stream, |
jjg@604 | 334 | * translating newlines appropriately for the platform. |
jjg@604 | 335 | */ |
jjg@604 | 336 | public void printNoteLines(String key, Object... args) { |
jjg@604 | 337 | printLines(noticeWriter, localize(key, args)); |
jjg@604 | 338 | } |
jjg@604 | 339 | |
jjg@73 | 340 | protected void directError(String key, Object... args) { |
jjg@604 | 341 | printErrLines(key, args); |
jjg@73 | 342 | errWriter.flush(); |
duke@1 | 343 | } |
duke@1 | 344 | |
duke@1 | 345 | /** Report a warning that cannot be suppressed. |
duke@1 | 346 | * @param pos The source position at which to report the warning. |
duke@1 | 347 | * @param key The key for the localized warning message. |
duke@1 | 348 | * @param args Fields of the warning message. |
duke@1 | 349 | */ |
duke@1 | 350 | public void strictWarning(DiagnosticPosition pos, String key, Object ... args) { |
duke@1 | 351 | writeDiagnostic(diags.warning(source, pos, key, args)); |
duke@1 | 352 | nwarnings++; |
duke@1 | 353 | } |
duke@1 | 354 | |
jjg@664 | 355 | /** Report all deferred diagnostics, and clear the deferDiagnostics flag. */ |
jjg@664 | 356 | public void reportDeferredDiagnostics() { |
jjg@664 | 357 | reportDeferredDiagnostics(EnumSet.allOf(JCDiagnostic.Kind.class)); |
jjg@664 | 358 | } |
jjg@664 | 359 | |
jjg@664 | 360 | /** Report selected deferred diagnostics, and clear the deferDiagnostics flag. */ |
jjg@664 | 361 | public void reportDeferredDiagnostics(Set<JCDiagnostic.Kind> kinds) { |
jjg@664 | 362 | deferDiagnostics = false; |
jjg@664 | 363 | JCDiagnostic d; |
jjg@664 | 364 | while ((d = deferredDiagnostics.poll()) != null) { |
jjg@664 | 365 | if (kinds.contains(d.getKind())) |
jjg@664 | 366 | report(d); |
jjg@664 | 367 | } |
jjg@664 | 368 | } |
jjg@664 | 369 | |
duke@1 | 370 | /** |
duke@1 | 371 | * Common diagnostic handling. |
duke@1 | 372 | * The diagnostic is counted, and depending on the options and how many diagnostics have been |
duke@1 | 373 | * reported so far, the diagnostic may be handed off to writeDiagnostic. |
duke@1 | 374 | */ |
duke@1 | 375 | public void report(JCDiagnostic diagnostic) { |
jjg@664 | 376 | if (deferDiagnostics) { |
jjg@664 | 377 | deferredDiagnostics.add(diagnostic); |
jjg@664 | 378 | return; |
jjg@664 | 379 | } |
jjg@664 | 380 | |
jjg@194 | 381 | if (expectDiagKeys != null) |
jjg@194 | 382 | expectDiagKeys.remove(diagnostic.getCode()); |
jjg@194 | 383 | |
duke@1 | 384 | switch (diagnostic.getType()) { |
duke@1 | 385 | case FRAGMENT: |
duke@1 | 386 | throw new IllegalArgumentException(); |
duke@1 | 387 | |
duke@1 | 388 | case NOTE: |
duke@1 | 389 | // Print out notes only when we are permitted to report warnings |
duke@1 | 390 | // Notes are only generated at the end of a compilation, so should be small |
duke@1 | 391 | // in number. |
jjg@376 | 392 | if ((emitWarnings || diagnostic.isMandatory()) && !suppressNotes) { |
duke@1 | 393 | writeDiagnostic(diagnostic); |
duke@1 | 394 | } |
duke@1 | 395 | break; |
duke@1 | 396 | |
duke@1 | 397 | case WARNING: |
duke@1 | 398 | if (emitWarnings || diagnostic.isMandatory()) { |
duke@1 | 399 | if (nwarnings < MaxWarnings) { |
duke@1 | 400 | writeDiagnostic(diagnostic); |
duke@1 | 401 | nwarnings++; |
duke@1 | 402 | } |
duke@1 | 403 | } |
duke@1 | 404 | break; |
duke@1 | 405 | |
duke@1 | 406 | case ERROR: |
duke@1 | 407 | if (nerrors < MaxErrors |
duke@1 | 408 | && shouldReport(diagnostic.getSource(), diagnostic.getIntPosition())) { |
duke@1 | 409 | writeDiagnostic(diagnostic); |
duke@1 | 410 | nerrors++; |
duke@1 | 411 | } |
duke@1 | 412 | break; |
duke@1 | 413 | } |
duke@1 | 414 | } |
duke@1 | 415 | |
duke@1 | 416 | /** |
duke@1 | 417 | * Write out a diagnostic. |
duke@1 | 418 | */ |
duke@1 | 419 | protected void writeDiagnostic(JCDiagnostic diag) { |
duke@1 | 420 | if (diagListener != null) { |
duke@1 | 421 | try { |
duke@1 | 422 | diagListener.report(diag); |
duke@1 | 423 | return; |
duke@1 | 424 | } |
duke@1 | 425 | catch (Throwable t) { |
duke@1 | 426 | throw new ClientCodeException(t); |
duke@1 | 427 | } |
duke@1 | 428 | } |
duke@1 | 429 | |
duke@1 | 430 | PrintWriter writer = getWriterForDiagnosticType(diag.getType()); |
duke@1 | 431 | |
mcimadamore@136 | 432 | printLines(writer, diagFormatter.format(diag, messages.getCurrentLocale())); |
duke@1 | 433 | |
duke@1 | 434 | if (promptOnError) { |
duke@1 | 435 | switch (diag.getType()) { |
duke@1 | 436 | case ERROR: |
duke@1 | 437 | case WARNING: |
duke@1 | 438 | prompt(); |
duke@1 | 439 | } |
duke@1 | 440 | } |
duke@1 | 441 | |
duke@1 | 442 | if (dumpOnError) |
duke@1 | 443 | new RuntimeException().printStackTrace(writer); |
duke@1 | 444 | |
duke@1 | 445 | writer.flush(); |
duke@1 | 446 | } |
duke@1 | 447 | |
duke@1 | 448 | @Deprecated |
duke@1 | 449 | protected PrintWriter getWriterForDiagnosticType(DiagnosticType dt) { |
duke@1 | 450 | switch (dt) { |
duke@1 | 451 | case FRAGMENT: |
duke@1 | 452 | throw new IllegalArgumentException(); |
duke@1 | 453 | |
duke@1 | 454 | case NOTE: |
duke@1 | 455 | return noticeWriter; |
duke@1 | 456 | |
duke@1 | 457 | case WARNING: |
duke@1 | 458 | return warnWriter; |
duke@1 | 459 | |
duke@1 | 460 | case ERROR: |
duke@1 | 461 | return errWriter; |
duke@1 | 462 | |
duke@1 | 463 | default: |
duke@1 | 464 | throw new Error(); |
duke@1 | 465 | } |
duke@1 | 466 | } |
duke@1 | 467 | |
duke@1 | 468 | /** Find a localized string in the resource bundle. |
jjg@604 | 469 | * Because this method is static, it ignores the locale. |
jjg@604 | 470 | * Use localize(key, args) when possible. |
duke@1 | 471 | * @param key The key for the localized string. |
duke@1 | 472 | * @param args Fields to substitute into the string. |
duke@1 | 473 | */ |
duke@1 | 474 | public static String getLocalizedString(String key, Object ... args) { |
mcimadamore@136 | 475 | return JavacMessages.getDefaultLocalizedString("compiler.misc." + key, args); |
duke@1 | 476 | } |
duke@1 | 477 | |
jjg@604 | 478 | /** Find a localized string in the resource bundle. |
jjg@604 | 479 | * @param key The key for the localized string. |
jjg@604 | 480 | * @param args Fields to substitute into the string. |
jjg@604 | 481 | */ |
jjg@604 | 482 | public String localize(String key, Object... args) { |
jjg@604 | 483 | return messages.getLocalizedString("compiler.misc." + key, args); |
jjg@604 | 484 | } |
jjg@604 | 485 | |
duke@1 | 486 | /*************************************************************************** |
duke@1 | 487 | * raw error messages without internationalization; used for experimentation |
duke@1 | 488 | * and quick prototyping |
duke@1 | 489 | ***************************************************************************/ |
duke@1 | 490 | |
jjg@73 | 491 | /** print an error or warning message: |
jjg@73 | 492 | */ |
duke@1 | 493 | private void printRawError(int pos, String msg) { |
jjg@73 | 494 | if (source == null || pos == Position.NOPOS) { |
duke@1 | 495 | printLines(errWriter, "error: " + msg); |
duke@1 | 496 | } else { |
jjg@73 | 497 | int line = source.getLineNumber(pos); |
mcimadamore@168 | 498 | JavaFileObject file = source.getFile(); |
duke@1 | 499 | if (file != null) |
duke@1 | 500 | printLines(errWriter, |
jjg@415 | 501 | file.getName() + ":" + |
duke@1 | 502 | line + ": " + msg); |
duke@1 | 503 | printErrLine(pos, errWriter); |
duke@1 | 504 | } |
duke@1 | 505 | errWriter.flush(); |
duke@1 | 506 | } |
duke@1 | 507 | |
jjg@73 | 508 | /** report an error: |
jjg@73 | 509 | */ |
duke@1 | 510 | public void rawError(int pos, String msg) { |
mcimadamore@168 | 511 | if (nerrors < MaxErrors && shouldReport(currentSourceFile(), pos)) { |
duke@1 | 512 | printRawError(pos, msg); |
duke@1 | 513 | prompt(); |
duke@1 | 514 | nerrors++; |
duke@1 | 515 | } |
duke@1 | 516 | errWriter.flush(); |
duke@1 | 517 | } |
duke@1 | 518 | |
jjg@73 | 519 | /** report a warning: |
jjg@73 | 520 | */ |
duke@1 | 521 | public void rawWarning(int pos, String msg) { |
duke@1 | 522 | if (nwarnings < MaxWarnings && emitWarnings) { |
duke@1 | 523 | printRawError(pos, "warning: " + msg); |
duke@1 | 524 | } |
duke@1 | 525 | prompt(); |
duke@1 | 526 | nwarnings++; |
duke@1 | 527 | errWriter.flush(); |
duke@1 | 528 | } |
duke@1 | 529 | |
duke@1 | 530 | public static String format(String fmt, Object... args) { |
duke@1 | 531 | return String.format((java.util.Locale)null, fmt, args); |
duke@1 | 532 | } |
duke@1 | 533 | |
duke@1 | 534 | } |