langtools/src/share/classes/com/sun/tools/apt/util/Bark.java
author mcimadamore
Thu, 09 Oct 2008 16:07:38 +0100
changeset 1471 57506cdfb7b4
parent 1264 076a3cde30d5
child 5520 86e4b9a9da40
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
/*
1264
076a3cde30d5 6754988: Update copyright year
xdono
parents: 1047
diff changeset
     2
 * Copyright 2004-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.apt.util;
06bc494ca11e Initial load
duke
parents:
diff changeset
    27
06bc494ca11e Initial load
duke
parents:
diff changeset
    28
import com.sun.tools.javac.util.Context;
06bc494ca11e Initial load
duke
parents:
diff changeset
    29
import com.sun.tools.javac.util.JCDiagnostic;
06bc494ca11e Initial load
duke
parents:
diff changeset
    30
import com.sun.tools.javac.util.JCDiagnostic.SimpleDiagnosticPosition;
06bc494ca11e Initial load
duke
parents:
diff changeset
    31
import com.sun.tools.javac.util.Log;
1471
57506cdfb7b4 6406133: JCDiagnostic.getMessage ignores locale argument
mcimadamore
parents: 1264
diff changeset
    32
import com.sun.tools.javac.util.JavacMessages;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    33
import com.sun.tools.javac.util.Position;
06bc494ca11e Initial load
duke
parents:
diff changeset
    34
06bc494ca11e Initial load
duke
parents:
diff changeset
    35
/** A subtype of Log for use in APT.
06bc494ca11e Initial load
duke
parents:
diff changeset
    36
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    37
 *  <p><b>This is NOT part of any API supported by Sun Microsystems.  If
06bc494ca11e Initial load
duke
parents:
diff changeset
    38
 *  you write code that depends on this, you do so at your own risk.
06bc494ca11e Initial load
duke
parents:
diff changeset
    39
 *  This code and its internal interfaces are subject to change or
06bc494ca11e Initial load
duke
parents:
diff changeset
    40
 *  deletion without notice.</b>
06bc494ca11e Initial load
duke
parents:
diff changeset
    41
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    42
public class Bark extends Log {
06bc494ca11e Initial load
duke
parents:
diff changeset
    43
    /** The context key for the bark. */
06bc494ca11e Initial load
duke
parents:
diff changeset
    44
    protected static final Context.Key<Bark> barkKey =
06bc494ca11e Initial load
duke
parents:
diff changeset
    45
        new Context.Key<Bark>();
06bc494ca11e Initial load
duke
parents:
diff changeset
    46
06bc494ca11e Initial load
duke
parents:
diff changeset
    47
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
    48
     * Preregisters factories to create and use a Bark object for use as
06bc494ca11e Initial load
duke
parents:
diff changeset
    49
     * both a Log and a Bark.
