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