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