Sat, 07 Nov 2020 10:30:02 +0800
Added tag mips-jdk8u275-b01 for changeset eb6ee6a5f2fe
aoqi@0 | 1 | /* |
aoqi@0 | 2 | * Copyright (c) 2003, 2013, Oracle and/or its affiliates. All rights reserved. |
aoqi@0 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
aoqi@0 | 4 | * |
aoqi@0 | 5 | * This code is free software; you can redistribute it and/or modify it |
aoqi@0 | 6 | * under the terms of the GNU General Public License version 2 only, as |
aoqi@0 | 7 | * published by the Free Software Foundation. Oracle designates this |
aoqi@0 | 8 | * particular file as subject to the "Classpath" exception as provided |
aoqi@0 | 9 | * by Oracle in the LICENSE file that accompanied this code. |
aoqi@0 | 10 | * |
aoqi@0 | 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
aoqi@0 | 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
aoqi@0 | 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
aoqi@0 | 14 | * version 2 for more details (a copy is included in the LICENSE file that |
aoqi@0 | 15 | * accompanied this code). |
aoqi@0 | 16 | * |
aoqi@0 | 17 | * You should have received a copy of the GNU General Public License version |
aoqi@0 | 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
aoqi@0 | 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
aoqi@0 | 20 | * |
aoqi@0 | 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
aoqi@0 | 22 | * or visit www.oracle.com if you need additional information or have any |
aoqi@0 | 23 | * questions. |
aoqi@0 | 24 | */ |
aoqi@0 | 25 | |
aoqi@0 | 26 | package com.sun.tools.javac.util; |
aoqi@0 | 27 | |
aoqi@0 | 28 | import java.util.EnumSet; |
aoqi@0 | 29 | import java.util.Locale; |
aoqi@0 | 30 | import java.util.Set; |
aoqi@0 | 31 | |
aoqi@0 | 32 | import javax.tools.Diagnostic; |
aoqi@0 | 33 | import javax.tools.JavaFileObject; |
aoqi@0 | 34 | |
aoqi@0 | 35 | import com.sun.tools.javac.api.DiagnosticFormatter; |
aoqi@0 | 36 | import com.sun.tools.javac.code.Lint.LintCategory; |
aoqi@0 | 37 | import com.sun.tools.javac.tree.EndPosTable; |
aoqi@0 | 38 | import com.sun.tools.javac.tree.JCTree; |
aoqi@0 | 39 | |
aoqi@0 | 40 | import static com.sun.tools.javac.util.JCDiagnostic.DiagnosticType.*; |
aoqi@0 | 41 | |
aoqi@0 | 42 | /** An abstraction of a diagnostic message generated by the compiler. |
aoqi@0 | 43 | * |
aoqi@0 | 44 | * <p><b>This is NOT part of any supported API. |
aoqi@0 | 45 | * If you write code that depends on this, you do so at your own risk. |
aoqi@0 | 46 | * This code and its internal interfaces are subject to change or |
aoqi@0 | 47 | * deletion without notice.</b> |
aoqi@0 | 48 | */ |
aoqi@0 | 49 | public class JCDiagnostic implements Diagnostic<JavaFileObject> { |
aoqi@0 | 50 | /** A factory for creating diagnostic objects. */ |
aoqi@0 | 51 | public static class Factory { |
aoqi@0 | 52 | /** The context key for the diagnostic factory. */ |
aoqi@0 | 53 | protected static final Context.Key<JCDiagnostic.Factory> diagnosticFactoryKey = |
aoqi@0 | 54 | new Context.Key<JCDiagnostic.Factory>(); |
aoqi@0 | 55 | |
aoqi@0 | 56 | /** Get the Factory instance for this context. */ |
aoqi@0 | 57 | public static Factory instance(Context context) { |
aoqi@0 | 58 | Factory instance = context.get(diagnosticFactoryKey); |
aoqi@0 | 59 | if (instance == null) |
aoqi@0 | 60 | instance = new Factory(context); |
aoqi@0 | 61 | return instance; |
aoqi@0 | 62 | } |
aoqi@0 | 63 | |
aoqi@0 | 64 | DiagnosticFormatter<JCDiagnostic> formatter; |
aoqi@0 | 65 | final String prefix; |
aoqi@0 | 66 | final Set<DiagnosticFlag> defaultErrorFlags; |
aoqi@0 | 67 | |
aoqi@0 | 68 | /** Create a new diagnostic factory. */ |
aoqi@0 | 69 | protected Factory(Context context) { |
aoqi@0 | 70 | this(JavacMessages.instance(context), "compiler"); |
aoqi@0 | 71 | context.put(diagnosticFactoryKey, this); |
aoqi@0 | 72 | |
aoqi@0 | 73 | final Options options = Options.instance(context); |
aoqi@0 | 74 | initOptions(options); |
aoqi@0 | 75 | options.addListener(new Runnable() { |
aoqi@0 | 76 | public void run() { |
aoqi@0 | 77 | initOptions(options); |
aoqi@0 | 78 | } |
aoqi@0 | 79 | }); |
aoqi@0 | 80 | } |
aoqi@0 | 81 | |
aoqi@0 | 82 | private void initOptions(Options options) { |
aoqi@0 | 83 | if (options.isSet("onlySyntaxErrorsUnrecoverable")) |
aoqi@0 | 84 | defaultErrorFlags.add(DiagnosticFlag.RECOVERABLE); |
aoqi@0 | 85 | } |
aoqi@0 | 86 | |
aoqi@0 | 87 | /** Create a new diagnostic factory. */ |
aoqi@0 | 88 | public Factory(JavacMessages messages, String prefix) { |
aoqi@0 | 89 | this.prefix = prefix; |
aoqi@0 | 90 | this.formatter = new BasicDiagnosticFormatter(messages); |
aoqi@0 | 91 | defaultErrorFlags = EnumSet.of(DiagnosticFlag.MANDATORY); |
aoqi@0 | 92 | } |
aoqi@0 | 93 | |
aoqi@0 | 94 | /** |
aoqi@0 | 95 | * Create an error diagnostic. |
aoqi@0 | 96 | * @param source The source of the compilation unit, if any, in which to report the error. |
aoqi@0 | 97 | * @param pos The source position at which to report the error. |
aoqi@0 | 98 | * @param key The key for the localized error message. |
aoqi@0 | 99 | * @param args Fields of the error message. |
aoqi@0 | 100 | */ |
aoqi@0 | 101 | public JCDiagnostic error( |
aoqi@0 | 102 | DiagnosticSource source, DiagnosticPosition pos, String key, Object... args) { |
aoqi@0 | 103 | return create(ERROR, null, defaultErrorFlags, source, pos, key, args); |
aoqi@0 | 104 | } |
aoqi@0 | 105 | |
aoqi@0 | 106 | /** |
aoqi@0 | 107 | * Create a warning diagnostic that will not be hidden by the -nowarn or -Xlint:none options. |
aoqi@0 | 108 | * @param source The source of the compilation unit, if any, in which to report the warning. |
aoqi@0 | 109 | * @param pos The source position at which to report the warning. |
aoqi@0 | 110 | * @param key The key for the localized warning message. |
aoqi@0 | 111 | * @param args Fields of the warning message. |
aoqi@0 | 112 | * @see MandatoryWarningHandler |
aoqi@0 | 113 | */ |
aoqi@0 | 114 | public JCDiagnostic mandatoryWarning( |
aoqi@0 | 115 | DiagnosticSource source, DiagnosticPosition pos, String key, Object... args) { |
aoqi@0 | 116 | return create(WARNING, null, EnumSet.of(DiagnosticFlag.MANDATORY), source, pos, key, args); |
aoqi@0 | 117 | } |
aoqi@0 | 118 | |
aoqi@0 | 119 | /** |
aoqi@0 | 120 | * Create a warning diagnostic that will not be hidden by the -nowarn or -Xlint:none options. |
aoqi@0 | 121 | * @param lc The lint category for the diagnostic |
aoqi@0 | 122 | * @param source The source of the compilation unit, if any, in which to report the warning. |
aoqi@0 | 123 | * @param pos The source position at which to report the warning. |
aoqi@0 | 124 | * @param key The key for the localized warning message. |
aoqi@0 | 125 | * @param args Fields of the warning message. |
aoqi@0 | 126 | * @see MandatoryWarningHandler |
aoqi@0 | 127 | */ |
aoqi@0 | 128 | public JCDiagnostic mandatoryWarning( |
aoqi@0 | 129 | LintCategory lc, |
aoqi@0 | 130 | DiagnosticSource source, DiagnosticPosition pos, String key, Object... args) { |
aoqi@0 | 131 | return create(WARNING, lc, EnumSet.of(DiagnosticFlag.MANDATORY), source, pos, key, args); |
aoqi@0 | 132 | } |
aoqi@0 | 133 | |
aoqi@0 | 134 | /** |
aoqi@0 | 135 | * Create a warning diagnostic. |
aoqi@0 | 136 | * @param lc The lint category for the diagnostic |
aoqi@0 | 137 | * @param key The key for the localized error message. |
aoqi@0 | 138 | * @param args Fields of the warning message. |
aoqi@0 | 139 | * @see MandatoryWarningHandler |
aoqi@0 | 140 | */ |
aoqi@0 | 141 | public JCDiagnostic warning( |
aoqi@0 | 142 | LintCategory lc, String key, Object... args) { |
aoqi@0 | 143 | return create(WARNING, lc, EnumSet.noneOf(DiagnosticFlag.class), null, null, key, args); |
aoqi@0 | 144 | } |
aoqi@0 | 145 | |
aoqi@0 | 146 | /** |
aoqi@0 | 147 | * Create a warning diagnostic. |
aoqi@0 | 148 | * @param source The source of the compilation unit, if any, in which to report the warning. |
aoqi@0 | 149 | * @param pos The source position at which to report the warning. |
aoqi@0 | 150 | * @param key The key for the localized warning message. |
aoqi@0 | 151 | * @param args Fields of the warning message. |
aoqi@0 | 152 | */ |
aoqi@0 | 153 | public JCDiagnostic warning( |
aoqi@0 | 154 | DiagnosticSource source, DiagnosticPosition pos, String key, Object... args) { |
aoqi@0 | 155 | return create(WARNING, null, EnumSet.noneOf(DiagnosticFlag.class), source, pos, key, args); |
aoqi@0 | 156 | } |
aoqi@0 | 157 | |
aoqi@0 | 158 | /** |
aoqi@0 | 159 | * Create a warning diagnostic. |
aoqi@0 | 160 | * @param lc The lint category for the diagnostic |
aoqi@0 | 161 | * @param source The source of the compilation unit, if any, in which to report the warning. |
aoqi@0 | 162 | * @param pos The source position at which to report the warning. |
aoqi@0 | 163 | * @param key The key for the localized warning message. |
aoqi@0 | 164 | * @param args Fields of the warning message. |
aoqi@0 | 165 | * @see MandatoryWarningHandler |
aoqi@0 | 166 | */ |
aoqi@0 | 167 | public JCDiagnostic warning( |
aoqi@0 | 168 | LintCategory lc, DiagnosticSource source, DiagnosticPosition pos, String key, Object... args) { |
aoqi@0 | 169 | return create(WARNING, lc, EnumSet.noneOf(DiagnosticFlag.class), source, pos, key, args); |
aoqi@0 | 170 | } |
aoqi@0 | 171 | |
aoqi@0 | 172 | /** |
aoqi@0 | 173 | * Create a note diagnostic that will not be hidden by the -nowarn or -Xlint:none options. |
aoqi@0 | 174 | * @param key The key for the localized message. |
aoqi@0 | 175 | * @param args Fields of the message. |
aoqi@0 | 176 | * @see MandatoryWarningHandler |
aoqi@0 | 177 | */ |
aoqi@0 | 178 | public JCDiagnostic mandatoryNote(DiagnosticSource source, String key, Object... args) { |
aoqi@0 | 179 | return create(NOTE, null, EnumSet.of(DiagnosticFlag.MANDATORY), source, null, key, args); |
aoqi@0 | 180 | } |
aoqi@0 | 181 | |
aoqi@0 | 182 | /** |
aoqi@0 | 183 | * Create a note diagnostic. |
aoqi@0 | 184 | * @param key The key for the localized error message. |
aoqi@0 | 185 | * @param args Fields of the message. |
aoqi@0 | 186 | */ |
aoqi@0 | 187 | public JCDiagnostic note(String key, Object... args) { |
aoqi@0 | 188 | return create(NOTE, null, EnumSet.noneOf(DiagnosticFlag.class), null, null, key, args); |
aoqi@0 | 189 | } |
aoqi@0 | 190 | |
aoqi@0 | 191 | /** |
aoqi@0 | 192 | * Create a note diagnostic. |
aoqi@0 | 193 | * @param source The source of the compilation unit, if any, in which to report the note. |
aoqi@0 | 194 | * @param pos The source position at which to report the note. |
aoqi@0 | 195 | * @param key The key for the localized message. |
aoqi@0 | 196 | * @param args Fields of the message. |
aoqi@0 | 197 | */ |
aoqi@0 | 198 | public JCDiagnostic note( |
aoqi@0 | 199 | DiagnosticSource source, DiagnosticPosition pos, String key, Object... args) { |
aoqi@0 | 200 | return create(NOTE, null, EnumSet.noneOf(DiagnosticFlag.class), source, pos, key, args); |
aoqi@0 | 201 | } |
aoqi@0 | 202 | |
aoqi@0 | 203 | /** |
aoqi@0 | 204 | * Create a fragment diagnostic, for use as an argument in other diagnostics |
aoqi@0 | 205 | * @param key The key for the localized message. |
aoqi@0 | 206 | * @param args Fields of the message. |
aoqi@0 | 207 | */ |
aoqi@0 | 208 | public JCDiagnostic fragment(String key, Object... args) { |
aoqi@0 | 209 | return create(FRAGMENT, null, EnumSet.noneOf(DiagnosticFlag.class), null, null, key, args); |
aoqi@0 | 210 | } |
aoqi@0 | 211 | |
aoqi@0 | 212 | /** |
aoqi@0 | 213 | * Create a new diagnostic of the given kind, which is not mandatory and which has |
aoqi@0 | 214 | * no lint category. |
aoqi@0 | 215 | * @param kind The diagnostic kind |
aoqi@0 | 216 | * @param source The source of the compilation unit, if any, in which to report the message. |
aoqi@0 | 217 | * @param pos The source position at which to report the message. |
aoqi@0 | 218 | * @param key The key for the localized message. |
aoqi@0 | 219 | * @param args Fields of the message. |
aoqi@0 | 220 | */ |
aoqi@0 | 221 | public JCDiagnostic create( |
aoqi@0 | 222 | DiagnosticType kind, DiagnosticSource source, DiagnosticPosition pos, String key, Object... args) { |
aoqi@0 | 223 | return create(kind, null, EnumSet.noneOf(DiagnosticFlag.class), source, pos, key, args); |
aoqi@0 | 224 | } |
aoqi@0 | 225 | |
aoqi@0 | 226 | /** |
aoqi@0 | 227 | * Create a new diagnostic of the given kind. |
aoqi@0 | 228 | * @param kind The diagnostic kind |
aoqi@0 | 229 | * @param lc The lint category, if applicable, or null |
aoqi@0 | 230 | * @param flags The set of flags for the diagnostic |
aoqi@0 | 231 | * @param source The source of the compilation unit, if any, in which to report the message. |
aoqi@0 | 232 | * @param pos The source position at which to report the message. |
aoqi@0 | 233 | * @param key The key for the localized message. |
aoqi@0 | 234 | * @param args Fields of the message. |
aoqi@0 | 235 | */ |
aoqi@0 | 236 | public JCDiagnostic create( |
aoqi@0 | 237 | DiagnosticType kind, LintCategory lc, Set<DiagnosticFlag> flags, DiagnosticSource source, DiagnosticPosition pos, String key, Object... args) { |
aoqi@0 | 238 | return new JCDiagnostic(formatter, kind, lc, flags, source, pos, qualify(kind, key), args); |
aoqi@0 | 239 | } |
aoqi@0 | 240 | |
aoqi@0 | 241 | protected String qualify(DiagnosticType t, String key) { |
aoqi@0 | 242 | return prefix + "." + t.key + "." + key; |
aoqi@0 | 243 | } |
aoqi@0 | 244 | } |
aoqi@0 | 245 | |
aoqi@0 | 246 | |
aoqi@0 | 247 | |
aoqi@0 | 248 | /** |
aoqi@0 | 249 | * Create a fragment diagnostic, for use as an argument in other diagnostics |
aoqi@0 | 250 | * @param key The key for the localized error message. |
aoqi@0 | 251 | * @param args Fields of the error message. |
aoqi@0 | 252 | * |
aoqi@0 | 253 | */ |
aoqi@0 | 254 | @Deprecated |
aoqi@0 | 255 | public static JCDiagnostic fragment(String key, Object... args) { |
aoqi@0 | 256 | return new JCDiagnostic(getFragmentFormatter(), |
aoqi@0 | 257 | FRAGMENT, |
aoqi@0 | 258 | null, |
aoqi@0 | 259 | EnumSet.noneOf(DiagnosticFlag.class), |
aoqi@0 | 260 | null, |
aoqi@0 | 261 | null, |
aoqi@0 | 262 | "compiler." + FRAGMENT.key + "." + key, |
aoqi@0 | 263 | args); |
aoqi@0 | 264 | } |
aoqi@0 | 265 | //where |
aoqi@0 | 266 | @Deprecated |
aoqi@0 | 267 | public static DiagnosticFormatter<JCDiagnostic> getFragmentFormatter() { |
aoqi@0 | 268 | if (fragmentFormatter == null) { |
aoqi@0 | 269 | fragmentFormatter = new BasicDiagnosticFormatter(JavacMessages.getDefaultMessages()); |
aoqi@0 | 270 | } |
aoqi@0 | 271 | return fragmentFormatter; |
aoqi@0 | 272 | } |
aoqi@0 | 273 | |
aoqi@0 | 274 | /** |
aoqi@0 | 275 | * A DiagnosticType defines the type of the diagnostic. |
aoqi@0 | 276 | **/ |
aoqi@0 | 277 | public enum DiagnosticType { |
aoqi@0 | 278 | /** A fragment of an enclosing diagnostic. */ |
aoqi@0 | 279 | FRAGMENT("misc"), |
aoqi@0 | 280 | /** A note: similar to, but less serious than, a warning. */ |
aoqi@0 | 281 | NOTE("note"), |
aoqi@0 | 282 | /** A warning. */ |
aoqi@0 | 283 | WARNING("warn"), |
aoqi@0 | 284 | /** An error. */ |
aoqi@0 | 285 | ERROR("err"); |
aoqi@0 | 286 | |
aoqi@0 | 287 | final String key; |
aoqi@0 | 288 | |
aoqi@0 | 289 | /** Create a DiagnosticType. |
aoqi@0 | 290 | * @param key A string used to create the resource key for the diagnostic. |
aoqi@0 | 291 | */ |
aoqi@0 | 292 | DiagnosticType(String key) { |
aoqi@0 | 293 | this.key = key; |
aoqi@0 | 294 | } |
aoqi@0 | 295 | }; |
aoqi@0 | 296 | |
aoqi@0 | 297 | /** |
aoqi@0 | 298 | * A DiagnosticPosition provides information about the positions in a file |
aoqi@0 | 299 | * that gave rise to a diagnostic. It always defines a "preferred" position |
aoqi@0 | 300 | * that most accurately defines the location of the diagnostic, it may also |
aoqi@0 | 301 | * provide a related tree node that spans that location. |
aoqi@0 | 302 | */ |
aoqi@0 | 303 | public static interface DiagnosticPosition { |
aoqi@0 | 304 | /** Gets the tree node, if any, to which the diagnostic applies. */ |
aoqi@0 | 305 | JCTree getTree(); |
aoqi@0 | 306 | /** If there is a tree node, get the start position of the tree node. |
aoqi@0 | 307 | * Otherwise, just returns the same as getPreferredPosition(). */ |
aoqi@0 | 308 | int getStartPosition(); |
aoqi@0 | 309 | /** Get the position within the file that most accurately defines the |
aoqi@0 | 310 | * location for the diagnostic. */ |
aoqi@0 | 311 | int getPreferredPosition(); |
aoqi@0 | 312 | /** If there is a tree node, and if endPositions are available, get |
aoqi@0 | 313 | * the end position of the tree node. Otherwise, just returns the |
aoqi@0 | 314 | * same as getPreferredPosition(). */ |
aoqi@0 | 315 | int getEndPosition(EndPosTable endPosTable); |
aoqi@0 | 316 | } |
aoqi@0 | 317 | |
aoqi@0 | 318 | /** |
aoqi@0 | 319 | * A DiagnosticPosition that simply identifies a position, but no related |
aoqi@0 | 320 | * tree node, as the location for a diagnostic. Used for scanner and parser |
aoqi@0 | 321 | * diagnostics. */ |
aoqi@0 | 322 | public static class SimpleDiagnosticPosition implements DiagnosticPosition { |
aoqi@0 | 323 | public SimpleDiagnosticPosition(int pos) { |
aoqi@0 | 324 | this.pos = pos; |
aoqi@0 | 325 | } |
aoqi@0 | 326 | |
aoqi@0 | 327 | public JCTree getTree() { |
aoqi@0 | 328 | return null; |
aoqi@0 | 329 | } |
aoqi@0 | 330 | |
aoqi@0 | 331 | public int getStartPosition() { |
aoqi@0 | 332 | return pos; |
aoqi@0 | 333 | } |
aoqi@0 | 334 | |
aoqi@0 | 335 | public int getPreferredPosition() { |
aoqi@0 | 336 | return pos; |
aoqi@0 | 337 | } |
aoqi@0 | 338 | |
aoqi@0 | 339 | public int getEndPosition(EndPosTable endPosTable) { |
aoqi@0 | 340 | return pos; |
aoqi@0 | 341 | } |
aoqi@0 | 342 | |
aoqi@0 | 343 | private final int pos; |
aoqi@0 | 344 | } |
aoqi@0 | 345 | |
aoqi@0 | 346 | public enum DiagnosticFlag { |
aoqi@0 | 347 | MANDATORY, |
aoqi@0 | 348 | RESOLVE_ERROR, |
aoqi@0 | 349 | SYNTAX, |
aoqi@0 | 350 | RECOVERABLE, |
aoqi@0 | 351 | NON_DEFERRABLE, |
aoqi@0 | 352 | COMPRESSED |
aoqi@0 | 353 | } |
aoqi@0 | 354 | |
aoqi@0 | 355 | private final DiagnosticType type; |
aoqi@0 | 356 | private final DiagnosticSource source; |
aoqi@0 | 357 | private final DiagnosticPosition position; |
aoqi@0 | 358 | private final String key; |
aoqi@0 | 359 | protected final Object[] args; |
aoqi@0 | 360 | private final Set<DiagnosticFlag> flags; |
aoqi@0 | 361 | private final LintCategory lintCategory; |
aoqi@0 | 362 | |
aoqi@0 | 363 | /** source line position (set lazily) */ |
aoqi@0 | 364 | private SourcePosition sourcePosition; |
aoqi@0 | 365 | |
aoqi@0 | 366 | /** |
aoqi@0 | 367 | * This class is used to defer the line/column position fetch logic after diagnostic construction. |
aoqi@0 | 368 | */ |
aoqi@0 | 369 | class SourcePosition { |
aoqi@0 | 370 | |
aoqi@0 | 371 | private final int line; |
aoqi@0 | 372 | private final int column; |
aoqi@0 | 373 | |
aoqi@0 | 374 | SourcePosition() { |
aoqi@0 | 375 | int n = (position == null ? Position.NOPOS : position.getPreferredPosition()); |
aoqi@0 | 376 | if (n == Position.NOPOS || source == null) |
aoqi@0 | 377 | line = column = -1; |
aoqi@0 | 378 | else { |
aoqi@0 | 379 | line = source.getLineNumber(n); |
aoqi@0 | 380 | column = source.getColumnNumber(n, true); |
aoqi@0 | 381 | } |
aoqi@0 | 382 | } |
aoqi@0 | 383 | |
aoqi@0 | 384 | public int getLineNumber() { |
aoqi@0 | 385 | return line; |
aoqi@0 | 386 | } |
aoqi@0 | 387 | |
aoqi@0 | 388 | public int getColumnNumber() { |
aoqi@0 | 389 | return column; |
aoqi@0 | 390 | } |
aoqi@0 | 391 | } |
aoqi@0 | 392 | |
aoqi@0 | 393 | /** |
aoqi@0 | 394 | * Create a diagnostic object. |
aoqi@0 | 395 | * @param formatter the formatter to use for the diagnostic |
aoqi@0 | 396 | * @param dt the type of diagnostic |
aoqi@0 | 397 | * @param lc the lint category for the diagnostic |
aoqi@0 | 398 | * @param source the name of the source file, or null if none. |
aoqi@0 | 399 | * @param pos the character offset within the source file, if given. |
aoqi@0 | 400 | * @param key a resource key to identify the text of the diagnostic |
aoqi@0 | 401 | * @param args arguments to be included in the text of the diagnostic |
aoqi@0 | 402 | */ |
aoqi@0 | 403 | protected JCDiagnostic(DiagnosticFormatter<JCDiagnostic> formatter, |
aoqi@0 | 404 | DiagnosticType dt, |
aoqi@0 | 405 | LintCategory lc, |
aoqi@0 | 406 | Set<DiagnosticFlag> flags, |
aoqi@0 | 407 | DiagnosticSource source, |
aoqi@0 | 408 | DiagnosticPosition pos, |
aoqi@0 | 409 | String key, |
aoqi@0 | 410 | Object... args) { |
aoqi@0 | 411 | if (source == null && pos != null && pos.getPreferredPosition() != Position.NOPOS) |
aoqi@0 | 412 | throw new IllegalArgumentException(); |
aoqi@0 | 413 | |
aoqi@0 | 414 | this.defaultFormatter = formatter; |
aoqi@0 | 415 | this.type = dt; |
aoqi@0 | 416 | this.lintCategory = lc; |
aoqi@0 | 417 | this.flags = flags; |
aoqi@0 | 418 | this.source = source; |
aoqi@0 | 419 | this.position = pos; |
aoqi@0 | 420 | this.key = key; |
aoqi@0 | 421 | this.args = args; |
aoqi@0 | 422 | } |
aoqi@0 | 423 | |
aoqi@0 | 424 | /** |
aoqi@0 | 425 | * Get the type of this diagnostic. |
aoqi@0 | 426 | * @return the type of this diagnostic |
aoqi@0 | 427 | */ |
aoqi@0 | 428 | public DiagnosticType getType() { |
aoqi@0 | 429 | return type; |
aoqi@0 | 430 | } |
aoqi@0 | 431 | |
aoqi@0 | 432 | /** |
aoqi@0 | 433 | * Get the subdiagnostic list |
aoqi@0 | 434 | * @return subdiagnostic list |
aoqi@0 | 435 | */ |
aoqi@0 | 436 | public List<JCDiagnostic> getSubdiagnostics() { |
aoqi@0 | 437 | return List.nil(); |
aoqi@0 | 438 | } |
aoqi@0 | 439 | |
aoqi@0 | 440 | public boolean isMultiline() { |
aoqi@0 | 441 | return false; |
aoqi@0 | 442 | } |
aoqi@0 | 443 | |
aoqi@0 | 444 | /** |
aoqi@0 | 445 | * Check whether or not this diagnostic is required to be shown. |
aoqi@0 | 446 | * @return true if this diagnostic is required to be shown. |
aoqi@0 | 447 | */ |
aoqi@0 | 448 | public boolean isMandatory() { |
aoqi@0 | 449 | return flags.contains(DiagnosticFlag.MANDATORY); |
aoqi@0 | 450 | } |
aoqi@0 | 451 | |
aoqi@0 | 452 | /** |
aoqi@0 | 453 | * Check whether this diagnostic has an associated lint category. |
aoqi@0 | 454 | */ |
aoqi@0 | 455 | public boolean hasLintCategory() { |
aoqi@0 | 456 | return (lintCategory != null); |
aoqi@0 | 457 | } |
aoqi@0 | 458 | |
aoqi@0 | 459 | /** |
aoqi@0 | 460 | * Get the associated lint category, or null if none. |
aoqi@0 | 461 | */ |
aoqi@0 | 462 | public LintCategory getLintCategory() { |
aoqi@0 | 463 | return lintCategory; |
aoqi@0 | 464 | } |
aoqi@0 | 465 | |
aoqi@0 | 466 | /** |
aoqi@0 | 467 | * Get the name of the source file referred to by this diagnostic. |
aoqi@0 | 468 | * @return the name of the source referred to with this diagnostic, or null if none |
aoqi@0 | 469 | */ |
aoqi@0 | 470 | public JavaFileObject getSource() { |
aoqi@0 | 471 | if (source == null) |
aoqi@0 | 472 | return null; |
aoqi@0 | 473 | else |
aoqi@0 | 474 | return source.getFile(); |
aoqi@0 | 475 | } |
aoqi@0 | 476 | |
aoqi@0 | 477 | /** |
aoqi@0 | 478 | * Get the source referred to by this diagnostic. |
aoqi@0 | 479 | * @return the source referred to with this diagnostic, or null if none |
aoqi@0 | 480 | */ |
aoqi@0 | 481 | public DiagnosticSource getDiagnosticSource() { |
aoqi@0 | 482 | return source; |
aoqi@0 | 483 | } |
aoqi@0 | 484 | |
aoqi@0 | 485 | protected int getIntStartPosition() { |
aoqi@0 | 486 | return (position == null ? Position.NOPOS : position.getStartPosition()); |
aoqi@0 | 487 | } |
aoqi@0 | 488 | |
aoqi@0 | 489 | protected int getIntPosition() { |
aoqi@0 | 490 | return (position == null ? Position.NOPOS : position.getPreferredPosition()); |
aoqi@0 | 491 | } |
aoqi@0 | 492 | |
aoqi@0 | 493 | protected int getIntEndPosition() { |
aoqi@0 | 494 | return (position == null ? Position.NOPOS : position.getEndPosition(source.getEndPosTable())); |
aoqi@0 | 495 | } |
aoqi@0 | 496 | |
aoqi@0 | 497 | public long getStartPosition() { |
aoqi@0 | 498 | return getIntStartPosition(); |
aoqi@0 | 499 | } |
aoqi@0 | 500 | |
aoqi@0 | 501 | public long getPosition() { |
aoqi@0 | 502 | return getIntPosition(); |
aoqi@0 | 503 | } |
aoqi@0 | 504 | |
aoqi@0 | 505 | public long getEndPosition() { |
aoqi@0 | 506 | return getIntEndPosition(); |
aoqi@0 | 507 | } |
aoqi@0 | 508 | |
aoqi@0 | 509 | public DiagnosticPosition getDiagnosticPosition() { |
aoqi@0 | 510 | return position; |
aoqi@0 | 511 | } |
aoqi@0 | 512 | |
aoqi@0 | 513 | /** |
aoqi@0 | 514 | * Get the line number within the source referred to by this diagnostic. |
aoqi@0 | 515 | * @return the line number within the source referred to by this diagnostic |
aoqi@0 | 516 | */ |
aoqi@0 | 517 | public long getLineNumber() { |
aoqi@0 | 518 | if (sourcePosition == null) { |
aoqi@0 | 519 | sourcePosition = new SourcePosition(); |
aoqi@0 | 520 | } |
aoqi@0 | 521 | return sourcePosition.getLineNumber(); |
aoqi@0 | 522 | } |
aoqi@0 | 523 | |
aoqi@0 | 524 | /** |
aoqi@0 | 525 | * Get the column number within the line of source referred to by this diagnostic. |
aoqi@0 | 526 | * @return the column number within the line of source referred to by this diagnostic |
aoqi@0 | 527 | */ |
aoqi@0 | 528 | public long getColumnNumber() { |
aoqi@0 | 529 | if (sourcePosition == null) { |
aoqi@0 | 530 | sourcePosition = new SourcePosition(); |
aoqi@0 | 531 | } |
aoqi@0 | 532 | return sourcePosition.getColumnNumber(); |
aoqi@0 | 533 | } |
aoqi@0 | 534 | |
aoqi@0 | 535 | /** |
aoqi@0 | 536 | * Get the arguments to be included in the text of the diagnostic. |
aoqi@0 | 537 | * @return the arguments to be included in the text of the diagnostic |
aoqi@0 | 538 | */ |
aoqi@0 | 539 | public Object[] getArgs() { |
aoqi@0 | 540 | return args; |
aoqi@0 | 541 | } |
aoqi@0 | 542 | |
aoqi@0 | 543 | /** |
aoqi@0 | 544 | * Get the prefix string associated with this type of diagnostic. |
aoqi@0 | 545 | * @return the prefix string associated with this type of diagnostic |
aoqi@0 | 546 | */ |
aoqi@0 | 547 | public String getPrefix() { |
aoqi@0 | 548 | return getPrefix(type); |
aoqi@0 | 549 | } |
aoqi@0 | 550 | |
aoqi@0 | 551 | /** |
aoqi@0 | 552 | * Get the prefix string associated with a particular type of diagnostic. |
aoqi@0 | 553 | * @return the prefix string associated with a particular type of diagnostic |
aoqi@0 | 554 | */ |
aoqi@0 | 555 | public String getPrefix(DiagnosticType dt) { |
aoqi@0 | 556 | return defaultFormatter.formatKind(this, Locale.getDefault()); |
aoqi@0 | 557 | } |
aoqi@0 | 558 | |
aoqi@0 | 559 | /** |
aoqi@0 | 560 | * Return the standard presentation of this diagnostic. |
aoqi@0 | 561 | */ |
aoqi@0 | 562 | @Override |
aoqi@0 | 563 | public String toString() { |
aoqi@0 | 564 | return defaultFormatter.format(this,Locale.getDefault()); |
aoqi@0 | 565 | } |
aoqi@0 | 566 | |
aoqi@0 | 567 | private DiagnosticFormatter<JCDiagnostic> defaultFormatter; |
aoqi@0 | 568 | @Deprecated |
aoqi@0 | 569 | private static DiagnosticFormatter<JCDiagnostic> fragmentFormatter; |
aoqi@0 | 570 | |
aoqi@0 | 571 | // Methods for javax.tools.Diagnostic |
aoqi@0 | 572 | |
aoqi@0 | 573 | public Diagnostic.Kind getKind() { |
aoqi@0 | 574 | switch (type) { |
aoqi@0 | 575 | case NOTE: |
aoqi@0 | 576 | return Diagnostic.Kind.NOTE; |
aoqi@0 | 577 | case WARNING: |
aoqi@0 | 578 | return flags.contains(DiagnosticFlag.MANDATORY) |
aoqi@0 | 579 | ? Diagnostic.Kind.MANDATORY_WARNING |
aoqi@0 | 580 | : Diagnostic.Kind.WARNING; |
aoqi@0 | 581 | case ERROR: |
aoqi@0 | 582 | return Diagnostic.Kind.ERROR; |
aoqi@0 | 583 | default: |
aoqi@0 | 584 | return Diagnostic.Kind.OTHER; |
aoqi@0 | 585 | } |
aoqi@0 | 586 | } |
aoqi@0 | 587 | |
aoqi@0 | 588 | public String getCode() { |
aoqi@0 | 589 | return key; |
aoqi@0 | 590 | } |
aoqi@0 | 591 | |
aoqi@0 | 592 | public String getMessage(Locale locale) { |
aoqi@0 | 593 | return defaultFormatter.formatMessage(this, locale); |
aoqi@0 | 594 | } |
aoqi@0 | 595 | |
aoqi@0 | 596 | public void setFlag(DiagnosticFlag flag) { |
aoqi@0 | 597 | flags.add(flag); |
aoqi@0 | 598 | |
aoqi@0 | 599 | if (type == DiagnosticType.ERROR) { |
aoqi@0 | 600 | switch (flag) { |
aoqi@0 | 601 | case SYNTAX: |
aoqi@0 | 602 | flags.remove(DiagnosticFlag.RECOVERABLE); |
aoqi@0 | 603 | break; |
aoqi@0 | 604 | case RESOLVE_ERROR: |
aoqi@0 | 605 | flags.add(DiagnosticFlag.RECOVERABLE); |
aoqi@0 | 606 | break; |
aoqi@0 | 607 | } |
aoqi@0 | 608 | } |
aoqi@0 | 609 | } |
aoqi@0 | 610 | |
aoqi@0 | 611 | public boolean isFlagSet(DiagnosticFlag flag) { |
aoqi@0 | 612 | return flags.contains(flag); |
aoqi@0 | 613 | } |
aoqi@0 | 614 | |
aoqi@0 | 615 | public static class MultilineDiagnostic extends JCDiagnostic { |
aoqi@0 | 616 | |
aoqi@0 | 617 | private final List<JCDiagnostic> subdiagnostics; |
aoqi@0 | 618 | |
aoqi@0 | 619 | public MultilineDiagnostic(JCDiagnostic other, List<JCDiagnostic> subdiagnostics) { |
aoqi@0 | 620 | super(other.defaultFormatter, |
aoqi@0 | 621 | other.getType(), |
aoqi@0 | 622 | other.getLintCategory(), |
aoqi@0 | 623 | other.flags, |
aoqi@0 | 624 | other.getDiagnosticSource(), |
aoqi@0 | 625 | other.position, |
aoqi@0 | 626 | other.getCode(), |
aoqi@0 | 627 | other.getArgs()); |
aoqi@0 | 628 | this.subdiagnostics = subdiagnostics; |
aoqi@0 | 629 | } |
aoqi@0 | 630 | |
aoqi@0 | 631 | @Override |
aoqi@0 | 632 | public List<JCDiagnostic> getSubdiagnostics() { |
aoqi@0 | 633 | return subdiagnostics; |
aoqi@0 | 634 | } |
aoqi@0 | 635 | |
aoqi@0 | 636 | @Override |
aoqi@0 | 637 | public boolean isMultiline() { |
aoqi@0 | 638 | return true; |
aoqi@0 | 639 | } |
aoqi@0 | 640 | } |
aoqi@0 | 641 | } |