1 /*
   2  * Copyright (c) 2005, 2019, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package javax.tools;
  27 
  28 import java.util.Locale;
  29 
  30 /**
  31  * Interface for diagnostics from tools.  A diagnostic usually reports
  32  * a problem at a specific position in a source file.  However, not
  33  * all diagnostics are associated with a position or a file.
  34  *
  35  * <p>A position is a zero-based character offset from the beginning of
  36  * a file.  Negative values (except {@link #NOPOS}) are not valid
  37  * positions.
  38  *
  39  * <p>Line and column numbers begin at 1.  Negative values (except
  40  * {@link #NOPOS}) and 0 are not valid line or column numbers.
  41  *
  42  * @param <S> the type of source object used by this diagnostic
  43  *
  44  * @author Peter von der Ah&eacute;
  45  * @author Jonathan Gibbons
  46  * @since 1.6
  47  */
  48 public interface Diagnostic<S> {
  49 
  50     /**
  51      * Kinds of diagnostics, for example, error or warning.
  52      *
  53      * The kind of a diagnostic can be used to determine how the
  54      * diagnostic should be presented to the user. For example,
  55      * errors might be colored red or prefixed with the word "Error",
  56      * while warnings might be colored yellow or prefixed with the
  57      * word "Warning". There is no requirement that the Kind
  58      * should imply any inherent semantic meaning to the message
  59      * of the diagnostic: for example, a tool might provide an
  60      * option to report all warnings as errors.
  61      */
  62     enum Kind {
  63         /**
  64          * Problem which prevents the tool's normal completion.
  65          */
  66         ERROR,
  67         /**
  68          * Problem which does not usually prevent the tool from
  69          * completing normally.
  70          */
  71         WARNING,
  72         /**
  73          * Problem similar to a warning, but is mandated by the tool's
  74          * specification.  For example, the Java&trade; Language
  75          * Specification mandates warnings on certain
  76          * unchecked operations and the use of deprecated methods.
  77          */
  78         MANDATORY_WARNING,
  79         /**
  80          * Informative message from the tool.
  81          */
  82         NOTE,
  83         /**
  84          * Diagnostic which does not fit within the other kinds.
  85          */
  86         OTHER,
  87     }
  88 
  89     /**
  90      * Used to signal that no position is available.
  91      */
  92     public final static long NOPOS = -1;
  93 
  94     /**
  95      * Returns the kind of this diagnostic, for example, error or
  96      * warning.
  97      * @return the kind of this diagnostic
  98      */
  99     Kind getKind();
 100 
 101     /**
 102      * Returns the source object associated with this diagnostic.
 103      *
 104      * @return the source object associated with this diagnostic.
 105      * {@code null} if no source object is associated with the
 106      * diagnostic.
 107      */
 108     S getSource();
 109 
 110     /**
 111      * Returns a character offset from the beginning of the source object
 112      * associated with this diagnostic that indicates the location of
 113      * the problem.  In addition, the following must be true:
 114      *
 115      * <p>{@code getStartPosition() <= getPosition()}
 116      * <p>{@code getPosition() <= getEndPosition()}
 117      *
 118      * @return character offset from beginning of source; {@link
 119      * #NOPOS} if {@link #getSource()} would return {@code null} or if
 120      * no location is suitable
 121      */
 122     long getPosition();
 123 
 124     /**
 125      * Returns the character offset from the beginning of the file
 126      * associated with this diagnostic that indicates the start of the
 127      * problem.
 128      *
 129      * @return offset from beginning of file; {@link #NOPOS} if and
 130      * only if {@link #getPosition()} returns {@link #NOPOS}
 131      */
 132     long getStartPosition();
 133 
 134     /**
 135      * Returns the character offset from the beginning of the file
 136      * associated with this diagnostic that indicates the end of the
 137      * problem.
 138      *
 139      * @return offset from beginning of file; {@link #NOPOS} if and
 140      * only if {@link #getPosition()} returns {@link #NOPOS}
 141      */
 142     long getEndPosition();
 143 
 144     /**
 145      * Returns the line number of the character offset returned by
 146      * {@linkplain #getPosition()}.
 147      *
 148      * @return a line number or {@link #NOPOS} if and only if {@link
 149      * #getPosition()} returns {@link #NOPOS}
 150      */
 151     long getLineNumber();
 152 
 153     /**
 154      * Returns the column number of the character offset returned by
 155      * {@linkplain #getPosition()}.
 156      *
 157      * @return a column number or {@link #NOPOS} if and only if {@link
 158      * #getPosition()} returns {@link #NOPOS}
 159      */
 160     long getColumnNumber();
 161 
 162     /**
 163      * Returns a diagnostic code indicating the type of diagnostic.  The
 164      * code is implementation-dependent and might be {@code null}.
 165      *
 166      * @return a diagnostic code
 167      */
 168     String getCode();
 169 
 170     /**
 171      * Returns a localized message for the given locale.  The actual
 172      * message is implementation-dependent.  If the locale is {@code
 173      * null} use the default locale.
 174      *
 175      * @param locale a locale; might be {@code null}
 176      * @return a localized message
 177      */
 178     String getMessage(Locale locale);
 179 }