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

Thu, 10 Jun 2010 16:08:01 -0700

author
jjg
date
Thu, 10 Jun 2010 16:08:01 -0700
changeset 581
f2fdd52e4e87
parent 554
9d9f26857129
child 612
d1bd93028447
permissions
-rw-r--r--

6944312: Potential rebranding issues in openjdk/langtools repository sources
Reviewed-by: darcy

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

mercurial