Wed, 16 Oct 2013 16:33:04 -0400
8026286: Improper locking of annotation queues causes assertion failures.
8026063: Calls to annotate.flush() cause incorrect type annotations to be generated.
Summary: Fix locking in ClassReader.java
Reviewed-by: jfranck
duke@1 | 1 | /* |
jjg@1521 | 2 | * Copyright (c) 1999, 2013, 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; |
jjg@1280 | 40 | import com.sun.tools.javac.tree.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@1406 | 76 | /** |
jjg@1406 | 77 | * DiagnosticHandler's provide the initial handling for diagnostics. |
jjg@1406 | 78 | * When a diagnostic handler is created and has been initialized, it |
jjg@1406 | 79 | * should install itself as the current diagnostic handler. When a |
jjg@1406 | 80 | * client has finished using a handler, the client should call |
jjg@1406 | 81 | * {@code log.removeDiagnosticHandler();} |
jjg@1406 | 82 | * |
jjg@1406 | 83 | * Note that javax.tools.DiagnosticListener (if set) is called later in the |
jjg@1406 | 84 | * diagnostic pipeline. |
jjg@1406 | 85 | */ |
jjg@1406 | 86 | public static abstract class DiagnosticHandler { |
jjg@1406 | 87 | /** |
jjg@1406 | 88 | * The previously installed diagnostic handler. |
jjg@1406 | 89 | */ |
jjg@1406 | 90 | protected DiagnosticHandler prev; |
jjg@1406 | 91 | |
jjg@1406 | 92 | /** |
jjg@1406 | 93 | * Install this diagnostic handler as the current one, |
jjg@1406 | 94 | * recording the previous one. |
jjg@1406 | 95 | */ |
jjg@1406 | 96 | protected void install(Log log) { |
jjg@1406 | 97 | prev = log.diagnosticHandler; |
jjg@1406 | 98 | log.diagnosticHandler = this; |
jjg@1406 | 99 | } |
jjg@1406 | 100 | |
jjg@1406 | 101 | /** |
jjg@1406 | 102 | * Handle a diagnostic. |
jjg@1406 | 103 | */ |
jjg@1406 | 104 | public abstract void report(JCDiagnostic diag); |
jjg@1406 | 105 | } |
jjg@1406 | 106 | |
jjg@1406 | 107 | /** |
jjg@1406 | 108 | * A DiagnosticHandler that discards all diagnostics. |
jjg@1406 | 109 | */ |
jjg@1406 | 110 | public static class DiscardDiagnosticHandler extends DiagnosticHandler { |
jjg@1406 | 111 | public DiscardDiagnosticHandler(Log log) { |
jjg@1406 | 112 | install(log); |
jjg@1406 | 113 | } |
jjg@1406 | 114 | |
jjg@1406 | 115 | public void report(JCDiagnostic diag) { } |
jjg@1406 | 116 | } |
jjg@1406 | 117 | |
jjg@1406 | 118 | /** |
jjg@1406 | 119 | * A DiagnosticHandler that can defer some or all diagnostics, |
jjg@1406 | 120 | * by buffering them for later examination and/or reporting. |
jjg@1406 | 121 | * If a diagnostic is not deferred, or is subsequently reported |
jjg@1406 | 122 | * with reportAllDiagnostics(), it will be reported to the previously |
jjg@1406 | 123 | * active diagnostic handler. |
jjg@1406 | 124 | */ |
jjg@1406 | 125 | public static class DeferredDiagnosticHandler extends DiagnosticHandler { |
alundblad@2047 | 126 | private Queue<JCDiagnostic> deferred = new ListBuffer<>(); |
jjg@1406 | 127 | private final Filter<JCDiagnostic> filter; |
jjg@1406 | 128 | |
jjg@1406 | 129 | public DeferredDiagnosticHandler(Log log) { |
jjg@1406 | 130 | this(log, null); |
jjg@1406 | 131 | } |
jjg@1406 | 132 | |
jjg@1406 | 133 | public DeferredDiagnosticHandler(Log log, Filter<JCDiagnostic> filter) { |
jjg@1406 | 134 | this.filter = filter; |
jjg@1406 | 135 | install(log); |
jjg@1406 | 136 | } |
jjg@1406 | 137 | |
jjg@1406 | 138 | public void report(JCDiagnostic diag) { |
mcimadamore@1613 | 139 | if (!diag.isFlagSet(JCDiagnostic.DiagnosticFlag.NON_DEFERRABLE) && |
mcimadamore@1613 | 140 | (filter == null || filter.accepts(diag))) { |
jjg@1406 | 141 | deferred.add(diag); |
mcimadamore@1613 | 142 | } else { |
jjg@1406 | 143 | prev.report(diag); |
mcimadamore@1613 | 144 | } |
jjg@1406 | 145 | } |
jjg@1406 | 146 | |
jjg@1406 | 147 | public Queue<JCDiagnostic> getDiagnostics() { |
jjg@1406 | 148 | return deferred; |
jjg@1406 | 149 | } |
jjg@1406 | 150 | |
jjg@1406 | 151 | /** Report all deferred diagnostics. */ |
jjg@1406 | 152 | public void reportDeferredDiagnostics() { |
jjg@1406 | 153 | reportDeferredDiagnostics(EnumSet.allOf(JCDiagnostic.Kind.class)); |
jjg@1406 | 154 | } |
jjg@1406 | 155 | |
jjg@1406 | 156 | /** Report selected deferred diagnostics. */ |
jjg@1406 | 157 | public void reportDeferredDiagnostics(Set<JCDiagnostic.Kind> kinds) { |
jjg@1406 | 158 | JCDiagnostic d; |
jjg@1406 | 159 | while ((d = deferred.poll()) != null) { |
jjg@1406 | 160 | if (kinds.contains(d.getKind())) |
jjg@1406 | 161 | prev.report(d); |
jjg@1406 | 162 | } |
jjg@1406 | 163 | deferred = null; // prevent accidental ongoing use |
jjg@1406 | 164 | } |
jjg@1406 | 165 | } |
jjg@1406 | 166 | |
jjg@1135 | 167 | public enum WriterKind { NOTICE, WARNING, ERROR }; |
duke@1 | 168 | |
jjg@1135 | 169 | protected PrintWriter errWriter; |
duke@1 | 170 | |
jjg@1135 | 171 | protected PrintWriter warnWriter; |
jjg@1135 | 172 | |
jjg@1135 | 173 | protected PrintWriter noticeWriter; |
duke@1 | 174 | |
duke@1 | 175 | /** The maximum number of errors/warnings that are reported. |
duke@1 | 176 | */ |
jjg@1135 | 177 | protected int MaxErrors; |
jjg@1135 | 178 | protected int MaxWarnings; |
duke@1 | 179 | |
duke@1 | 180 | /** Switch: prompt user on each error. |
duke@1 | 181 | */ |
duke@1 | 182 | public boolean promptOnError; |
duke@1 | 183 | |
duke@1 | 184 | /** Switch: emit warning messages. |
duke@1 | 185 | */ |
duke@1 | 186 | public boolean emitWarnings; |
duke@1 | 187 | |
jjg@376 | 188 | /** Switch: suppress note messages. |
jjg@376 | 189 | */ |
jjg@376 | 190 | public boolean suppressNotes; |
jjg@376 | 191 | |
duke@1 | 192 | /** Print stack trace on errors? |
duke@1 | 193 | */ |
duke@1 | 194 | public boolean dumpOnError; |
duke@1 | 195 | |
duke@1 | 196 | /** Print multiple errors for same source locations. |
duke@1 | 197 | */ |
duke@1 | 198 | public boolean multipleErrors; |
duke@1 | 199 | |
duke@1 | 200 | /** |
duke@1 | 201 | * Diagnostic listener, if provided through programmatic |
duke@1 | 202 | * interface to javac (JSR 199). |
duke@1 | 203 | */ |
duke@1 | 204 | protected DiagnosticListener<? super JavaFileObject> diagListener; |
jjg@73 | 205 | |
duke@1 | 206 | /** |
mcimadamore@221 | 207 | * Formatter for diagnostics. |
duke@1 | 208 | */ |
mcimadamore@83 | 209 | private DiagnosticFormatter<JCDiagnostic> diagFormatter; |
duke@1 | 210 | |
mcimadamore@136 | 211 | /** |
mcimadamore@221 | 212 | * Keys for expected diagnostics. |
jjg@194 | 213 | */ |
jjg@194 | 214 | public Set<String> expectDiagKeys; |
jjg@194 | 215 | |
jjg@194 | 216 | /** |
mcimadamore@1759 | 217 | * Set to true if a compressed diagnostic is reported |
mcimadamore@1759 | 218 | */ |
mcimadamore@1759 | 219 | public boolean compressedOutput; |
mcimadamore@1759 | 220 | |
mcimadamore@1759 | 221 | /** |
mcimadamore@221 | 222 | * JavacMessages object used for localization. |
mcimadamore@136 | 223 | */ |
mcimadamore@136 | 224 | private JavacMessages messages; |
mcimadamore@136 | 225 | |
jjg@664 | 226 | /** |
jjg@1521 | 227 | * Handler for initial dispatch of diagnostics. |
jjg@664 | 228 | */ |
jjg@1406 | 229 | private DiagnosticHandler diagnosticHandler; |
jjg@664 | 230 | |
duke@1 | 231 | /** Construct a log with given I/O redirections. |
duke@1 | 232 | */ |
duke@1 | 233 | protected Log(Context context, PrintWriter errWriter, PrintWriter warnWriter, PrintWriter noticeWriter) { |
jjg@73 | 234 | super(JCDiagnostic.Factory.instance(context)); |
duke@1 | 235 | context.put(logKey, this); |
duke@1 | 236 | this.errWriter = errWriter; |
duke@1 | 237 | this.warnWriter = warnWriter; |
duke@1 | 238 | this.noticeWriter = noticeWriter; |
duke@1 | 239 | |
duke@1 | 240 | @SuppressWarnings("unchecked") // FIXME |
jjg@194 | 241 | DiagnosticListener<? super JavaFileObject> dl = |
duke@1 | 242 | context.get(DiagnosticListener.class); |
jjg@194 | 243 | this.diagListener = dl; |
jjg@194 | 244 | |
jjg@1406 | 245 | diagnosticHandler = new DefaultDiagnosticHandler(); |
jjg@1406 | 246 | |
jjg@1135 | 247 | messages = JavacMessages.instance(context); |
jjg@1136 | 248 | messages.add(Main.javacBundleName); |
jjg@1135 | 249 | |
jjg@1135 | 250 | final Options options = Options.instance(context); |
jjg@1135 | 251 | initOptions(options); |
jjg@1135 | 252 | options.addListener(new Runnable() { |
jjg@1135 | 253 | public void run() { |
jjg@1135 | 254 | initOptions(options); |
jjg@1135 | 255 | } |
jjg@1135 | 256 | }); |
duke@1 | 257 | } |
duke@1 | 258 | // where |
jjg@1135 | 259 | private void initOptions(Options options) { |
jjg@1135 | 260 | this.dumpOnError = options.isSet(DOE); |
jjg@1135 | 261 | this.promptOnError = options.isSet(PROMPT); |
jjg@1135 | 262 | this.emitWarnings = options.isUnset(XLINT_CUSTOM, "none"); |
jjg@1135 | 263 | this.suppressNotes = options.isSet("suppressNotes"); |
jjg@1135 | 264 | this.MaxErrors = getIntOption(options, XMAXERRS, getDefaultMaxErrors()); |
jjg@1135 | 265 | this.MaxWarnings = getIntOption(options, XMAXWARNS, getDefaultMaxWarnings()); |
jjg@1135 | 266 | |
jjg@1135 | 267 | boolean rawDiagnostics = options.isSet("rawDiagnostics"); |
jjg@1135 | 268 | this.diagFormatter = rawDiagnostics ? new RawDiagnosticFormatter(options) : |
jjg@1135 | 269 | new BasicDiagnosticFormatter(options, messages); |
jjg@1135 | 270 | |
jjg@1135 | 271 | String ek = options.get("expectKeys"); |
jjg@1135 | 272 | if (ek != null) |
jjg@1135 | 273 | expectDiagKeys = new HashSet<String>(Arrays.asList(ek.split(", *"))); |
jjg@1135 | 274 | } |
jjg@1135 | 275 | |
jjg@1157 | 276 | private int getIntOption(Options options, Option option, int defaultValue) { |
jjg@1157 | 277 | String s = options.get(option); |
duke@1 | 278 | try { |
jjg@464 | 279 | if (s != null) { |
jjg@464 | 280 | int n = Integer.parseInt(s); |
jjg@464 | 281 | return (n <= 0 ? Integer.MAX_VALUE : n); |
jjg@464 | 282 | } |
duke@1 | 283 | } catch (NumberFormatException e) { |
duke@1 | 284 | // silently ignore ill-formed numbers |
duke@1 | 285 | } |
duke@1 | 286 | return defaultValue; |
duke@1 | 287 | } |
duke@1 | 288 | |
jjg@584 | 289 | /** Default value for -Xmaxerrs. |
jjg@584 | 290 | */ |
jjg@584 | 291 | protected int getDefaultMaxErrors() { |
jjg@584 | 292 | return 100; |
jjg@584 | 293 | } |
jjg@584 | 294 | |
jjg@584 | 295 | /** Default value for -Xmaxwarns. |
jjg@584 | 296 | */ |
jjg@584 | 297 | protected int getDefaultMaxWarnings() { |
jjg@584 | 298 | return 100; |
jjg@584 | 299 | } |
jjg@584 | 300 | |
duke@1 | 301 | /** The default writer for diagnostics |
duke@1 | 302 | */ |
jjg@1135 | 303 | static PrintWriter defaultWriter(Context context) { |
duke@1 | 304 | PrintWriter result = context.get(outKey); |
duke@1 | 305 | if (result == null) |
duke@1 | 306 | context.put(outKey, result = new PrintWriter(System.err)); |
duke@1 | 307 | return result; |
duke@1 | 308 | } |
duke@1 | 309 | |
duke@1 | 310 | /** Construct a log with default settings. |
duke@1 | 311 | */ |
duke@1 | 312 | protected Log(Context context) { |
duke@1 | 313 | this(context, defaultWriter(context)); |
duke@1 | 314 | } |
duke@1 | 315 | |
duke@1 | 316 | /** Construct a log with all output redirected. |
duke@1 | 317 | */ |
duke@1 | 318 | protected Log(Context context, PrintWriter defaultWriter) { |
duke@1 | 319 | this(context, defaultWriter, defaultWriter, defaultWriter); |
duke@1 | 320 | } |
duke@1 | 321 | |
duke@1 | 322 | /** Get the Log instance for this context. */ |
duke@1 | 323 | public static Log instance(Context context) { |
duke@1 | 324 | Log instance = context.get(logKey); |
duke@1 | 325 | if (instance == null) |
duke@1 | 326 | instance = new Log(context); |
duke@1 | 327 | return instance; |
duke@1 | 328 | } |
duke@1 | 329 | |
duke@1 | 330 | /** The number of errors encountered so far. |
duke@1 | 331 | */ |
duke@1 | 332 | public int nerrors = 0; |
duke@1 | 333 | |
duke@1 | 334 | /** The number of warnings encountered so far. |
duke@1 | 335 | */ |
duke@1 | 336 | public int nwarnings = 0; |
duke@1 | 337 | |
duke@1 | 338 | /** A set of all errors generated so far. This is used to avoid printing an |
duke@1 | 339 | * error message more than once. For each error, a pair consisting of the |
duke@1 | 340 | * source file name and source code position of the error is added to the set. |
duke@1 | 341 | */ |
duke@1 | 342 | private Set<Pair<JavaFileObject, Integer>> recorded = new HashSet<Pair<JavaFileObject,Integer>>(); |
duke@1 | 343 | |
duke@1 | 344 | public boolean hasDiagnosticListener() { |
duke@1 | 345 | return diagListener != null; |
duke@1 | 346 | } |
duke@1 | 347 | |
ksrini@1138 | 348 | public void setEndPosTable(JavaFileObject name, EndPosTable endPosTable) { |
jjg@73 | 349 | name.getClass(); // null check |
ksrini@1138 | 350 | getSource(name).setEndPosTable(endPosTable); |
duke@1 | 351 | } |
duke@1 | 352 | |
mcimadamore@168 | 353 | /** Return current sourcefile. |
duke@1 | 354 | */ |
mcimadamore@168 | 355 | public JavaFileObject currentSourceFile() { |
duke@1 | 356 | return source == null ? null : source.getFile(); |
duke@1 | 357 | } |
duke@1 | 358 | |
mcimadamore@221 | 359 | /** Get the current diagnostic formatter. |
mcimadamore@221 | 360 | */ |
mcimadamore@221 | 361 | public DiagnosticFormatter<JCDiagnostic> getDiagnosticFormatter() { |
mcimadamore@221 | 362 | return diagFormatter; |
mcimadamore@221 | 363 | } |
mcimadamore@221 | 364 | |
mcimadamore@221 | 365 | /** Set the current diagnostic formatter. |
mcimadamore@221 | 366 | */ |
mcimadamore@221 | 367 | public void setDiagnosticFormatter(DiagnosticFormatter<JCDiagnostic> diagFormatter) { |
mcimadamore@221 | 368 | this.diagFormatter = diagFormatter; |
mcimadamore@221 | 369 | } |
mcimadamore@221 | 370 | |
jjg@1135 | 371 | public PrintWriter getWriter(WriterKind kind) { |
jjg@1135 | 372 | switch (kind) { |
jjg@1135 | 373 | case NOTICE: return noticeWriter; |
jjg@1135 | 374 | case WARNING: return warnWriter; |
jjg@1135 | 375 | case ERROR: return errWriter; |
jjg@1135 | 376 | default: throw new IllegalArgumentException(); |
jjg@1135 | 377 | } |
jjg@1135 | 378 | } |
jjg@1135 | 379 | |
jjg@1135 | 380 | public void setWriter(WriterKind kind, PrintWriter pw) { |
jjg@1135 | 381 | pw.getClass(); |
jjg@1135 | 382 | switch (kind) { |
jjg@1135 | 383 | case NOTICE: noticeWriter = pw; break; |
jjg@1135 | 384 | case WARNING: warnWriter = pw; break; |
jjg@1135 | 385 | case ERROR: errWriter = pw; break; |
jjg@1135 | 386 | default: throw new IllegalArgumentException(); |
jjg@1135 | 387 | } |
jjg@1135 | 388 | } |
jjg@1135 | 389 | |
jjg@1135 | 390 | public void setWriters(PrintWriter pw) { |
jjg@1135 | 391 | pw.getClass(); |
jjg@1135 | 392 | noticeWriter = warnWriter = errWriter = pw; |
jjg@1135 | 393 | } |
jjg@1135 | 394 | |
jjg@1521 | 395 | /** |
jjg@1521 | 396 | * Propagate the previous log's information. |
jjg@1521 | 397 | */ |
jjg@1521 | 398 | public void initRound(Log other) { |
jjg@1159 | 399 | this.noticeWriter = other.noticeWriter; |
jjg@1159 | 400 | this.warnWriter = other.warnWriter; |
jjg@1159 | 401 | this.errWriter = other.errWriter; |
jjg@1323 | 402 | this.sourceMap = other.sourceMap; |
jjg@1521 | 403 | this.recorded = other.recorded; |
jjg@1521 | 404 | this.nerrors = other.nerrors; |
jjg@1521 | 405 | this.nwarnings = other.nwarnings; |
jjg@1323 | 406 | } |
jjg@1323 | 407 | |
jjg@1406 | 408 | /** |
jjg@1406 | 409 | * Replace the specified diagnostic handler with the |
jjg@1406 | 410 | * handler that was current at the time this handler was created. |
jjg@1406 | 411 | * The given handler must be the currently installed handler; |
jjg@1406 | 412 | * it must be specified explicitly for clarity and consistency checking. |
jjg@1406 | 413 | */ |
jjg@1406 | 414 | public void popDiagnosticHandler(DiagnosticHandler h) { |
jjg@1406 | 415 | Assert.check(diagnosticHandler == h); |
jjg@1406 | 416 | diagnosticHandler = h.prev; |
jjg@1406 | 417 | } |
jjg@1406 | 418 | |
duke@1 | 419 | /** Flush the logs |
duke@1 | 420 | */ |
duke@1 | 421 | public void flush() { |
duke@1 | 422 | errWriter.flush(); |
duke@1 | 423 | warnWriter.flush(); |
duke@1 | 424 | noticeWriter.flush(); |
duke@1 | 425 | } |
duke@1 | 426 | |
jjg@1135 | 427 | public void flush(WriterKind kind) { |
jjg@1135 | 428 | getWriter(kind).flush(); |
jjg@1135 | 429 | } |
jjg@1135 | 430 | |
duke@1 | 431 | /** Returns true if an error needs to be reported for a given |
duke@1 | 432 | * source name and pos. |
duke@1 | 433 | */ |
duke@1 | 434 | protected boolean shouldReport(JavaFileObject file, int pos) { |
duke@1 | 435 | if (multipleErrors || file == null) |
duke@1 | 436 | return true; |
duke@1 | 437 | |
duke@1 | 438 | Pair<JavaFileObject,Integer> coords = new Pair<JavaFileObject,Integer>(file, pos); |
duke@1 | 439 | boolean shouldReport = !recorded.contains(coords); |
duke@1 | 440 | if (shouldReport) |
duke@1 | 441 | recorded.add(coords); |
duke@1 | 442 | return shouldReport; |
duke@1 | 443 | } |
duke@1 | 444 | |
duke@1 | 445 | /** Prompt user after an error. |
duke@1 | 446 | */ |
duke@1 | 447 | public void prompt() { |
duke@1 | 448 | if (promptOnError) { |
jjg@604 | 449 | System.err.println(localize("resume.abort")); |
duke@1 | 450 | try { |
duke@1 | 451 | while (true) { |
duke@1 | 452 | switch (System.in.read()) { |
duke@1 | 453 | case 'a': case 'A': |
duke@1 | 454 | System.exit(-1); |
duke@1 | 455 | return; |
duke@1 | 456 | case 'r': case 'R': |
duke@1 | 457 | return; |
duke@1 | 458 | case 'x': case 'X': |
duke@1 | 459 | throw new AssertionError("user abort"); |
duke@1 | 460 | default: |
duke@1 | 461 | } |
duke@1 | 462 | } |
duke@1 | 463 | } catch (IOException e) {} |
duke@1 | 464 | } |
duke@1 | 465 | } |
duke@1 | 466 | |
duke@1 | 467 | /** Print the faulty source code line and point to the error. |
duke@1 | 468 | * @param pos Buffer index of the error position, must be on current line |
duke@1 | 469 | */ |
duke@1 | 470 | private void printErrLine(int pos, PrintWriter writer) { |
jjg@73 | 471 | String line = (source == null ? null : source.getLine(pos)); |
jjg@73 | 472 | if (line == null) |
duke@1 | 473 | return; |
mcimadamore@100 | 474 | int col = source.getColumnNumber(pos, false); |
duke@1 | 475 | |
jjg@1136 | 476 | printRawLines(writer, line); |
jjg@73 | 477 | for (int i = 0; i < col - 1; i++) { |
jjg@73 | 478 | writer.print((line.charAt(i) == '\t') ? "\t" : " "); |
duke@1 | 479 | } |
duke@1 | 480 | writer.println("^"); |
duke@1 | 481 | writer.flush(); |
duke@1 | 482 | } |
duke@1 | 483 | |
jjg@1136 | 484 | public void printNewline() { |
jjg@1136 | 485 | noticeWriter.println(); |
jjg@1136 | 486 | } |
jjg@1136 | 487 | |
jjg@1136 | 488 | public void printNewline(WriterKind wk) { |
jjg@1136 | 489 | getWriter(wk).println(); |
jjg@1136 | 490 | } |
jjg@1136 | 491 | |
jjg@1136 | 492 | public void printLines(String key, Object... args) { |
jjg@1136 | 493 | printRawLines(noticeWriter, localize(key, args)); |
jjg@1136 | 494 | } |
jjg@1136 | 495 | |
jjg@1136 | 496 | public void printLines(PrefixKind pk, String key, Object... args) { |
jjg@1136 | 497 | printRawLines(noticeWriter, localize(pk, key, args)); |
jjg@1136 | 498 | } |
jjg@1136 | 499 | |
jjg@1136 | 500 | public void printLines(WriterKind wk, String key, Object... args) { |
jjg@1136 | 501 | printRawLines(getWriter(wk), localize(key, args)); |
jjg@1136 | 502 | } |
jjg@1136 | 503 | |
jjg@1136 | 504 | public void printLines(WriterKind wk, PrefixKind pk, String key, Object... args) { |
jjg@1136 | 505 | printRawLines(getWriter(wk), localize(pk, key, args)); |
jjg@1135 | 506 | } |
jjg@1135 | 507 | |
jjg@1135 | 508 | /** Print the text of a message, translating newlines appropriately |
jjg@1135 | 509 | * for the platform. |
jjg@1135 | 510 | */ |
jjg@1136 | 511 | public void printRawLines(String msg) { |
jjg@1136 | 512 | printRawLines(noticeWriter, msg); |
jjg@1136 | 513 | } |
jjg@1136 | 514 | |
jjg@1136 | 515 | /** Print the text of a message, translating newlines appropriately |
jjg@1136 | 516 | * for the platform. |
jjg@1136 | 517 | */ |
jjg@1136 | 518 | public void printRawLines(WriterKind kind, String msg) { |
jjg@1136 | 519 | printRawLines(getWriter(kind), msg); |
jjg@1136 | 520 | } |
jjg@1136 | 521 | |
jjg@1136 | 522 | /** Print the text of a message, translating newlines appropriately |
jjg@1136 | 523 | * for the platform. |
jjg@1136 | 524 | */ |
jjg@1136 | 525 | public static void printRawLines(PrintWriter writer, String msg) { |
duke@1 | 526 | int nl; |
duke@1 | 527 | while ((nl = msg.indexOf('\n')) != -1) { |
duke@1 | 528 | writer.println(msg.substring(0, nl)); |
duke@1 | 529 | msg = msg.substring(nl+1); |
duke@1 | 530 | } |
duke@1 | 531 | if (msg.length() != 0) writer.println(msg); |
duke@1 | 532 | } |
duke@1 | 533 | |
jjg@909 | 534 | /** |
jjg@909 | 535 | * Print the localized text of a "verbose" message to the |
jjg@909 | 536 | * noticeWriter stream. |
jjg@909 | 537 | */ |
jjg@909 | 538 | public void printVerbose(String key, Object... args) { |
jjg@1136 | 539 | printRawLines(noticeWriter, localize("verbose." + key, args)); |
jjg@909 | 540 | } |
jjg@909 | 541 | |
jjg@73 | 542 | protected void directError(String key, Object... args) { |
jjg@1136 | 543 | printRawLines(errWriter, localize(key, args)); |
jjg@73 | 544 | errWriter.flush(); |
duke@1 | 545 | } |
duke@1 | 546 | |
duke@1 | 547 | /** Report a warning that cannot be suppressed. |
duke@1 | 548 | * @param pos The source position at which to report the warning. |
duke@1 | 549 | * @param key The key for the localized warning message. |
duke@1 | 550 | * @param args Fields of the warning message. |
duke@1 | 551 | */ |
duke@1 | 552 | public void strictWarning(DiagnosticPosition pos, String key, Object ... args) { |
duke@1 | 553 | writeDiagnostic(diags.warning(source, pos, key, args)); |
duke@1 | 554 | nwarnings++; |
duke@1 | 555 | } |
duke@1 | 556 | |
jjg@1406 | 557 | /** |
jjg@1406 | 558 | * Primary method to report a diagnostic. |
jjg@1406 | 559 | * @param diagnostic |
jjg@1406 | 560 | */ |
jjg@1406 | 561 | public void report(JCDiagnostic diagnostic) { |
jjg@1406 | 562 | diagnosticHandler.report(diagnostic); |
jjg@1406 | 563 | } |
jjg@664 | 564 | |
duke@1 | 565 | /** |
duke@1 | 566 | * Common diagnostic handling. |
duke@1 | 567 | * The diagnostic is counted, and depending on the options and how many diagnostics have been |
duke@1 | 568 | * reported so far, the diagnostic may be handed off to writeDiagnostic. |
duke@1 | 569 | */ |
jjg@1406 | 570 | private class DefaultDiagnosticHandler extends DiagnosticHandler { |
jjg@1406 | 571 | public void report(JCDiagnostic diagnostic) { |
jjg@1406 | 572 | if (expectDiagKeys != null) |
jjg@1406 | 573 | expectDiagKeys.remove(diagnostic.getCode()); |
jjg@664 | 574 | |
jjg@1406 | 575 | switch (diagnostic.getType()) { |
jjg@1406 | 576 | case FRAGMENT: |
jjg@1406 | 577 | throw new IllegalArgumentException(); |
jjg@194 | 578 | |
jjg@1406 | 579 | case NOTE: |
jjg@1406 | 580 | // Print out notes only when we are permitted to report warnings |
jjg@1406 | 581 | // Notes are only generated at the end of a compilation, so should be small |
jjg@1406 | 582 | // in number. |
jjg@1406 | 583 | if ((emitWarnings || diagnostic.isMandatory()) && !suppressNotes) { |
jjg@1406 | 584 | writeDiagnostic(diagnostic); |
jjg@1406 | 585 | } |
jjg@1406 | 586 | break; |
duke@1 | 587 | |
jjg@1406 | 588 | case WARNING: |
jjg@1406 | 589 | if (emitWarnings || diagnostic.isMandatory()) { |
jjg@1406 | 590 | if (nwarnings < MaxWarnings) { |
jjg@1406 | 591 | writeDiagnostic(diagnostic); |
jjg@1406 | 592 | nwarnings++; |
jjg@1406 | 593 | } |
jjg@1406 | 594 | } |
jjg@1406 | 595 | break; |
jjg@1406 | 596 | |
jjg@1406 | 597 | case ERROR: |
jjg@1406 | 598 | if (nerrors < MaxErrors |
jjg@1406 | 599 | && shouldReport(diagnostic.getSource(), diagnostic.getIntPosition())) { |
jjg@1406 | 600 | writeDiagnostic(diagnostic); |
jjg@1406 | 601 | nerrors++; |
jjg@1406 | 602 | } |
jjg@1406 | 603 | break; |
duke@1 | 604 | } |
mcimadamore@1759 | 605 | if (diagnostic.isFlagSet(JCDiagnostic.DiagnosticFlag.COMPRESSED)) { |
mcimadamore@1759 | 606 | compressedOutput = true; |
mcimadamore@1759 | 607 | } |
duke@1 | 608 | } |
duke@1 | 609 | } |
duke@1 | 610 | |
duke@1 | 611 | /** |
duke@1 | 612 | * Write out a diagnostic. |
duke@1 | 613 | */ |
duke@1 | 614 | protected void writeDiagnostic(JCDiagnostic diag) { |
duke@1 | 615 | if (diagListener != null) { |
jjg@946 | 616 | diagListener.report(diag); |
jjg@946 | 617 | return; |
duke@1 | 618 | } |
duke@1 | 619 | |
duke@1 | 620 | PrintWriter writer = getWriterForDiagnosticType(diag.getType()); |
duke@1 | 621 | |
jjg@1136 | 622 | printRawLines(writer, diagFormatter.format(diag, messages.getCurrentLocale())); |
duke@1 | 623 | |
duke@1 | 624 | if (promptOnError) { |
duke@1 | 625 | switch (diag.getType()) { |
duke@1 | 626 | case ERROR: |
duke@1 | 627 | case WARNING: |
duke@1 | 628 | prompt(); |
duke@1 | 629 | } |
duke@1 | 630 | } |
duke@1 | 631 | |
duke@1 | 632 | if (dumpOnError) |
duke@1 | 633 | new RuntimeException().printStackTrace(writer); |
duke@1 | 634 | |
duke@1 | 635 | writer.flush(); |
duke@1 | 636 | } |
duke@1 | 637 | |
duke@1 | 638 | @Deprecated |
duke@1 | 639 | protected PrintWriter getWriterForDiagnosticType(DiagnosticType dt) { |
duke@1 | 640 | switch (dt) { |
duke@1 | 641 | case FRAGMENT: |
duke@1 | 642 | throw new IllegalArgumentException(); |
duke@1 | 643 | |
duke@1 | 644 | case NOTE: |
duke@1 | 645 | return noticeWriter; |
duke@1 | 646 | |
duke@1 | 647 | case WARNING: |
duke@1 | 648 | return warnWriter; |
duke@1 | 649 | |
duke@1 | 650 | case ERROR: |
duke@1 | 651 | return errWriter; |
duke@1 | 652 | |
duke@1 | 653 | default: |
duke@1 | 654 | throw new Error(); |
duke@1 | 655 | } |
duke@1 | 656 | } |
duke@1 | 657 | |
duke@1 | 658 | /** Find a localized string in the resource bundle. |
jjg@604 | 659 | * Because this method is static, it ignores the locale. |
jjg@604 | 660 | * Use localize(key, args) when possible. |
duke@1 | 661 | * @param key The key for the localized string. |
duke@1 | 662 | * @param args Fields to substitute into the string. |
duke@1 | 663 | */ |
duke@1 | 664 | public static String getLocalizedString(String key, Object ... args) { |
jjg@1136 | 665 | return JavacMessages.getDefaultLocalizedString(PrefixKind.COMPILER_MISC.key(key), args); |
duke@1 | 666 | } |
duke@1 | 667 | |
jjg@604 | 668 | /** Find a localized string in the resource bundle. |
jjg@604 | 669 | * @param key The key for the localized string. |
jjg@604 | 670 | * @param args Fields to substitute into the string. |
jjg@604 | 671 | */ |
jjg@604 | 672 | public String localize(String key, Object... args) { |
jjg@1136 | 673 | return localize(PrefixKind.COMPILER_MISC, key, args); |
jjg@604 | 674 | } |
jjg@604 | 675 | |
jjg@1136 | 676 | /** Find a localized string in the resource bundle. |
jjg@1136 | 677 | * @param key The key for the localized string. |
jjg@1136 | 678 | * @param args Fields to substitute into the string. |
jjg@1136 | 679 | */ |
jjg@1136 | 680 | public String localize(PrefixKind pk, String key, Object... args) { |
jjg@1136 | 681 | if (useRawMessages) |
jjg@1136 | 682 | return pk.key(key); |
jjg@1136 | 683 | else |
jjg@1136 | 684 | return messages.getLocalizedString(pk.key(key), args); |
jjg@1136 | 685 | } |
jjg@1136 | 686 | // where |
jjg@1136 | 687 | // backdoor hook for testing, should transition to use -XDrawDiagnostics |
jjg@1136 | 688 | private static boolean useRawMessages = false; |
jjg@1136 | 689 | |
duke@1 | 690 | /*************************************************************************** |
duke@1 | 691 | * raw error messages without internationalization; used for experimentation |
duke@1 | 692 | * and quick prototyping |
duke@1 | 693 | ***************************************************************************/ |
duke@1 | 694 | |
jjg@73 | 695 | /** print an error or warning message: |
jjg@73 | 696 | */ |
duke@1 | 697 | private void printRawError(int pos, String msg) { |
jjg@73 | 698 | if (source == null || pos == Position.NOPOS) { |
jjg@1136 | 699 | printRawLines(errWriter, "error: " + msg); |
duke@1 | 700 | } else { |
jjg@73 | 701 | int line = source.getLineNumber(pos); |
mcimadamore@168 | 702 | JavaFileObject file = source.getFile(); |
duke@1 | 703 | if (file != null) |
jjg@1136 | 704 | printRawLines(errWriter, |
jjg@415 | 705 | file.getName() + ":" + |
duke@1 | 706 | line + ": " + msg); |
duke@1 | 707 | printErrLine(pos, errWriter); |
duke@1 | 708 | } |
duke@1 | 709 | errWriter.flush(); |
duke@1 | 710 | } |
duke@1 | 711 | |
jjg@73 | 712 | /** report an error: |
jjg@73 | 713 | */ |
duke@1 | 714 | public void rawError(int pos, String msg) { |
mcimadamore@168 | 715 | if (nerrors < MaxErrors && shouldReport(currentSourceFile(), pos)) { |
duke@1 | 716 | printRawError(pos, msg); |
duke@1 | 717 | prompt(); |
duke@1 | 718 | nerrors++; |
duke@1 | 719 | } |
duke@1 | 720 | errWriter.flush(); |
duke@1 | 721 | } |
duke@1 | 722 | |
jjg@73 | 723 | /** report a warning: |
jjg@73 | 724 | */ |
duke@1 | 725 | public void rawWarning(int pos, String msg) { |
duke@1 | 726 | if (nwarnings < MaxWarnings && emitWarnings) { |
duke@1 | 727 | printRawError(pos, "warning: " + msg); |
duke@1 | 728 | } |
duke@1 | 729 | prompt(); |
duke@1 | 730 | nwarnings++; |
duke@1 | 731 | errWriter.flush(); |
duke@1 | 732 | } |
duke@1 | 733 | |
duke@1 | 734 | public static String format(String fmt, Object... args) { |
duke@1 | 735 | return String.format((java.util.Locale)null, fmt, args); |
duke@1 | 736 | } |
duke@1 | 737 | |
duke@1 | 738 | } |