06bc494ca11e Initial load
duke
parents:
diff changeset
    50
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    51
    public static void preRegister(final Context context) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    52
        context.put(barkKey, new Context.Factory<Bark>() {
06bc494ca11e Initial load
duke
parents:
diff changeset
    53
            public Bark make() {
06bc494ca11e Initial load
duke
parents:
diff changeset
    54
                return new Bark(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
    55
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
    56
        });
06bc494ca11e Initial load
duke
parents:
diff changeset
    57
        context.put(Log.logKey, new Context.Factory<Log>() {
06bc494ca11e Initial load
duke
parents:
diff changeset
    58
            public Log make() {
06bc494ca11e Initial load
duke
parents:
diff changeset
    59
                return Bark.instance(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
    60
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
    61
        });
06bc494ca11e Initial load
duke
parents:
diff changeset
    62
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    63
06bc494ca11e Initial load
duke
parents:
diff changeset
    64
    /** Get the Bark instance for this context. */
06bc494ca11e Initial load
duke
parents:
diff changeset
    65
    public static Bark instance(Context context) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    66
        Bark instance = context.get(barkKey);
06bc494ca11e Initial load
duke
parents:
diff changeset
    67
        if (instance == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
    68
            instance = new Bark(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
    69
        return instance;
06bc494ca11e Initial load
duke
parents:
diff changeset
    70
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    71
06bc494ca11e Initial load
duke
parents:
diff changeset
    72
    /** Specifies whether or not to ignore any diagnostics that are reported.
06bc494ca11e Initial load
duke
parents:
diff changeset
    73
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    74
    private boolean ignoreDiagnostics;
06bc494ca11e Initial load
duke
parents:
diff changeset
    75
06bc494ca11e Initial load
duke
parents:
diff changeset
    76
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
    77
     * Factory for APT-specific diagnostics.
06bc494ca11e Initial load
duke
parents:
diff changeset
    78
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    79
    private JCDiagnostic.Factory aptDiags;
06bc494ca11e Initial load
duke
parents:
diff changeset
    80
06bc494ca11e Initial load
duke
parents:
diff changeset
    81
06bc494ca11e Initial load
duke
parents:
diff changeset
    82
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
    83
     * Creates a Bark.
06bc494ca11e Initial load
duke
parents:
diff changeset
    84
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    85
    protected Bark(Context context) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    86
        super(context); // will register this object in context with Log.logKey
06bc494ca11e Initial load
duke
parents:
diff changeset
    87
        context.put(barkKey, this);
06bc494ca11e Initial load
duke
parents:
diff changeset
    88
06bc494ca11e Initial load
duke
parents:
diff changeset
    89
        // register additional resource bundle for APT messages.
1471
57506cdfb7b4 6406133: JCDiagnostic.getMessage ignores locale argument
mcimadamore
parents: 1264
diff changeset
    90
        JavacMessages aptMessages = JavacMessages.instance(context);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    91
        aptMessages.add("com.sun.tools.apt.resources.apt");
06bc494ca11e Initial load
duke
parents:
diff changeset
    92
        aptDiags = new JCDiagnostic.Factory(aptMessages, "apt");
06bc494ca11e Initial load
duke
parents:
diff changeset
    93
06bc494ca11e Initial load
duke
parents:
diff changeset
    94
        multipleErrors = true;
06bc494ca11e Initial load
duke
parents:
diff changeset
    95
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    96
06bc494ca11e Initial load
duke
parents:
diff changeset
    97
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
    98
     * Sets a flag indicating whether or not to ignore all diagnostics.
06bc494ca11e Initial load
duke
parents:
diff changeset
    99
     * When ignored, they are not reported to the output writers, not are they
06bc494ca11e Initial load
duke
parents:
diff changeset
   100
     * counted in the various counters.
06bc494ca11e Initial load
duke
parents:
diff changeset
   101
     * @param b If true, subsequent diagnostics will be ignored.
06bc494ca11e Initial load
duke
parents:
diff changeset
   102
     * @return the previous state of the flag
06bc494ca11e Initial load
duke
parents:
diff changeset
   103
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   104
    public boolean setDiagnosticsIgnored(boolean b) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   105
        boolean prev = ignoreDiagnostics;
06bc494ca11e Initial load
duke
parents:
diff changeset
   106
        ignoreDiagnostics = b;
06bc494ca11e Initial load
duke
parents:
diff changeset
   107
        return prev;
06bc494ca11e Initial load
duke
parents:
diff changeset
   108
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   109
06bc494ca11e Initial load
duke
parents:
diff changeset
   110
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   111
     * Report a diagnostic if they are not currently being ignored.
06bc494ca11e Initial load
duke
parents:
diff changeset
   112
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   113
    @Override
06bc494ca11e Initial load
duke
parents:
diff changeset
   114
    public void report(JCDiagnostic diagnostic) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   115
        if (ignoreDiagnostics)
06bc494ca11e Initial load
duke
parents:
diff changeset
   116
            return;
06bc494ca11e Initial load
duke
parents:
diff changeset
   117
06bc494ca11e Initial load
duke
parents:
diff changeset
   118
        super.report(diagnostic);
06bc494ca11e Initial load
duke
parents:
diff changeset
   119
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   120
06bc494ca11e Initial load
duke
parents:
diff changeset
   121
    /** Report an error.
06bc494ca11e Initial load
duke
parents:
diff changeset
   122
     *  @param key    The key for the localized error message.
06bc494ca11e Initial load
duke
parents:
diff changeset
   123
     *  @param args   Fields of the error message.
06bc494ca11e Initial load
duke
parents:
diff changeset
   124
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   125
    public void aptError(String key, Object... args) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   126
        aptError(Position.NOPOS, key, args);
06bc494ca11e Initial load
duke
parents:
diff changeset
   127
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   128
06bc494ca11e Initial load
duke
parents:
diff changeset
   129
    /** Report an error, unless another error was already reported at same
06bc494ca11e Initial load
duke
parents:
diff changeset
   130
     *  source position.
06bc494ca11e Initial load
duke
parents:
diff changeset
   131
     *  @param pos    The source position at which to report the error.
06bc494ca11e Initial load
duke
parents:
diff changeset
   132
     *  @param key    The key for the localized error message.
06bc494ca11e Initial load
duke
parents:
diff changeset
   133
     *  @param args   Fields of the error message.
06bc494ca11e Initial load
duke
parents:
diff changeset
   134
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   135
    public void aptError(int pos, String key, Object ... args) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   136
        report(aptDiags.error(source, new SimpleDiagnosticPosition(pos), key, args));
06bc494ca11e Initial load
duke
parents:
diff changeset
   137
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   138
06bc494ca11e Initial load
duke
parents:
diff changeset
   139
    /** Report a warning, unless suppressed by the  -nowarn option or the
06bc494ca11e Initial load
duke
parents:
diff changeset
   140
     *  maximum number of warnings has been reached.
06bc494ca11e Initial load
duke
parents:
diff changeset
   141
     *  @param key    The key for the localized warning message.
06bc494ca11e Initial load
duke
parents:
diff changeset
   142
     *  @param args   Fields of the warning message.
06bc494ca11e Initial load
duke
parents:
diff changeset
   143
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   144
    public void aptWarning(String key, Object... args) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   145
        aptWarning(Position.NOPOS, 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
    /** Report a warning, unless suppressed by the  -nowarn option or the
06bc494ca11e Initial load
duke
parents:
diff changeset
   149
     *  maximum number of warnings has been reached.
06bc494ca11e Initial load
duke
parents:
diff changeset
   150
     *  @param pos    The source position at which to report the warning.
06bc494ca11e Initial load
duke
parents:
diff changeset
   151
     *  @param key    The key for the localized warning message.
06bc494ca11e Initial load
duke
parents:
diff changeset
   152
     *  @param args   Fields of the warning message.
06bc494ca11e Initial load
duke
parents:
diff changeset
   153
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   154
    public void aptWarning(int pos, String key, Object ... args) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   155
        report(aptDiags.warning(source, new SimpleDiagnosticPosition(pos), key, args));
06bc494ca11e Initial load
duke
parents:
diff changeset
   156
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   157
06bc494ca11e Initial load
duke
parents:
diff changeset
   158
    /** Report a note, unless suppressed by the  -nowarn option.
06bc494ca11e Initial load
duke
parents:
diff changeset
   159
     *  @param key    The key for the localized note message.
06bc494ca11e Initial load
duke
parents:
diff changeset
   160
     *  @param args   Fields of the note message.
06bc494ca11e Initial load
duke
parents:
diff changeset
   161
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   162
    public void aptNote(String key, Object... args) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   163
        aptNote(Position.NOPOS, key, args);
06bc494ca11e Initial load
duke
parents:
diff changeset
   164
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   165
06bc494ca11e Initial load
duke
parents:
diff changeset
   166
    /** Report a note, unless suppressed by the  -nowarn option.
06bc494ca11e Initial load
duke
parents:
diff changeset
   167
     *  @param pos    The source position at which to report the note.
06bc494ca11e Initial load
duke
parents:
diff changeset
   168
     *  @param key    The key for the localized note message.
06bc494ca11e Initial load
duke
parents:
diff changeset
   169
     *  @param args   Fields of the note message.
06bc494ca11e Initial load
duke
parents:
diff changeset
   170
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   171
    public void aptNote(int pos, String key, Object ... args) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   172
        report(aptDiags.note(source, new SimpleDiagnosticPosition(pos), key, args));
06bc494ca11e Initial load
duke
parents:
diff changeset
   173
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   174
}