langtools/src/share/classes/com/sun/tools/javac/util/JCDiagnostic.java
author mcimadamore
Mon, 28 Jul 2008 10:22:10 +0100
changeset 942 98e1d4f8aacd
parent 939 38e24969c7e9
child 1040 c0f5acfd9d15
permissions -rw-r--r--
6720185: DiagnosticFormatter refactoring Summary: Brand new hierarchy of diagnostic formatters for achieving better reusability Reviewed-by: jjg
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     1
/*
735
372aa565a221 6719955: Update copyright year
xdono
parents: 731
diff changeset
     2
 * Copyright 2003-2008 Sun Microsystems, Inc.  All Rights Reserved.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
06bc494ca11e Initial load
duke
parents:
diff changeset
     4
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
06bc494ca11e Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
06bc494ca11e Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
06bc494ca11e Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
06bc494ca11e Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
06bc494ca11e Initial load
duke
parents:
diff changeset
    10
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
06bc494ca11e Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
06bc494ca11e Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
06bc494ca11e Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
06bc494ca11e Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
06bc494ca11e Initial load
duke
parents:
diff changeset
    16
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
06bc494ca11e Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
06bc494ca11e Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
06bc494ca11e Initial load
duke
parents:
diff changeset
    20
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
06bc494ca11e Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
06bc494ca11e Initial load
duke
parents:
diff changeset
    23
 * have any questions.
06bc494ca11e Initial load
duke
parents:
diff changeset
    24
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    25
06bc494ca11e Initial load
duke
parents:
diff changeset
    26
package com.sun.tools.javac.util;
06bc494ca11e Initial load
duke
parents:
diff changeset
    27
06bc494ca11e Initial load
duke
parents:
diff changeset
    28
import java.util.Locale;
06bc494ca11e Initial load
duke
parents:
diff changeset
    29
import java.util.Map;
06bc494ca11e Initial load
duke
parents:
diff changeset
    30
06bc494ca11e Initial load
duke
parents:
diff changeset
    31
import javax.tools.Diagnostic;
06bc494ca11e Initial load
duke
parents:
diff changeset
    32
import javax.tools.JavaFileObject;
06bc494ca11e Initial load
duke
parents:
diff changeset
    33
942
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents: 939
diff changeset
    34
import com.sun.tools.javac.api.DiagnosticFormatter;
731
1dd22bdb9ca5 6714364: refactor javac File handling code into new javac.file package
jjg
parents: 10
diff changeset
    35
import com.sun.tools.javac.file.JavacFileManager;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    36
import com.sun.tools.javac.tree.JCTree;
06bc494ca11e Initial load
duke
parents:
diff changeset
    37
06bc494ca11e Initial load
duke
parents:
diff changeset
    38
import static com.sun.tools.javac.util.JCDiagnostic.DiagnosticType.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    39
06bc494ca11e Initial load
duke
parents:
diff changeset
    40
/** An abstraction of a diagnostic message generated by the compiler.
06bc494ca11e Initial load
duke
parents:
diff changeset
    41
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    42
 *  <p><b>This is NOT part of any API supported by Sun Microsystems.  If
06bc494ca11e Initial load
duke
parents:
diff changeset
    43
 *  you write code that depends on this, you do so at your own risk.
06bc494ca11e Initial load
duke
parents:
diff changeset
    44
 *  This code and its internal interfaces are subject to change or
06bc494ca11e Initial load
duke
parents:
diff changeset
    45
 *  deletion without notice.</b>
06bc494ca11e Initial load
duke
parents:
diff changeset
    46
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    47
public class JCDiagnostic implements Diagnostic<JavaFileObject> {
06bc494ca11e Initial load
duke
parents:
diff changeset
    48
    /** A factory for creating diagnostic objects. */
