Wed, 11 Apr 2012 10:50:11 +0100
7154127: Inference cleanup: remove bound check analysis from visitors in Types.java
Summary: Remove bound checking rules from recursive subtype visitors in Types.java and replace with centralized bound-checking logic
Reviewed-by: jjg, dlsmith
duke@1 | 1 | /* |
jjg@909 | 2 | * Copyright (c) 1999, 2011, 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; |
jjg@664 | 32 | import java.util.Queue; |
duke@1 | 33 | import java.util.Set; |
duke@1 | 34 | import javax.tools.DiagnosticListener; |
duke@1 | 35 | import javax.tools.JavaFileObject; |
jjg@50 | 36 | |
jjg@700 | 37 | import com.sun.tools.javac.api.DiagnosticFormatter; |
jjg@1157 | 38 | import com.sun.tools.javac.main.Main; |
jjg@1157 | 39 | import com.sun.tools.javac.main.Option; |
ksrini@1138 | 40 | import com.sun.tools.javac.parser.EndPosTable; |
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@1157 | 44 | import static com.sun.tools.javac.main.Option.*; |
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 | |
jjg@1136 | 63 | /* TODO: Should unify this with prefix handling in JCDiagnostic.Factory. */ |
jjg@1136 | 64 | public enum PrefixKind { |
jjg@1136 | 65 | JAVAC("javac."), |
jjg@1136 | 66 | COMPILER_MISC("compiler.misc."); |
jjg@1136 | 67 | PrefixKind(String v) { |
jjg@1136 | 68 | value = v; |
jjg@1136 | 69 | } |
jjg@1136 | 70 | public String key(String k) { |
jjg@1136 | 71 | return value + k; |
jjg@1136 | 72 | } |
jjg@1136 | 73 | final String value; |
jjg@1136 | 74 | } |
jjg@1136 | 75 | |
jjg@1135 | 76 | public enum WriterKind { NOTICE, WARNING, ERROR }; |
duke@1 | 77 | |
jjg@1135 | 78 | protected PrintWriter errWriter; |
duke@1 | 79 | |
jjg@1135 | 80 | protected PrintWriter warnWriter; |
jjg@1135 | 81 | |
jjg@1135 | 82 | protected PrintWriter noticeWriter; |
duke@1 | 83 | |
duke@1 | 84 | /** The maximum number of errors/warnings that are reported. |
duke@1 | 85 | */ |
jjg@1135 | 86 | protected int MaxErrors; |
jjg@1135 | 87 | protected int MaxWarnings; |
duke@1 | 88 | |
duke@1 | 89 | /** Switch: prompt user on each error. |
duke@1 | 90 | */ |
duke@1 | 91 | public boolean promptOnError; |
duke@1 | 92 | |
duke@1 | 93 | /** Switch: emit warning messages. |
duke@1 | 94 | */ |
duke@1 | 95 | public boolean emitWarnings; |
duke@1 | 96 | |
jjg@376 | 97 | /** Switch: suppress note messages. |
jjg@376 | 98 | */ |
jjg@376 | 99 | public boolean suppressNotes; |
jjg@376 | 100 | |
duke@1 | 101 | /** Print stack trace on errors? |
duke@1 | 102 | */ |
duke@1 | 103 | public boolean dumpOnError; |
duke@1 | 104 | |
duke@1 | 105 | /** Print multiple errors for same source locations. |
duke@1 | 106 | */ |
duke@1 | 107 | public boolean multipleErrors; |
duke@1 | 108 | |
duke@1 | 109 | /** |
duke@1 | 110 | * Diagnostic listener, if provided through programmatic |
duke@1 | 111 | * interface to javac (JSR 199). |
duke@1 | 112 | */ |
duke@1 | 113 | protected DiagnosticListener<? super JavaFileObject> diagListener; |
jjg@73 | 114 | |
duke@1 | 115 | /** |
mcimadamore@221 | 116 | * Formatter for diagnostics. |
duke@1 | 117 | */ |
mcimadamore@83 | 118 | private DiagnosticFormatter<JCDiagnostic> diagFormatter; |
duke@1 | 119 | |
mcimadamore@136 | 120 | /** |
mcimadamore@221 | 121 | * Keys for expected diagnostics. |
jjg@194 | 122 | */ |
jjg@194 | 123 | public Set<String> expectDiagKeys; |
jjg@194 | 124 | |
jjg@194 | 125 | /** |
mcimadamore@221 | 126 | * JavacMessages object used for localization. |
mcimadamore@136 | 127 | */ |
mcimadamore@136 | 128 | private JavacMessages messages; |
mcimadamore@136 | 129 | |
jjg@664 | 130 | /** |
jjg@664 | 131 | * Deferred diagnostics |
jjg@664 | 132 | */ |
jjg@664 | 133 | public boolean deferDiagnostics; |
jjg@664 | 134 | public Queue<JCDiagnostic> deferredDiagnostics = new ListBuffer<JCDiagnostic>(); |
jjg@664 | 135 | |
duke@1 | 136 | /** Construct a log with given I/O redirections. |
duke@1 | 137 | */ |
duke@1 | 138 | protected Log(Context context, PrintWriter errWriter, PrintWriter warnWriter, PrintWriter noticeWriter) { |
jjg@73 | 139 | super(JCDiagnostic.Factory.instance(context)); |
duke@1 | 140 | context.put(logKey, this); |
duke@1 | 141 | this.errWriter = errWriter; |
duke@1 | 142 | this.warnWriter = warnWriter; |
duke@1 | 143 | this.noticeWriter = noticeWriter; |
duke@1 | 144 | |
duke@1 | 145 | @SuppressWarnings("unchecked") // FIXME |
jjg@194 | 146 | DiagnosticListener<? super JavaFileObject> dl = |
duke@1 | 147 | context.get(DiagnosticListener.class); |
jjg@194 | 148 | this.diagListener = dl; |
jjg@194 | 149 | |
jjg@1135 | 150 | messages = JavacMessages.instance(context); |
jjg@1136 | 151 | messages.add(Main.javacBundleName); |
jjg@1135 | 152 | |
jjg@1135 | 153 | final Options options = Options.instance(context); |
jjg@1135 | 154 | initOptions(options); |
jjg@1135 | 155 | options.addListener(new Runnable() { |
jjg@1135 | 156 | public void run() { |
jjg@1135 | 157 | initOptions(options); |
jjg@1135 | 158 | } |
jjg@1135 | 159 | }); |
duke@1 | 160 | } |
duke@1 | 161 | // where |
jjg@1135 | 162 | private void initOptions(Options options) { |
jjg@1135 | 163 | this.dumpOnError = options.isSet(DOE); |
jjg@1135 | 164 | this.promptOnError = options.isSet(PROMPT); |
jjg@1135 | 165 | this.emitWarnings = options.isUnset(XLINT_CUSTOM, "none"); |
jjg@1135 | 166 | this.suppressNotes = options.isSet("suppressNotes"); |
jjg@1135 | 167 | this.MaxErrors = getIntOption(options, XMAXERRS, getDefaultMaxErrors()); |
jjg@1135 | 168 | this.MaxWarnings = getIntOption(options, XMAXWARNS, getDefaultMaxWarnings()); |
jjg@1135 | 169 | |
jjg@1135 | 170 | boolean rawDiagnostics = options.isSet("rawDiagnostics"); |
jjg@1135 | 171 | this.diagFormatter = rawDiagnostics ? new RawDiagnosticFormatter(options) : |
jjg@1135 | 172 | new BasicDiagnosticFormatter(options, messages); |
jjg@1135 | 173 | |
jjg@1135 | 174 | String ek = options.get("expectKeys"); |
jjg@1135 | 175 | if (ek != null) |
jjg@1135 | 176 | expectDiagKeys = new HashSet<String>(Arrays.asList(ek.split(", *"))); |
jjg@1135 | 177 | } |
jjg@1135 | 178 | |
jjg@1157 | 179 | private int getIntOption(Options options, Option option, int defaultValue) { |
jjg@1157 | 180 | String s = options.get(option); |
duke@1 | 181 | try { |
jjg@464 | 182 | if (s != null) { |
jjg@464 | 183 | int n = Integer.parseInt(s); |
jjg@464 | 184 | return (n <= 0 ? Integer.MAX_VALUE : n); |
jjg@464 | 185 | } |
duke@1 | 186 | } catch (NumberFormatException e) { |
duke@1 | 187 | // silently ignore ill-formed numbers |
duke@1 | 188 | } |
duke@1 | 189 | return defaultValue; |
duke@1 | 190 | } |
duke@1 | 191 | |
jjg@584 | 192 | /** Default value for -Xmaxerrs. |
jjg@584 | 193 | */ |
jjg@584 | 194 | protected int getDefaultMaxErrors() { |
jjg@584 | 195 | return 100; |
jjg@584 | 196 | } |
jjg@584 | 197 | |
jjg@584 | 198 | /** Default value for -Xmaxwarns. |
jjg@584 | 199 | */ |
jjg@584 | 200 | protected int getDefaultMaxWarnings() { |
jjg@584 | 201 | return 100; |
jjg@584 | 202 | } |
jjg@584 | 203 | |
duke@1 | 204 | /** The default writer for diagnostics |
duke@1 | 205 | */ |
jjg@1135 | 206 | static PrintWriter defaultWriter(Context context) { |
duke@1 | 207 | PrintWriter result = context.get(outKey); |
duke@1 | 208 | if (result == null) |
duke@1 | 209 | context.put(outKey, result = new PrintWriter(System.err)); |
duke@1 | 210 | return result; |
duke@1 | 211 | } |
duke@1 | 212 | |
duke@1 | 213 | /** Construct a log with default settings. |
duke@1 | 214 | */ |
duke@1 | 215 | protected Log(Context context) { |
duke@1 | 216 | this(context, defaultWriter(context)); |
duke@1 | 217 | } |
duke@1 | 218 | |
duke@1 | 219 | /** Construct a log with all output redirected. |
duke@1 | 220 | */ |
duke@1 | 221 | protected Log(Context context, PrintWriter defaultWriter) { |
duke@1 | 222 | this(context, defaultWriter, defaultWriter, defaultWriter); |
duke@1 | 223 | } |
duke@1 | 224 | |
duke@1 | 225 | /** Get the Log instance for this context. */ |
duke@1 | 226 | public static Log instance(Context context) { |
duke@1 | 227 | Log instance = context.get(logKey); |
duke@1 | 228 | if (instance == null) |
duke@1 | 229 | instance = new Log(context); |
duke@1 | 230 | return instance; |
duke@1 | 231 | } |
duke@1 | 232 | |
duke@1 | 233 | /** The number of errors encountered so far. |
duke@1 | 234 | */ |
duke@1 | 235 | public int nerrors = 0; |
duke@1 | 236 | |
duke@1 | 237 | /** The number of warnings encountered so far. |
duke@1 | 238 | */ |
duke@1 | 239 | public int nwarnings = 0; |
duke@1 | 240 | |
duke@1 | 241 | /** A set of all errors generated so far. This is used to avoid printing an |
duke@1 | 242 | * error message more than once. For each error, a pair consisting of the |
duke@1 | 243 | * source file name and source code position of the error is added to the set. |
duke@1 | 244 | */ |
duke@1 | 245 | private Set<Pair<JavaFileObject, Integer>> recorded = new HashSet<Pair<JavaFileObject,Integer>>(); |
duke@1 | 246 | |
duke@1 | 247 | public boolean hasDiagnosticListener() { |
duke@1 | 248 | return diagListener != null; |
duke@1 | 249 | } |
duke@1 | 250 | |
ksrini@1138 | 251 | public void setEndPosTable(JavaFileObject name, EndPosTable endPosTable) { |
jjg@73 | 252 | name.getClass(); // null check |
ksrini@1138 | 253 | getSource(name).setEndPosTable(endPosTable); |
duke@1 | 254 | } |
duke@1 | 255 | |
mcimadamore@168 | 256 | /** Return current sourcefile. |
duke@1 | 257 | */ |
mcimadamore@168 | 258 | public JavaFileObject currentSourceFile() { |
duke@1 | 259 | return source == null ? null : source.getFile(); |
duke@1 | 260 | } |
duke@1 | 261 | |
mcimadamore@221 | 262 | /** Get the current diagnostic formatter. |
mcimadamore@221 | 263 | */ |
mcimadamore@221 | 264 | public DiagnosticFormatter<JCDiagnostic> getDiagnosticFormatter() { |
mcimadamore@221 | 265 | return diagFormatter; |
mcimadamore@221 | 266 | } |
mcimadamore@221 | 267 | |
mcimadamore@221 | 268 | /** Set the current diagnostic formatter. |
mcimadamore@221 | 269 | */ |
mcimadamore@221 | 270 | public void setDiagnosticFormatter(DiagnosticFormatter<JCDiagnostic> diagFormatter) { |
mcimadamore@221 | 271 | this.diagFormatter = diagFormatter; |
mcimadamore@221 | 272 | } |
mcimadamore@221 | 273 | |
jjg@1135 | 274 | public PrintWriter getWriter(WriterKind kind) { |
jjg@1135 | 275 | switch (kind) { |
jjg@1135 | 276 | case NOTICE: return noticeWriter; |
jjg@1135 | 277 | case WARNING: return warnWriter; |
jjg@1135 | 278 | case ERROR: return errWriter; |
jjg@1135 | 279 | default: throw new IllegalArgumentException(); |
jjg@1135 | 280 | } |
jjg@1135 | 281 | } |
jjg@1135 | 282 | |
jjg@1135 | 283 | public void setWriter(WriterKind kind, PrintWriter pw) { |
jjg@1135 | 284 | pw.getClass(); |
jjg@1135 | 285 | switch (kind) { |
jjg@1135 | 286 | case NOTICE: noticeWriter = pw; break; |
jjg@1135 | 287 | case WARNING: warnWriter = pw; break; |
jjg@1135 | 288 | case ERROR: errWriter = pw; break; |
jjg@1135 | 289 | default: throw new IllegalArgumentException(); |
jjg@1135 | 290 | } |
jjg@1135 | 291 | } |
jjg@1135 | 292 | |
jjg@1135 | 293 | public void setWriters(PrintWriter pw) { |
jjg@1135 | 294 | pw.getClass(); |
jjg@1135 | 295 | noticeWriter = warnWriter = errWriter = pw; |
jjg@1135 | 296 | } |
jjg@1135 | 297 | |
jjg@1159 | 298 | public void setWriters(Log other) { |
jjg@1159 | 299 | this.noticeWriter = other.noticeWriter; |
jjg@1159 | 300 | this.warnWriter = other.warnWriter; |
jjg@1159 | 301 | this.errWriter = other.errWriter; |
jjg@1159 | 302 | } |
jjg@1159 | 303 | |
duke@1 | 304 | /** Flush the logs |
duke@1 | 305 | */ |
duke@1 | 306 | public void flush() { |
duke@1 | 307 | errWriter.flush(); |
duke@1 | 308 | warnWriter.flush(); |
duke@1 | 309 | noticeWriter.flush(); |
duke@1 | 310 | } |
duke@1 | 311 | |
jjg@1135 | 312 | public void flush(WriterKind kind) { |
jjg@1135 | 313 | getWriter(kind).flush(); |
jjg@1135 | 314 | } |
jjg@1135 | 315 | |
duke@1 | 316 | /** Returns true if an error needs to be reported for a given |
duke@1 | 317 | * source name and pos. |
duke@1 | 318 | */ |
duke@1 | 319 | protected boolean shouldReport(JavaFileObject file, int pos) { |
duke@1 | 320 | if (multipleErrors || file == null) |
duke@1 | 321 | return true; |
duke@1 | 322 | |
duke@1 | 323 | Pair<JavaFileObject,Integer> coords = new Pair<JavaFileObject,Integer>(file, pos); |
duke@1 | 324 | boolean shouldReport = !recorded.contains(coords); |
duke@1 | 325 | if (shouldReport) |
duke@1 | 326 | recorded.add(coords); |
duke@1 | 327 | return shouldReport; |
duke@1 | 328 | } |
duke@1 | 329 | |
duke@1 | 330 | /** Prompt user after an error. |
duke@1 | 331 | */ |
duke@1 | 332 | public void prompt() { |
duke@1 | 333 | if (promptOnError) { |
jjg@604 | 334 | System.err.println(localize("resume.abort")); |
duke@1 | 335 | try { |
duke@1 | 336 | while (true) { |
duke@1 | 337 | switch (System.in.read()) { |
duke@1 | 338 | case 'a': case 'A': |
duke@1 | 339 | System.exit(-1); |
duke@1 | 340 | return; |
duke@1 | 341 | case 'r': case 'R': |
duke@1 | 342 | return; |
duke@1 | 343 | case 'x': case 'X': |
duke@1 | 344 | throw new AssertionError("user abort"); |
duke@1 | 345 | default: |
duke@1 | 346 | } |
duke@1 | 347 | } |
duke@1 | 348 | } catch (IOException e) {} |
duke@1 | 349 | } |
duke@1 | 350 | } |
duke@1 | 351 | |
duke@1 | 352 | /** Print the faulty source code line and point to the error. |
duke@1 | 353 | * @param pos Buffer index of the error position, must be on current line |
duke@1 | 354 | */ |
duke@1 | 355 | private void printErrLine(int pos, PrintWriter writer) { |
jjg@73 | 356 | String line = (source == null ? null : source.getLine(pos)); |
jjg@73 | 357 | if (line == null) |
duke@1 | 358 | return; |
mcimadamore@100 | 359 | int col = source.getColumnNumber(pos, false); |
duke@1 | 360 | |
jjg@1136 | 361 | printRawLines(writer, line); |
jjg@73 | 362 | for (int i = 0; i < col - 1; i++) { |
jjg@73 | 363 | writer.print((line.charAt(i) == '\t') ? "\t" : " "); |
duke@1 | 364 | } |
duke@1 | 365 | writer.println("^"); |
duke@1 | 366 | writer.flush(); |
duke@1 | 367 | } |
duke@1 | 368 | |
jjg@1136 | 369 | public void printNewline() { |
jjg@1136 | 370 | noticeWriter.println(); |
jjg@1136 | 371 | } |
jjg@1136 | 372 | |
jjg@1136 | 373 | public void printNewline(WriterKind wk) { |
jjg@1136 | 374 | getWriter(wk).println(); |
jjg@1136 | 375 | } |
jjg@1136 | 376 | |
jjg@1136 | 377 | public void printLines(String key, Object... args) { |
jjg@1136 | 378 | printRawLines(noticeWriter, localize(key, args)); |
jjg@1136 | 379 | } |
jjg@1136 | 380 | |
jjg@1136 | 381 | public void printLines(PrefixKind pk, String key, Object... args) { |
jjg@1136 | 382 | printRawLines(noticeWriter, localize(pk, key, args)); |
jjg@1136 | 383 | } |
jjg@1136 | 384 | |
jjg@1136 | 385 | public void printLines(WriterKind wk, String key, Object... args) { |
jjg@1136 | 386 | printRawLines(getWriter(wk), localize(key, args)); |
jjg@1136 | 387 | } |
jjg@1136 | 388 | |
jjg@1136 | 389 | public void printLines(WriterKind wk, PrefixKind pk, String key, Object... args) { |
jjg@1136 | 390 | printRawLines(getWriter(wk), localize(pk, key, args)); |
jjg@1135 | 391 | } |
jjg@1135 | 392 | |
jjg@1135 | 393 | /** Print the text of a message, translating newlines appropriately |
jjg@1135 | 394 | * for the platform. |
jjg@1135 | 395 | */ |
jjg@1136 | 396 | public void printRawLines(String msg) { |
jjg@1136 | 397 | printRawLines(noticeWriter, msg); |
jjg@1136 | 398 | } |
jjg@1136 | 399 | |
jjg@1136 | 400 | /** Print the text of a message, translating newlines appropriately |
jjg@1136 | 401 | * for the platform. |
jjg@1136 | 402 | */ |
jjg@1136 | 403 | public void printRawLines(WriterKind kind, String msg) { |
jjg@1136 | 404 | printRawLines(getWriter(kind), msg); |
jjg@1136 | 405 | } |
jjg@1136 | 406 | |
jjg@1136 | 407 | /** Print the text of a message, translating newlines appropriately |
jjg@1136 | 408 | * for the platform. |
jjg@1136 | 409 | */ |
jjg@1136 | 410 | public static void printRawLines(PrintWriter writer, String msg) { |
duke@1 | 411 | int nl; |
duke@1 | 412 | while ((nl = msg.indexOf('\n')) != -1) { |
duke@1 | 413 | writer.println(msg.substring(0, nl)); |
duke@1 | 414 | msg = msg.substring(nl+1); |
duke@1 | 415 | } |
duke@1 | 416 | if (msg.length() != 0) writer.println(msg); |
duke@1 | 417 | } |
duke@1 | 418 | |
jjg@909 | 419 | /** |
jjg@909 | 420 | * Print the localized text of a "verbose" message to the |
jjg@909 | 421 | * noticeWriter stream. |
jjg@909 | 422 | */ |
jjg@909 | 423 | public void printVerbose(String key, Object... args) { |
jjg@1136 | 424 | printRawLines(noticeWriter, localize("verbose." + key, args)); |
jjg@909 | 425 | } |
jjg@909 | 426 | |
jjg@73 | 427 | protected void directError(String key, Object... args) { |
jjg@1136 | 428 | printRawLines(errWriter, localize(key, args)); |
jjg@73 | 429 | errWriter.flush(); |
duke@1 | 430 | } |
duke@1 | 431 | |
duke@1 | 432 | /** Report a warning that cannot be suppressed. |
duke@1 | 433 | * @param pos The source position at which to report the warning. |
duke@1 | 434 | * @param key The key for the localized warning message. |
duke@1 | 435 | * @param args Fields of the warning message. |
duke@1 | 436 | */ |
duke@1 | 437 | public void strictWarning(DiagnosticPosition pos, String key, Object ... args) { |
duke@1 | 438 | writeDiagnostic(diags.warning(source, pos, key, args)); |
duke@1 | 439 | nwarnings++; |
duke@1 | 440 | } |
duke@1 | 441 | |
jjg@664 | 442 | /** Report all deferred diagnostics, and clear the deferDiagnostics flag. */ |
jjg@664 | 443 | public void reportDeferredDiagnostics() { |
jjg@664 | 444 | reportDeferredDiagnostics(EnumSet.allOf(JCDiagnostic.Kind.class)); |
jjg@664 | 445 | } |
jjg@664 | 446 | |
jjg@664 | 447 | /** Report selected deferred diagnostics, and clear the deferDiagnostics flag. */ |
jjg@664 | 448 | public void reportDeferredDiagnostics(Set<JCDiagnostic.Kind> kinds) { |
jjg@664 | 449 | deferDiagnostics = false; |
jjg@664 | 450 | JCDiagnostic d; |
jjg@664 | 451 | while ((d = deferredDiagnostics.poll()) != null) { |
jjg@664 | 452 | if (kinds.contains(d.getKind())) |
jjg@664 | 453 | report(d); |
jjg@664 | 454 | } |
jjg@664 | 455 | } |
jjg@664 | 456 | |
duke@1 | 457 | /** |
duke@1 | 458 | * Common diagnostic handling. |
duke@1 | 459 | * The diagnostic is counted, and depending on the options and how many diagnostics have been |
duke@1 | 460 | * reported so far, the diagnostic may be handed off to writeDiagnostic. |
duke@1 | 461 | */ |
duke@1 | 462 | public void report(JCDiagnostic diagnostic) { |
jjg@664 | 463 | if (deferDiagnostics) { |
jjg@664 | 464 | deferredDiagnostics.add(diagnostic); |
jjg@664 | 465 | return; |
jjg@664 | 466 | } |
jjg@664 | 467 | |
jjg@194 | 468 | if (expectDiagKeys != null) |
jjg@194 | 469 | expectDiagKeys.remove(diagnostic.getCode()); |
jjg@194 | 470 | |
duke@1 | 471 | switch (diagnostic.getType()) { |
duke@1 | 472 | case FRAGMENT: |
duke@1 | 473 | throw new IllegalArgumentException(); |
duke@1 | 474 | |
duke@1 | 475 | case NOTE: |
duke@1 | 476 | // Print out notes only when we are permitted to report warnings |
duke@1 | 477 | // Notes are only generated at the end of a compilation, so should be small |
duke@1 | 478 | // in number. |
jjg@376 | 479 | if ((emitWarnings || diagnostic.isMandatory()) && !suppressNotes) { |
duke@1 | 480 | writeDiagnostic(diagnostic); |
duke@1 | 481 | } |
duke@1 | 482 | break; |
duke@1 | 483 | |
duke@1 | 484 | case WARNING: |
duke@1 | 485 | if (emitWarnings || diagnostic.isMandatory()) { |
duke@1 | 486 | if (nwarnings < MaxWarnings) { |
duke@1 | 487 | writeDiagnostic(diagnostic); |
duke@1 | 488 | nwarnings++; |
duke@1 | 489 | } |
duke@1 | 490 | } |
duke@1 | 491 | break; |
duke@1 | 492 | |
duke@1 | 493 | case ERROR: |
duke@1 | 494 | if (nerrors < MaxErrors |
duke@1 | 495 | && shouldReport(diagnostic.getSource(), diagnostic.getIntPosition())) { |
duke@1 | 496 | writeDiagnostic(diagnostic); |
duke@1 | 497 | nerrors++; |
duke@1 | 498 | } |
duke@1 | 499 | break; |
duke@1 | 500 | } |
duke@1 | 501 | } |
duke@1 | 502 | |
duke@1 | 503 | /** |
duke@1 | 504 | * Write out a diagnostic. |
duke@1 | 505 | */ |
duke@1 | 506 | protected void writeDiagnostic(JCDiagnostic diag) { |
duke@1 | 507 | if (diagListener != null) { |
jjg@946 | 508 | diagListener.report(diag); |
jjg@946 | 509 | return; |
duke@1 | 510 | } |
duke@1 | 511 | |
duke@1 | 512 | PrintWriter writer = getWriterForDiagnosticType(diag.getType()); |
duke@1 | 513 | |
jjg@1136 | 514 | printRawLines(writer, diagFormatter.format(diag, messages.getCurrentLocale())); |
duke@1 | 515 | |
duke@1 | 516 | if (promptOnError) { |
duke@1 | 517 | switch (diag.getType()) { |
duke@1 | 518 | case ERROR: |
duke@1 | 519 | case WARNING: |
duke@1 | 520 | prompt(); |
duke@1 | 521 | } |
duke@1 | 522 | } |
duke@1 | 523 | |
duke@1 | 524 | if (dumpOnError) |
duke@1 | 525 | new RuntimeException().printStackTrace(writer); |
duke@1 | 526 | |
duke@1 | 527 | writer.flush(); |
duke@1 | 528 | } |
duke@1 | 529 | |
duke@1 | 530 | @Deprecated |
duke@1 | 531 | protected PrintWriter getWriterForDiagnosticType(DiagnosticType dt) { |
duke@1 | 532 | switch (dt) { |
duke@1 | 533 | case FRAGMENT: |
duke@1 | 534 | throw new IllegalArgumentException(); |
duke@1 | 535 | |
duke@1 | 536 | case NOTE: |
duke@1 | 537 | return noticeWriter; |
duke@1 | 538 | |
duke@1 | 539 | case WARNING: |
duke@1 | 540 | return warnWriter; |
duke@1 | 541 | |
duke@1 | 542 | case ERROR: |
duke@1 | 543 | return errWriter; |
duke@1 | 544 | |
duke@1 | 545 | default: |
duke@1 | 546 | throw new Error(); |
duke@1 | 547 | } |
duke@1 | 548 | } |
duke@1 | 549 | |
duke@1 | 550 | /** Find a localized string in the resource bundle. |
jjg@604 | 551 | * Because this method is static, it ignores the locale. |
jjg@604 | 552 | * Use localize(key, args) when possible. |
duke@1 | 553 | * @param key The key for the localized string. |
duke@1 | 554 | * @param args Fields to substitute into the string. |
duke@1 | 555 | */ |
duke@1 | 556 | public static String getLocalizedString(String key, Object ... args) { |
jjg@1136 | 557 | return JavacMessages.getDefaultLocalizedString(PrefixKind.COMPILER_MISC.key(key), args); |
duke@1 | 558 | } |
duke@1 | 559 | |
jjg@604 | 560 | /** Find a localized string in the resource bundle. |
jjg@604 | 561 | * @param key The key for the localized string. |
jjg@604 | 562 | * @param args Fields to substitute into the string. |
jjg@604 | 563 | */ |
jjg@604 | 564 | public String localize(String key, Object... args) { |
jjg@1136 | 565 | return localize(PrefixKind.COMPILER_MISC, key, args); |
jjg@604 | 566 | } |
jjg@604 | 567 | |
jjg@1136 | 568 | /** Find a localized string in the resource bundle. |
jjg@1136 | 569 | * @param key The key for the localized string. |
jjg@1136 | 570 | * @param args Fields to substitute into the string. |
jjg@1136 | 571 | */ |
jjg@1136 | 572 | public String localize(PrefixKind pk, String key, Object... args) { |
jjg@1136 | 573 | if (useRawMessages) |
jjg@1136 | 574 | return pk.key(key); |
jjg@1136 | 575 | else |
jjg@1136 | 576 | return messages.getLocalizedString(pk.key(key), args); |
jjg@1136 | 577 | } |
jjg@1136 | 578 | // where |
jjg@1136 | 579 | // backdoor hook for testing, should transition to use -XDrawDiagnostics |
jjg@1136 | 580 | private static boolean useRawMessages = false; |
jjg@1136 | 581 | |
duke@1 | 582 | /*************************************************************************** |
duke@1 | 583 | * raw error messages without internationalization; used for experimentation |
duke@1 | 584 | * and quick prototyping |
duke@1 | 585 | ***************************************************************************/ |
duke@1 | 586 | |
jjg@73 | 587 | /** print an error or warning message: |
jjg@73 | 588 | */ |
duke@1 | 589 | private void printRawError(int pos, String msg) { |
jjg@73 | 590 | if (source == null || pos == Position.NOPOS) { |
jjg@1136 | 591 | printRawLines(errWriter, "error: " + msg); |
duke@1 | 592 | } else { |
jjg@73 | 593 | int line = source.getLineNumber(pos); |
mcimadamore@168 | 594 | JavaFileObject file = source.getFile(); |
duke@1 | 595 | if (file != null) |
jjg@1136 | 596 | printRawLines(errWriter, |
jjg@415 | 597 | file.getName() + ":" + |
duke@1 | 598 | line + ": " + msg); |
duke@1 | 599 | printErrLine(pos, errWriter); |
duke@1 | 600 | } |
duke@1 | 601 | errWriter.flush(); |
duke@1 | 602 | } |
duke@1 | 603 | |
jjg@73 | 604 | /** report an error: |
jjg@73 | 605 | */ |
duke@1 | 606 | public void rawError(int pos, String msg) { |
mcimadamore@168 | 607 | if (nerrors < MaxErrors && shouldReport(currentSourceFile(), pos)) { |
duke@1 | 608 | printRawError(pos, msg); |
duke@1 | 609 | prompt(); |
duke@1 | 610 | nerrors++; |
duke@1 | 611 | } |
duke@1 | 612 | errWriter.flush(); |
duke@1 | 613 | } |
duke@1 | 614 | |
jjg@73 | 615 | /** report a warning: |
jjg@73 | 616 | */ |
duke@1 | 617 | public void rawWarning(int pos, String msg) { |
duke@1 | 618 | if (nwarnings < MaxWarnings && emitWarnings) { |
duke@1 | 619 | printRawError(pos, "warning: " + msg); |
duke@1 | 620 | } |
duke@1 | 621 | prompt(); |
duke@1 | 622 | nwarnings++; |
duke@1 | 623 | errWriter.flush(); |
duke@1 | 624 | } |
duke@1 | 625 | |
duke@1 | 626 | public static String format(String fmt, Object... args) { |
duke@1 | 627 | return String.format((java.util.Locale)null, fmt, args); |
duke@1 | 628 | } |
duke@1 | 629 | |
duke@1 | 630 | } |