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