06bc494ca11e Initial load
duke
parents:
diff changeset
    49
    public static class Factory {
06bc494ca11e Initial load
duke
parents:
diff changeset
    50
        /** The context key for the diagnostic factory. */
06bc494ca11e Initial load
duke
parents:
diff changeset
    51
        protected static final Context.Key<JCDiagnostic.Factory> diagnosticFactoryKey =
06bc494ca11e Initial load
duke
parents:
diff changeset
    52
            new Context.Key<JCDiagnostic.Factory>();
06bc494ca11e Initial load
duke
parents:
diff changeset
    53
06bc494ca11e Initial load
duke
parents:
diff changeset
    54
        /** Get the Factory instance for this context. */
06bc494ca11e Initial load
duke
parents:
diff changeset
    55
        public static Factory instance(Context context) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    56
            Factory instance = context.get(diagnosticFactoryKey);
06bc494ca11e Initial load
duke
parents:
diff changeset
    57
            if (instance == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
    58
                instance = new Factory(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
    59
            return instance;
06bc494ca11e Initial load
duke
parents:
diff changeset
    60
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
    61
06bc494ca11e Initial load
duke
parents:
diff changeset
    62
        final Messages messages;
06bc494ca11e Initial load
duke
parents:
diff changeset
    63
        final String prefix;
06bc494ca11e Initial load
duke
parents:
diff changeset
    64
06bc494ca11e Initial load
duke
parents:
diff changeset
    65
        /** Create a new diagnostic factory. */
06bc494ca11e Initial load
duke
parents:
diff changeset
    66
        protected Factory(Context context) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    67
            context.put(diagnosticFactoryKey, this);
06bc494ca11e Initial load
duke
parents:
diff changeset
    68
            messages = Messages.instance(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
    69
            prefix = "compiler";
06bc494ca11e Initial load
duke
parents:
diff changeset
    70
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
    71
06bc494ca11e Initial load
duke
parents:
diff changeset
    72
        /** Create a new diagnostic factory. */
06bc494ca11e Initial load
duke
parents:
diff changeset
    73
        public Factory(Messages messages, String prefix) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    74
            this.messages = messages;
06bc494ca11e Initial load
duke
parents:
diff changeset
    75
            this.prefix = prefix;
06bc494ca11e Initial load
duke
parents:
diff changeset
    76
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
    77
06bc494ca11e Initial load
duke
parents:
diff changeset
    78
        /**
06bc494ca11e Initial load
duke
parents:
diff changeset
    79
         * Create an error diagnostic.
06bc494ca11e Initial load
duke
parents:
diff changeset
    80
         *  @param source The source of the compilation unit, if any, in which to report the error.
06bc494ca11e Initial load
duke
parents:
diff changeset
    81
         *  @param pos    The source position at which to report the error.
06bc494ca11e Initial load
duke
parents:
diff changeset
    82
         *  @param key    The key for the localized error message.
06bc494ca11e Initial load
duke
parents:
diff changeset
    83
         *  @param args   Fields of the error message.
06bc494ca11e Initial load
duke
parents:
diff changeset
    84
         */
06bc494ca11e Initial load
duke
parents:
diff changeset
    85
        public JCDiagnostic error(
06bc494ca11e Initial load
duke
parents:
diff changeset
    86
                DiagnosticSource source, DiagnosticPosition pos, String key, Object... args) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    87
            return new JCDiagnostic(messages, ERROR, true, source, pos, qualify(ERROR, key), args);
06bc494ca11e Initial load
duke
parents:
diff changeset
    88
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
    89
06bc494ca11e Initial load
duke
parents:
diff changeset
    90
        /**
06bc494ca11e Initial load
duke
parents:
diff changeset
    91
         * Create a warning diagnostic that will not be hidden by the -nowarn or -Xlint:none options.
06bc494ca11e Initial load
duke
parents:
diff changeset
    92
         *  @param source The source of the compilation unit, if any, in which to report the warning.
06bc494ca11e Initial load
duke
parents:
diff changeset
    93
         *  @param pos    The source position at which to report the warning.
06bc494ca11e Initial load
duke
parents:
diff changeset
    94
         *  @param key    The key for the localized error message.
06bc494ca11e Initial load
duke
parents:
diff changeset
    95
         *  @param args   Fields of the error message.
06bc494ca11e Initial load
duke
parents:
diff changeset
    96
         *  @see MandatoryWarningHandler
06bc494ca11e Initial load
duke
parents:
diff changeset
    97
         */
06bc494ca11e Initial load
duke
parents:
diff changeset
    98
        public JCDiagnostic mandatoryWarning(
06bc494ca11e Initial load
duke
parents:
diff changeset
    99
                 DiagnosticSource source, DiagnosticPosition pos, String key, Object... args) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   100
            return new JCDiagnostic(messages, WARNING, true, source, pos, qualify(WARNING, key), args);
06bc494ca11e Initial load
duke
parents:
diff changeset
   101
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   102
06bc494ca11e Initial load
duke
parents:
diff changeset
   103
        /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   104
         * Create a warning diagnostic.
06bc494ca11e Initial load
duke
parents:
diff changeset
   105
         *  @param source The source of the compilation unit, if any, in which to report the warning.
06bc494ca11e Initial load
duke
parents:
diff changeset
   106
         *  @param pos    The source position at which to report the warning.
06bc494ca11e Initial load
duke
parents:
diff changeset
   107
         *  @param key    The key for the localized error message.
06bc494ca11e Initial load
duke
parents:
diff changeset
   108
         *  @param args   Fields of the error message.
06bc494ca11e Initial load
duke
parents:
diff changeset
   109
         */
06bc494ca11e Initial load
duke
parents:
diff changeset
   110
        public JCDiagnostic warning(
06bc494ca11e Initial load
duke
parents:
diff changeset
   111
                DiagnosticSource source, DiagnosticPosition pos, String key, Object... args) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   112
            return new JCDiagnostic(messages, WARNING, false, source, pos, qualify(WARNING, key), args);
06bc494ca11e Initial load
duke
parents:
diff changeset
   113
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   114
06bc494ca11e Initial load
duke
parents:
diff changeset
   115
        /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   116
         * Create a note diagnostic that will not be hidden by the -nowarn or -Xlint:none options.
06bc494ca11e Initial load
duke
parents:
diff changeset
   117
         *  @param key    The key for the localized error message.
06bc494ca11e Initial load
duke
parents:
diff changeset
   118
         *  @param args   Fields of the error message.
06bc494ca11e Initial load
duke
parents:
diff changeset
   119
         *  @see MandatoryWarningHandler
06bc494ca11e Initial load
duke
parents:
diff changeset
   120
         */
06bc494ca11e Initial load
duke
parents:
diff changeset
   121
        public JCDiagnostic mandatoryNote(DiagnosticSource source, String key, Object... args) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   122
            return new JCDiagnostic(messages, NOTE, true, source, null, qualify(NOTE, key), args);
06bc494ca11e Initial load
duke
parents:
diff changeset
   123
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   124
06bc494ca11e Initial load
duke
parents:
diff changeset
   125
        /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   126
         * Create a note diagnostic.
06bc494ca11e Initial load
duke
parents:
diff changeset
   127
         *  @param key    The key for the localized error message.
06bc494ca11e Initial load
duke
parents:
diff changeset
   128
         *  @param args   Fields of the error message.
06bc494ca11e Initial load
duke
parents:
diff changeset
   129
         */
06bc494ca11e Initial load
duke
parents:
diff changeset
   130
        public JCDiagnostic note(String key, Object... args) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   131
            return note(null, null, key, args);
06bc494ca11e Initial load
duke
parents:
diff changeset
   132
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   133
06bc494ca11e Initial load
duke
parents:
diff changeset
   134
        /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   135
         * Create a note diagnostic.
06bc494ca11e Initial load
duke
parents:
diff changeset
   136
         *  @param source The source of the compilation unit, if any, in which to report the note.
06bc494ca11e Initial load
duke
parents:
diff changeset
   137
         *  @param pos    The source position at which to report the note.
06bc494ca11e Initial load
duke
parents:
diff changeset
   138
         *  @param key    The key for the localized error message.
06bc494ca11e Initial load
duke
parents:
diff changeset
   139
         *  @param args   Fields of the error message.
06bc494ca11e Initial load
duke
parents:
diff changeset
   140
         */
06bc494ca11e Initial load
duke
parents:
diff changeset
   141
        public JCDiagnostic note(
06bc494ca11e Initial load
duke
parents:
diff changeset
   142
                DiagnosticSource source, DiagnosticPosition pos, String key, Object... args) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   143
            return new JCDiagnostic(messages, NOTE, false, source, pos, qualify(NOTE, key), args);
06bc494ca11e Initial load
duke
parents:
diff changeset
   144
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   145
06bc494ca11e Initial load
duke
parents:
diff changeset
   146
        /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   147
         * Create a fragment diagnostic, for use as an argument in other diagnostics
06bc494ca11e Initial load
duke
parents:
diff changeset
   148
         *  @param key    The key for the localized error message.
06bc494ca11e Initial load
duke
parents:
diff changeset
   149
         *  @param args   Fields of the error message.
06bc494ca11e Initial load
duke
parents:
diff changeset
   150
         */
06bc494ca11e Initial load
duke
parents:
diff changeset
   151
        public JCDiagnostic fragment(String key, Object... args) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   152
            return new JCDiagnostic(messages, FRAGMENT, false, null, null, qualify(FRAGMENT, key), args);
06bc494ca11e Initial load
duke
parents:
diff changeset
   153
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   154
06bc494ca11e Initial load
duke
parents:
diff changeset
   155
        protected String qualify(DiagnosticType t, String key) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   156
            return prefix + "." + t.key + "." + key;
06bc494ca11e Initial load
duke
parents:
diff changeset
   157
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   158
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   159
06bc494ca11e Initial load
duke
parents:
diff changeset
   160
06bc494ca11e Initial load
duke
parents:
diff changeset
   161
06bc494ca11e Initial load
duke
parents:
diff changeset
   162
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   163
     * Create a fragment diagnostic, for use as an argument in other diagnostics
06bc494ca11e Initial load
duke
parents:
diff changeset
   164
     *  @param key    The key for the localized error message.
06bc494ca11e Initial load
duke
parents:
diff changeset
   165
     *  @param args   Fields of the error message.
06bc494ca11e Initial load
duke
parents:
diff changeset
   166
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   167
    // should be deprecated
06bc494ca11e Initial load
duke
parents:
diff changeset
   168
    public static JCDiagnostic fragment(String key, Object... args) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   169
        return new JCDiagnostic(Messages.getDefaultMessages(),
06bc494ca11e Initial load
duke
parents:
diff changeset
   170
                              FRAGMENT,
06bc494ca11e Initial load
duke
parents:
diff changeset
   171
                              false,
06bc494ca11e Initial load
duke
parents:
diff changeset
   172
                              null,
06bc494ca11e Initial load
duke
parents:
diff changeset
   173
                              null,
06bc494ca11e Initial load
duke
parents:
diff changeset
   174
                              "compiler." + FRAGMENT.key + "." + key,
06bc494ca11e Initial load
duke
parents:
diff changeset
   175
                              args);
06bc494ca11e Initial load
duke
parents:
diff changeset
   176
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   177
06bc494ca11e Initial load
duke
parents:
diff changeset
   178
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   179
     * A DiagnosticType defines the type of the diagnostic.
06bc494ca11e Initial load
duke
parents:
diff changeset
   180
     **/
06bc494ca11e Initial load
duke
parents:
diff changeset
   181
    public enum DiagnosticType {
06bc494ca11e Initial load
duke
parents:
diff changeset
   182
        /** A fragment of an enclosing diagnostic. */
06bc494ca11e Initial load
duke
parents:
diff changeset
   183
        FRAGMENT("misc"),
06bc494ca11e Initial load
duke
parents:
diff changeset
   184
        /** A note: similar to, but less serious than, a warning. */
06bc494ca11e Initial load
duke
parents:
diff changeset
   185
        NOTE("note"),
06bc494ca11e Initial load
duke
parents:
diff changeset
   186
        /** A warning. */
06bc494ca11e Initial load
duke
parents:
diff changeset
   187
        WARNING("warn"),
06bc494ca11e Initial load
duke
parents:
diff changeset
   188
        /** An error. */
06bc494ca11e Initial load
duke
parents:
diff changeset
   189
        ERROR("err");
06bc494ca11e Initial load
duke
parents:
diff changeset
   190
06bc494ca11e Initial load
duke
parents:
diff changeset
   191
        final String key;
06bc494ca11e Initial load
duke
parents:
diff changeset
   192
06bc494ca11e Initial load
duke
parents:
diff changeset
   193
        /** Create a DiagnosticType.
06bc494ca11e Initial load
duke
parents:
diff changeset
   194
         * @param key A string used to create the resource key for the diagnostic.
06bc494ca11e Initial load
duke
parents:
diff changeset
   195
         */
06bc494ca11e Initial load
duke
parents:
diff changeset
   196
        DiagnosticType(String key) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   197
            this.key = key;
06bc494ca11e Initial load
duke
parents:
diff changeset
   198
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   199
    };
06bc494ca11e Initial load
duke
parents:
diff changeset
   200
06bc494ca11e Initial load
duke
parents:
diff changeset
   201
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   202
     * A DiagnosticPosition provides information about the positions in a file
06bc494ca11e Initial load
duke
parents:
diff changeset
   203
     * that gave rise to a diagnostic. It always defines a "preferred" position
06bc494ca11e Initial load
duke
parents:
diff changeset
   204
     * that most accurately defines the location of the diagnostic, it may also
06bc494ca11e Initial load
duke
parents:
diff changeset
   205
     * provide a related tree node that spans that location.
06bc494ca11e Initial load
duke
parents:
diff changeset
   206
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   207
    public static interface DiagnosticPosition {
06bc494ca11e Initial load
duke
parents:
diff changeset
   208
        /** Gets the tree node, if any, to which the diagnostic applies. */
06bc494ca11e Initial load
duke
parents:
diff changeset
   209
        JCTree getTree();
06bc494ca11e Initial load
duke
parents:
diff changeset
   210
        /** If there is a tree node, get the start position of the tree node.
06bc494ca11e Initial load
duke
parents:
diff changeset
   211
         *  Otherwise, just returns the same as getPreferredPosition(). */
06bc494ca11e Initial load
duke
parents:
diff changeset
   212
        int getStartPosition();
06bc494ca11e Initial load
duke
parents:
diff changeset
   213
        /** Get the position within the file that most accurately defines the
06bc494ca11e Initial load
duke
parents:
diff changeset
   214
         *  location for the diagnostic. */
06bc494ca11e Initial load
duke
parents:
diff changeset
   215
        int getPreferredPosition();
06bc494ca11e Initial load
duke
parents:
diff changeset
   216
        /** If there is a tree node, and if endPositions are available, get
06bc494ca11e Initial load
duke
parents:
diff changeset
   217
         *  the end position of the tree node. Otherwise, just returns the
06bc494ca11e Initial load
duke
parents:
diff changeset
   218
         *  same as getPreferredPosition(). */
06bc494ca11e Initial load
duke
parents:
diff changeset
   219
        int getEndPosition(Map<JCTree, Integer> endPosTable);
06bc494ca11e Initial load
duke
parents:
diff changeset
   220
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   221
06bc494ca11e Initial load
duke
parents:
diff changeset
   222
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   223
     * A DiagnosticPosition that simply identifies a position, but no related
06bc494ca11e Initial load
duke
parents:
diff changeset
   224
     * tree node, as the location for a diagnostic. Used for scanner and parser
06bc494ca11e Initial load
duke
parents:
diff changeset
   225
     * diagnostics. */
06bc494ca11e Initial load
duke
parents:
diff changeset
   226
    public static class SimpleDiagnosticPosition implements DiagnosticPosition {
06bc494ca11e Initial load
duke
parents:
diff changeset
   227
        public SimpleDiagnosticPosition(int pos) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   228
            this.pos = pos;
06bc494ca11e Initial load
duke
parents:
diff changeset
   229
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   230
06bc494ca11e Initial load
duke
parents:
diff changeset
   231
        public JCTree getTree() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   232
            return null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   233
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   234
06bc494ca11e Initial load
duke
parents:
diff changeset
   235
        public int getStartPosition() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   236
            return pos;
06bc494ca11e Initial load
duke
parents:
diff changeset
   237
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   238
06bc494ca11e Initial load
duke
parents:
diff changeset
   239
        public int getPreferredPosition() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   240
            return pos;
06bc494ca11e Initial load
duke
parents:
diff changeset
   241
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   242
06bc494ca11e Initial load
duke
parents:
diff changeset
   243
        public int getEndPosition(Map<JCTree, Integer> endPosTable) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   244
            return pos;
06bc494ca11e Initial load
duke
parents:
diff changeset
   245
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   246
06bc494ca11e Initial load
duke
parents:
diff changeset
   247
        private final int pos;
06bc494ca11e Initial load
duke
parents:
diff changeset
   248
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   249
06bc494ca11e Initial load
duke
parents:
diff changeset
   250
    private final Messages messages;
06bc494ca11e Initial load
duke
parents:
diff changeset
   251
    private final DiagnosticType type;
06bc494ca11e Initial load
duke
parents:
diff changeset
   252
    private final DiagnosticSource source;
06bc494ca11e Initial load
duke
parents:
diff changeset
   253
    private final DiagnosticPosition position;
06bc494ca11e Initial load
duke
parents:
diff changeset
   254
    private final int line;
06bc494ca11e Initial load
duke
parents:
diff changeset
   255
    private final int column;
06bc494ca11e Initial load
duke
parents:
diff changeset
   256
    private final String key;
942
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents: 939
diff changeset
   257
    protected Object[] args;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   258
    private boolean mandatory;
06bc494ca11e Initial load
duke
parents:
diff changeset
   259
06bc494ca11e Initial load
duke
parents:
diff changeset
   260
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   261
     * Create a diagnostic object.
06bc494ca11e Initial load
duke
parents:
diff changeset
   262
     * @param messages the resource for localized messages
06bc494ca11e Initial load
duke
parents:
diff changeset
   263
     * @param dt the type of diagnostic
06bc494ca11e Initial load
duke
parents:
diff changeset
   264
     * @param name the name of the source file, or null if none.
06bc494ca11e Initial load
duke
parents:
diff changeset
   265
     * @param pos the character offset within the source file, if given.
06bc494ca11e Initial load
duke
parents:
diff changeset
   266
     * @param key a resource key to identify the text of the diagnostic
06bc494ca11e Initial load
duke
parents:
diff changeset
   267
     * @param args arguments to be included in the text of the diagnostic
06bc494ca11e Initial load
duke
parents:
diff changeset
   268
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   269
    protected JCDiagnostic(Messages messages,
06bc494ca11e Initial load
duke
parents:
diff changeset
   270
                       DiagnosticType dt,
06bc494ca11e Initial load
duke
parents:
diff changeset
   271
                       boolean mandatory,
06bc494ca11e Initial load
duke
parents:
diff changeset
   272
                       DiagnosticSource source,
06bc494ca11e Initial load
duke
parents:
diff changeset
   273
                       DiagnosticPosition pos,
06bc494ca11e Initial load
duke
parents:
diff changeset
   274
                       String key,
06bc494ca11e Initial load
duke
parents:
diff changeset
   275
                       Object ... args) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   276
        if (source == null && pos != null && pos.getPreferredPosition() != Position.NOPOS)
06bc494ca11e Initial load
duke
parents:
diff changeset
   277
            throw new IllegalArgumentException();
06bc494ca11e Initial load
duke
parents:
diff changeset
   278
06bc494ca11e Initial load
duke
parents:
diff changeset
   279
        this.messages = messages;
06bc494ca11e Initial load
duke
parents:
diff changeset
   280
        this.type = dt;
06bc494ca11e Initial load
duke
parents:
diff changeset
   281
        this.mandatory = mandatory;
06bc494ca11e Initial load
duke
parents:
diff changeset
   282
        this.source = source;
06bc494ca11e Initial load
duke
parents:
diff changeset
   283
        this.position = pos;
06bc494ca11e Initial load
duke
parents:
diff changeset
   284
        this.key = key;
06bc494ca11e Initial load
duke
parents:
diff changeset
   285
        this.args = args;
06bc494ca11e Initial load
duke
parents:
diff changeset
   286
06bc494ca11e Initial load
duke
parents:
diff changeset
   287
        int n = (pos == null ? Position.NOPOS : pos.getPreferredPosition());
06bc494ca11e Initial load
duke
parents:
diff changeset
   288
        if (n == Position.NOPOS || source == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   289
            line = column = -1;
06bc494ca11e Initial load
duke
parents:
diff changeset
   290
        else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   291
            line = source.getLineNumber(n);
06bc494ca11e Initial load
duke
parents:
diff changeset
   292
            column = source.getColumnNumber(n);
06bc494ca11e Initial load
duke
parents:
diff changeset
   293
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   294
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   295
06bc494ca11e Initial load
duke
parents:
diff changeset
   296
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   297
     * Get the type of this diagnostic.
06bc494ca11e Initial load
duke
parents:
diff changeset
   298
     * @return the type of this diagnostic
06bc494ca11e Initial load
duke
parents:
diff changeset
   299
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   300
    public DiagnosticType getType() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   301
        return type;
06bc494ca11e Initial load
duke
parents:
diff changeset
   302
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   303
06bc494ca11e Initial load
duke
parents:
diff changeset
   304
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   305
     * Check whether or not this diagnostic is required to be shown.
06bc494ca11e Initial load
duke
parents:
diff changeset
   306
     * @return true if this diagnostic is required to be shown.
06bc494ca11e Initial load
duke
parents:
diff changeset
   307
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   308
    public boolean isMandatory() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   309
        return mandatory;
06bc494ca11e Initial load
duke
parents:
diff changeset
   310
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   311
06bc494ca11e Initial load
duke
parents:
diff changeset
   312
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   313
     * Get the name of the source file referred to by this diagnostic.
06bc494ca11e Initial load
duke
parents:
diff changeset
   314
     * @return the name of the source referred to with this diagnostic, or null if none
06bc494ca11e Initial load
duke
parents:
diff changeset
   315
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   316
    public JavaFileObject getSource() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   317
        if (source == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   318
            return null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   319
        else
06bc494ca11e Initial load
duke
parents:
diff changeset
   320
            return source.getFile();
06bc494ca11e Initial load
duke
parents:
diff changeset
   321
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   322
06bc494ca11e Initial load
duke
parents:
diff changeset
   323
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   324
     * Get the name of the source file referred to by this diagnostic.
06bc494ca11e Initial load
duke
parents:
diff changeset
   325
     * @return the name of the source referred to with this diagnostic, or null if none
06bc494ca11e Initial load
duke
parents:
diff changeset
   326
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   327
    public String getSourceName() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   328
        JavaFileObject s = getSource();
06bc494ca11e Initial load
duke
parents:
diff changeset
   329
        return s == null ? null : JavacFileManager.getJavacFileName(s);
06bc494ca11e Initial load
duke
parents:
diff changeset
   330
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   331
06bc494ca11e Initial load
duke
parents:
diff changeset
   332
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   333
     * Get the source referred to by this diagnostic.
06bc494ca11e Initial load
duke
parents:
diff changeset
   334
     * @return the source referred to with this diagnostic, or null if none
06bc494ca11e Initial load
duke
parents:
diff changeset
   335
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   336
    public DiagnosticSource getDiagnosticSource() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   337
        return source;
06bc494ca11e Initial load
duke
parents:
diff changeset
   338
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   339
06bc494ca11e Initial load
duke
parents:
diff changeset
   340
    protected int getIntStartPosition() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   341
        return (position == null ? Position.NOPOS : position.getStartPosition());
06bc494ca11e Initial load
duke
parents:
diff changeset
   342
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   343
06bc494ca11e Initial load
duke
parents:
diff changeset
   344
    protected int getIntPosition() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   345
        return (position == null ? Position.NOPOS : position.getPreferredPosition());
06bc494ca11e Initial load
duke
parents:
diff changeset
   346
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   347
06bc494ca11e Initial load
duke
parents:
diff changeset
   348
    protected int getIntEndPosition() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   349
        return (position == null ? Position.NOPOS : position.getEndPosition(source.getEndPosTable()));
06bc494ca11e Initial load
duke
parents:
diff changeset
   350
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   351
06bc494ca11e Initial load
duke
parents:
diff changeset
   352
    public long getStartPosition() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   353
        return getIntStartPosition();
06bc494ca11e Initial load
duke
parents:
diff changeset
   354
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   355
06bc494ca11e Initial load
duke
parents:
diff changeset
   356
    public long getPosition() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   357
        return getIntPosition();
06bc494ca11e Initial load
duke
parents:
diff changeset
   358
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   359
06bc494ca11e Initial load
duke
parents:
diff changeset
   360
    public long getEndPosition() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   361
        return getIntEndPosition();
06bc494ca11e Initial load
duke
parents:
diff changeset
   362
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   363
06bc494ca11e Initial load
duke
parents:
diff changeset
   364
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   365
     * Get the line number within the source referred to by this diagnostic.
06bc494ca11e Initial load
duke
parents:
diff changeset
   366
     * @return  the line number within the source referred to by this diagnostic
06bc494ca11e Initial load
duke
parents:
diff changeset
   367
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   368
    public long getLineNumber() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   369
        return line;
06bc494ca11e Initial load
duke
parents:
diff changeset
   370
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   371
06bc494ca11e Initial load
duke
parents:
diff changeset
   372
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   373
     * Get the column number within the line of source referred to by this diagnostic.
06bc494ca11e Initial load
duke
parents:
diff changeset
   374
     * @return  the column number within the line of source referred to by this diagnostic
06bc494ca11e Initial load
duke
parents:
diff changeset
   375
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   376
    public long getColumnNumber() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   377
        return column;
06bc494ca11e Initial load
duke
parents:
diff changeset
   378
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   379
06bc494ca11e Initial load
duke
parents:
diff changeset
   380
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   381
     * Get the arguments to be included in the text of the diagnostic.
06bc494ca11e Initial load
duke
parents:
diff changeset
   382
     * @return  the arguments to be included in the text of the diagnostic
06bc494ca11e Initial load
duke
parents:
diff changeset
   383
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   384
    public Object[] getArgs() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   385
        return args;
06bc494ca11e Initial load
duke
parents:
diff changeset
   386
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   387
06bc494ca11e Initial load
duke
parents:
diff changeset
   388
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   389
     * Get the prefix string associated with this type of diagnostic.
06bc494ca11e Initial load
duke
parents:
diff changeset
   390
     * @return the prefix string associated with this type of diagnostic
06bc494ca11e Initial load
duke
parents:
diff changeset
   391
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   392
    public String getPrefix() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   393
        return getPrefix(type);
06bc494ca11e Initial load
duke
parents:
diff changeset
   394
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   395
06bc494ca11e Initial load
duke
parents:
diff changeset
   396
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   397
     * Get the prefix string associated with a particular type of diagnostic.
06bc494ca11e Initial load
duke
parents:
diff changeset
   398
     * @return the prefix string associated with a particular type of diagnostic
06bc494ca11e Initial load
duke
parents:
diff changeset
   399
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   400
    public String getPrefix(DiagnosticType dt) {
942
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents: 939
diff changeset
   401
        return getFormatter().formatKind(this, Locale.getDefault());
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents: 939
diff changeset
   402
    }
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents: 939
diff changeset
   403
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents: 939
diff changeset
   404
     private DiagnosticFormatter<JCDiagnostic> getFormatter() {
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents: 939
diff changeset
   405
        if (defaultFormatter == null) {
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents: 939
diff changeset
   406
            defaultFormatter = new BasicDiagnosticFormatter(messages);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   407
        }
942
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents: 939
diff changeset
   408
        return defaultFormatter;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   409
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   410
942
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents: 939
diff changeset
   411
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   412
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   413
     * Return the standard presentation of this diagnostic.
06bc494ca11e Initial load
duke
parents:
diff changeset
   414
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   415
    public String toString() {
942
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents: 939
diff changeset
   416
        return getFormatter().format(this,Locale.getDefault());
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   417
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   418
942
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents: 939
diff changeset
   419
    private static DiagnosticFormatter<JCDiagnostic> defaultFormatter;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   420
06bc494ca11e Initial load
duke
parents:
diff changeset
   421
    // Methods for javax.tools.Diagnostic
06bc494ca11e Initial load
duke
parents:
diff changeset
   422
06bc494ca11e Initial load
duke
parents:
diff changeset
   423
    public Diagnostic.Kind getKind() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   424
        switch (type) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   425
        case NOTE:
06bc494ca11e Initial load
duke
parents:
diff changeset
   426
            return Diagnostic.Kind.NOTE;
06bc494ca11e Initial load
duke
parents:
diff changeset
   427
        case WARNING:
06bc494ca11e Initial load
duke
parents:
diff changeset
   428
            return mandatory ? Diagnostic.Kind.MANDATORY_WARNING
06bc494ca11e Initial load
duke
parents:
diff changeset
   429
                             : Diagnostic.Kind.WARNING;
06bc494ca11e Initial load
duke
parents:
diff changeset
   430
        case ERROR:
06bc494ca11e Initial load
duke
parents:
diff changeset
   431
            return Diagnostic.Kind.ERROR;
06bc494ca11e Initial load
duke
parents:
diff changeset
   432
        default:
06bc494ca11e Initial load
duke
parents:
diff changeset
   433
            return Diagnostic.Kind.OTHER;
06bc494ca11e Initial load
duke
parents:
diff changeset
   434
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   435
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   436
06bc494ca11e Initial load
duke
parents:
diff changeset
   437
    public String getCode() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   438
        return key;
06bc494ca11e Initial load
duke
parents:
diff changeset
   439
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   440
06bc494ca11e Initial load
duke
parents:
diff changeset
   441
    public String getMessage(Locale locale) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   442
        // RFE 6406133: JCDiagnostic.getMessage ignores locale argument
942
98e1d4f8aacd 6720185: DiagnosticFormatter refactoring
mcimadamore
parents: 939
diff changeset
   443
        return getFormatter().formatMessage(this, locale);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   444
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   445
}