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