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