src/share/classes/com/sun/tools/javac/util/JCDiagnostic.java

Thu, 29 Jan 2009 12:19:14 +0000

author
mcimadamore
date
Thu, 29 Jan 2009 12:19:14 +0000
changeset 212
79f2f2c7d846
parent 168
4cdaaf4c5dca
child 302
18e0269f25e3
permissions
-rw-r--r--

6729401: Compiler error when using F-bounded generics with free type variables
Summary: Javac applies wrong substitution to recursive type-variable bounds
Reviewed-by: jjg

duke@1 1 /*
xdono@54 2 * Copyright 2003-2008 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.util.Locale;
duke@1 29 import java.util.Map;
duke@1 30
duke@1 31 import javax.tools.Diagnostic;
duke@1 32 import javax.tools.JavaFileObject;
duke@1 33
mcimadamore@83 34 import com.sun.tools.javac.api.DiagnosticFormatter;
jjg@50 35 import com.sun.tools.javac.file.JavacFileManager;
duke@1 36 import com.sun.tools.javac.tree.JCTree;
duke@1 37
duke@1 38 import static com.sun.tools.javac.util.JCDiagnostic.DiagnosticType.*;
duke@1 39
duke@1 40 /** An abstraction of a diagnostic message generated by the compiler.
duke@1 41 *
duke@1 42 * <p><b>This is NOT part of any API supported by Sun Microsystems. If
duke@1 43 * you write code that depends on this, you do so at your own risk.
duke@1 44 * This code and its internal interfaces are subject to change or
duke@1 45 * deletion without notice.</b>
duke@1 46 */
duke@1 47 public class JCDiagnostic implements Diagnostic<JavaFileObject> {
duke@1 48 /** A factory for creating diagnostic objects. */
duke@1 49 public static class Factory {
duke@1 50 /** The context key for the diagnostic factory. */
duke@1 51 protected static final Context.Key<JCDiagnostic.Factory> diagnosticFactoryKey =
duke@1 52 new Context.Key<JCDiagnostic.Factory>();
duke@1 53
duke@1 54 /** Get the Factory instance for this context. */
duke@1 55 public static Factory instance(Context context) {
duke@1 56 Factory instance = context.get(diagnosticFactoryKey);
duke@1 57 if (instance == null)
duke@1 58 instance = new Factory(context);
duke@1 59 return instance;
duke@1 60 }
duke@1 61
mcimadamore@89 62 DiagnosticFormatter<JCDiagnostic> formatter;
duke@1 63 final String prefix;
duke@1 64
duke@1 65 /** Create a new diagnostic factory. */
duke@1 66 protected Factory(Context context) {
mcimadamore@136 67 this(JavacMessages.instance(context), "compiler");
duke@1 68 context.put(diagnosticFactoryKey, this);
duke@1 69 }
duke@1 70
duke@1 71 /** Create a new diagnostic factory. */
mcimadamore@136 72 public Factory(JavacMessages messages, String prefix) {
duke@1 73 this.prefix = prefix;
mcimadamore@89 74 this.formatter = new BasicDiagnosticFormatter(messages);
duke@1 75 }
duke@1 76
duke@1 77 /**
duke@1 78 * Create an error diagnostic.
duke@1 79 * @param source The source of the compilation unit, if any, in which to report the error.
duke@1 80 * @param pos The source position at which to report the error.
duke@1 81 * @param key The key for the localized error message.
duke@1 82 * @param args Fields of the error message.
duke@1 83 */
duke@1 84 public JCDiagnostic error(
duke@1 85 DiagnosticSource source, DiagnosticPosition pos, String key, Object... args) {
mcimadamore@89 86 return new JCDiagnostic(formatter, ERROR, true, source, pos, qualify(ERROR, key), args);
duke@1 87 }
duke@1 88
duke@1 89 /**
duke@1 90 * Create a warning diagnostic that will not be hidden by the -nowarn or -Xlint:none options.
duke@1 91 * @param source The source of the compilation unit, if any, in which to report the warning.
duke@1 92 * @param pos The source position at which to report the warning.
duke@1 93 * @param key The key for the localized error message.
duke@1 94 * @param args Fields of the error message.
duke@1 95 * @see MandatoryWarningHandler
duke@1 96 */
duke@1 97 public JCDiagnostic mandatoryWarning(
duke@1 98 DiagnosticSource source, DiagnosticPosition pos, String key, Object... args) {
mcimadamore@89 99 return new JCDiagnostic(formatter, WARNING, true, source, pos, qualify(WARNING, key), args);
duke@1 100 }
duke@1 101
duke@1 102 /**
duke@1 103 * Create a warning diagnostic.
duke@1 104 * @param source The source of the compilation unit, if any, in which to report the warning.
duke@1 105 * @param pos The source position at which to report the warning.
duke@1 106 * @param key The key for the localized error message.
duke@1 107 * @param args Fields of the error message.
duke@1 108 */
duke@1 109 public JCDiagnostic warning(
duke@1 110 DiagnosticSource source, DiagnosticPosition pos, String key, Object... args) {
mcimadamore@89 111 return new JCDiagnostic(formatter, WARNING, false, source, pos, qualify(WARNING, key), args);
duke@1 112 }
duke@1 113
duke@1 114 /**
duke@1 115 * Create a note diagnostic that will not be hidden by the -nowarn or -Xlint:none options.
duke@1 116 * @param key The key for the localized error message.
duke@1 117 * @param args Fields of the error message.
duke@1 118 * @see MandatoryWarningHandler
duke@1 119 */
duke@1 120 public JCDiagnostic mandatoryNote(DiagnosticSource source, String key, Object... args) {
mcimadamore@89 121 return new JCDiagnostic(formatter, NOTE, true, source, null, qualify(NOTE, key), args);
duke@1 122 }
duke@1 123
duke@1 124 /**
duke@1 125 * Create a note diagnostic.
duke@1 126 * @param key The key for the localized error message.
duke@1 127 * @param args Fields of the error message.
duke@1 128 */
duke@1 129 public JCDiagnostic note(String key, Object... args) {
duke@1 130 return note(null, null, key, args);
duke@1 131 }
duke@1 132
duke@1 133 /**
duke@1 134 * Create a note diagnostic.
duke@1 135 * @param source The source of the compilation unit, if any, in which to report the note.
duke@1 136 * @param pos The source position at which to report the note.
duke@1 137 * @param key The key for the localized error message.
duke@1 138 * @param args Fields of the error message.
duke@1 139 */
duke@1 140 public JCDiagnostic note(
duke@1 141 DiagnosticSource source, DiagnosticPosition pos, String key, Object... args) {
mcimadamore@89 142 return new JCDiagnostic(formatter, NOTE, false, source, pos, qualify(NOTE, key), args);
duke@1 143 }
duke@1 144
duke@1 145 /**
duke@1 146 * Create a fragment diagnostic, for use as an argument in other diagnostics
duke@1 147 * @param key The key for the localized error message.
duke@1 148 * @param args Fields of the error message.
duke@1 149 */
duke@1 150 public JCDiagnostic fragment(String key, Object... args) {
mcimadamore@89 151 return new JCDiagnostic(formatter, FRAGMENT, false, null, null, qualify(FRAGMENT, key), args);
duke@1 152 }
duke@1 153
duke@1 154 protected String qualify(DiagnosticType t, String key) {
duke@1 155 return prefix + "." + t.key + "." + key;
duke@1 156 }
duke@1 157 }
duke@1 158
duke@1 159
duke@1 160
duke@1 161 /**
duke@1 162 * Create a fragment diagnostic, for use as an argument in other diagnostics
duke@1 163 * @param key The key for the localized error message.
duke@1 164 * @param args Fields of the error message.
mcimadamore@89 165 *
duke@1 166 */
mcimadamore@89 167 @Deprecated
duke@1 168 public static JCDiagnostic fragment(String key, Object... args) {
mcimadamore@89 169 return new JCDiagnostic(getFragmentFormatter(),
duke@1 170 FRAGMENT,
duke@1 171 false,
duke@1 172 null,
duke@1 173 null,
duke@1 174 "compiler." + FRAGMENT.key + "." + key,
duke@1 175 args);
duke@1 176 }
mcimadamore@89 177 //where
mcimadamore@89 178 @Deprecated
mcimadamore@89 179 public static DiagnosticFormatter<JCDiagnostic> getFragmentFormatter() {
mcimadamore@89 180 if (fragmentFormatter == null) {
mcimadamore@136 181 fragmentFormatter = new BasicDiagnosticFormatter(JavacMessages.getDefaultMessages());
mcimadamore@89 182 }
mcimadamore@89 183 return fragmentFormatter;
mcimadamore@89 184 }
duke@1 185
duke@1 186 /**
duke@1 187 * A DiagnosticType defines the type of the diagnostic.
duke@1 188 **/
duke@1 189 public enum DiagnosticType {
duke@1 190 /** A fragment of an enclosing diagnostic. */
duke@1 191 FRAGMENT("misc"),
duke@1 192 /** A note: similar to, but less serious than, a warning. */
duke@1 193 NOTE("note"),
duke@1 194 /** A warning. */
duke@1 195 WARNING("warn"),
duke@1 196 /** An error. */
duke@1 197 ERROR("err");
duke@1 198
duke@1 199 final String key;
duke@1 200
duke@1 201 /** Create a DiagnosticType.
duke@1 202 * @param key A string used to create the resource key for the diagnostic.
duke@1 203 */
duke@1 204 DiagnosticType(String key) {
duke@1 205 this.key = key;
duke@1 206 }
duke@1 207 };
duke@1 208
duke@1 209 /**
duke@1 210 * A DiagnosticPosition provides information about the positions in a file
duke@1 211 * that gave rise to a diagnostic. It always defines a "preferred" position
duke@1 212 * that most accurately defines the location of the diagnostic, it may also
duke@1 213 * provide a related tree node that spans that location.
duke@1 214 */
duke@1 215 public static interface DiagnosticPosition {
duke@1 216 /** Gets the tree node, if any, to which the diagnostic applies. */
duke@1 217 JCTree getTree();
duke@1 218 /** If there is a tree node, get the start position of the tree node.
duke@1 219 * Otherwise, just returns the same as getPreferredPosition(). */
duke@1 220 int getStartPosition();
duke@1 221 /** Get the position within the file that most accurately defines the
duke@1 222 * location for the diagnostic. */
duke@1 223 int getPreferredPosition();
duke@1 224 /** If there is a tree node, and if endPositions are available, get
duke@1 225 * the end position of the tree node. Otherwise, just returns the
duke@1 226 * same as getPreferredPosition(). */
duke@1 227 int getEndPosition(Map<JCTree, Integer> endPosTable);
duke@1 228 }
duke@1 229
duke@1 230 /**
duke@1 231 * A DiagnosticPosition that simply identifies a position, but no related
duke@1 232 * tree node, as the location for a diagnostic. Used for scanner and parser
duke@1 233 * diagnostics. */
duke@1 234 public static class SimpleDiagnosticPosition implements DiagnosticPosition {
duke@1 235 public SimpleDiagnosticPosition(int pos) {
duke@1 236 this.pos = pos;
duke@1 237 }
duke@1 238
duke@1 239 public JCTree getTree() {
duke@1 240 return null;
duke@1 241 }
duke@1 242
duke@1 243 public int getStartPosition() {
duke@1 244 return pos;
duke@1 245 }
duke@1 246
duke@1 247 public int getPreferredPosition() {
duke@1 248 return pos;
duke@1 249 }
duke@1 250
duke@1 251 public int getEndPosition(Map<JCTree, Integer> endPosTable) {
duke@1 252 return pos;
duke@1 253 }
duke@1 254
duke@1 255 private final int pos;
duke@1 256 }
duke@1 257
duke@1 258 private final DiagnosticType type;
duke@1 259 private final DiagnosticSource source;
duke@1 260 private final DiagnosticPosition position;
duke@1 261 private final int line;
duke@1 262 private final int column;
duke@1 263 private final String key;
mcimadamore@83 264 protected Object[] args;
duke@1 265 private boolean mandatory;
duke@1 266
duke@1 267 /**
duke@1 268 * Create a diagnostic object.
duke@1 269 * @param messages the resource for localized messages
duke@1 270 * @param dt the type of diagnostic
duke@1 271 * @param name the name of the source file, or null if none.
duke@1 272 * @param pos the character offset within the source file, if given.
duke@1 273 * @param key a resource key to identify the text of the diagnostic
duke@1 274 * @param args arguments to be included in the text of the diagnostic
duke@1 275 */
mcimadamore@89 276 protected JCDiagnostic(DiagnosticFormatter<JCDiagnostic> formatter,
duke@1 277 DiagnosticType dt,
duke@1 278 boolean mandatory,
duke@1 279 DiagnosticSource source,
duke@1 280 DiagnosticPosition pos,
duke@1 281 String key,
duke@1 282 Object ... args) {
duke@1 283 if (source == null && pos != null && pos.getPreferredPosition() != Position.NOPOS)
duke@1 284 throw new IllegalArgumentException();
duke@1 285
mcimadamore@89 286 this.defaultFormatter = formatter;
duke@1 287 this.type = dt;
duke@1 288 this.mandatory = mandatory;
duke@1 289 this.source = source;
duke@1 290 this.position = pos;
duke@1 291 this.key = key;
mcimadamore@168 292 this.args = args;
duke@1 293
duke@1 294 int n = (pos == null ? Position.NOPOS : pos.getPreferredPosition());
duke@1 295 if (n == Position.NOPOS || source == null)
duke@1 296 line = column = -1;
duke@1 297 else {
duke@1 298 line = source.getLineNumber(n);
mcimadamore@100 299 column = source.getColumnNumber(n, true);
duke@1 300 }
duke@1 301 }
duke@1 302
duke@1 303 /**
duke@1 304 * Get the type of this diagnostic.
duke@1 305 * @return the type of this diagnostic
duke@1 306 */
duke@1 307 public DiagnosticType getType() {
duke@1 308 return type;
duke@1 309 }
duke@1 310
duke@1 311 /**
mcimadamore@168 312 * Get the subdiagnostic list
mcimadamore@168 313 * @return subdiagnostic list
mcimadamore@168 314 */
mcimadamore@168 315 public List<JCDiagnostic> getSubdiagnostics() {
mcimadamore@168 316 return List.nil();
mcimadamore@168 317 }
mcimadamore@168 318
mcimadamore@168 319 public boolean isMultiline() {
mcimadamore@168 320 return false;
mcimadamore@168 321 }
mcimadamore@168 322
mcimadamore@168 323 /**
duke@1 324 * Check whether or not this diagnostic is required to be shown.
duke@1 325 * @return true if this diagnostic is required to be shown.
duke@1 326 */
duke@1 327 public boolean isMandatory() {
duke@1 328 return mandatory;
duke@1 329 }
duke@1 330
duke@1 331 /**
duke@1 332 * Get the name of the source file referred to by this diagnostic.
duke@1 333 * @return the name of the source referred to with this diagnostic, or null if none
duke@1 334 */
duke@1 335 public JavaFileObject getSource() {
duke@1 336 if (source == null)
duke@1 337 return null;
duke@1 338 else
duke@1 339 return source.getFile();
duke@1 340 }
duke@1 341
duke@1 342 /**
duke@1 343 * Get the name of the source file referred to by this diagnostic.
duke@1 344 * @return the name of the source referred to with this diagnostic, or null if none
duke@1 345 */
duke@1 346 public String getSourceName() {
duke@1 347 JavaFileObject s = getSource();
duke@1 348 return s == null ? null : JavacFileManager.getJavacFileName(s);
duke@1 349 }
duke@1 350
duke@1 351 /**
duke@1 352 * Get the source referred to by this diagnostic.
duke@1 353 * @return the source referred to with this diagnostic, or null if none
duke@1 354 */
duke@1 355 public DiagnosticSource getDiagnosticSource() {
duke@1 356 return source;
duke@1 357 }
duke@1 358
duke@1 359 protected int getIntStartPosition() {
duke@1 360 return (position == null ? Position.NOPOS : position.getStartPosition());
duke@1 361 }
duke@1 362
duke@1 363 protected int getIntPosition() {
duke@1 364 return (position == null ? Position.NOPOS : position.getPreferredPosition());
duke@1 365 }
duke@1 366
duke@1 367 protected int getIntEndPosition() {
duke@1 368 return (position == null ? Position.NOPOS : position.getEndPosition(source.getEndPosTable()));
duke@1 369 }
duke@1 370
duke@1 371 public long getStartPosition() {
duke@1 372 return getIntStartPosition();
duke@1 373 }
duke@1 374
duke@1 375 public long getPosition() {
duke@1 376 return getIntPosition();
duke@1 377 }
duke@1 378
duke@1 379 public long getEndPosition() {
duke@1 380 return getIntEndPosition();
duke@1 381 }
duke@1 382
duke@1 383 /**
duke@1 384 * Get the line number within the source referred to by this diagnostic.
duke@1 385 * @return the line number within the source referred to by this diagnostic
duke@1 386 */
duke@1 387 public long getLineNumber() {
duke@1 388 return line;
duke@1 389 }
duke@1 390
duke@1 391 /**
duke@1 392 * Get the column number within the line of source referred to by this diagnostic.
duke@1 393 * @return the column number within the line of source referred to by this diagnostic
duke@1 394 */
duke@1 395 public long getColumnNumber() {
duke@1 396 return column;
duke@1 397 }
duke@1 398
duke@1 399 /**
duke@1 400 * Get the arguments to be included in the text of the diagnostic.
duke@1 401 * @return the arguments to be included in the text of the diagnostic
duke@1 402 */
duke@1 403 public Object[] getArgs() {
duke@1 404 return args;
duke@1 405 }
duke@1 406
duke@1 407 /**
duke@1 408 * Get the prefix string associated with this type of diagnostic.
duke@1 409 * @return the prefix string associated with this type of diagnostic
duke@1 410 */
duke@1 411 public String getPrefix() {
duke@1 412 return getPrefix(type);
duke@1 413 }
duke@1 414
duke@1 415 /**
duke@1 416 * Get the prefix string associated with a particular type of diagnostic.
duke@1 417 * @return the prefix string associated with a particular type of diagnostic
duke@1 418 */
duke@1 419 public String getPrefix(DiagnosticType dt) {
mcimadamore@89 420 return defaultFormatter.formatKind(this, Locale.getDefault());
mcimadamore@83 421 }
mcimadamore@83 422
duke@1 423 /**
duke@1 424 * Return the standard presentation of this diagnostic.
duke@1 425 */
duke@1 426 public String toString() {
mcimadamore@89 427 return defaultFormatter.format(this,Locale.getDefault());
duke@1 428 }
duke@1 429
mcimadamore@89 430 private DiagnosticFormatter<JCDiagnostic> defaultFormatter;
mcimadamore@89 431 @Deprecated
mcimadamore@89 432 private static DiagnosticFormatter<JCDiagnostic> fragmentFormatter;
duke@1 433
duke@1 434 // Methods for javax.tools.Diagnostic
duke@1 435
duke@1 436 public Diagnostic.Kind getKind() {
duke@1 437 switch (type) {
duke@1 438 case NOTE:
duke@1 439 return Diagnostic.Kind.NOTE;
duke@1 440 case WARNING:
duke@1 441 return mandatory ? Diagnostic.Kind.MANDATORY_WARNING
duke@1 442 : Diagnostic.Kind.WARNING;
duke@1 443 case ERROR:
duke@1 444 return Diagnostic.Kind.ERROR;
duke@1 445 default:
duke@1 446 return Diagnostic.Kind.OTHER;
duke@1 447 }
duke@1 448 }
duke@1 449
duke@1 450 public String getCode() {
duke@1 451 return key;
duke@1 452 }
duke@1 453
duke@1 454 public String getMessage(Locale locale) {
mcimadamore@89 455 return defaultFormatter.formatMessage(this, locale);
duke@1 456 }
mcimadamore@168 457
mcimadamore@168 458 public static class MultilineDiagnostic extends JCDiagnostic {
mcimadamore@168 459
mcimadamore@168 460 private final List<JCDiagnostic> subdiagnostics;
mcimadamore@168 461
mcimadamore@168 462 public MultilineDiagnostic(JCDiagnostic other, List<JCDiagnostic> subdiagnostics) {
mcimadamore@168 463 super(other.defaultFormatter,
mcimadamore@168 464 other.getType(),
mcimadamore@168 465 other.isMandatory(),
mcimadamore@168 466 other.getDiagnosticSource(),
mcimadamore@168 467 other.position,
mcimadamore@168 468 other.getCode(),
mcimadamore@168 469 other.getArgs());
mcimadamore@168 470 this.subdiagnostics = subdiagnostics;
mcimadamore@168 471 }
mcimadamore@168 472
mcimadamore@168 473 @Override
mcimadamore@168 474 public List<JCDiagnostic> getSubdiagnostics() {
mcimadamore@168 475 return subdiagnostics;
mcimadamore@168 476 }
mcimadamore@168 477
mcimadamore@168 478 @Override
mcimadamore@168 479 public boolean isMultiline() {
mcimadamore@168 480 return true;
mcimadamore@168 481 }
mcimadamore@168 482 }
duke@1 483 }

mercurial