langtools/src/share/classes/com/sun/tools/javac/util/MandatoryWarningHandler.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 10 06bc494ca11e
child 813 ab91293d33f4
permissions -rw-r--r--
Initial load
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     1
/*
06bc494ca11e Initial load
duke
parents:
diff changeset
     2
 * Copyright 2005-2006 Sun Microsystems, Inc.  All Rights Reserved.
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.HashSet;
06bc494ca11e Initial load
duke
parents:
diff changeset
    29
import java.util.Set;
06bc494ca11e Initial load
duke
parents:
diff changeset
    30
import javax.tools.JavaFileObject;
06bc494ca11e Initial load
duke
parents:
diff changeset
    31
06bc494ca11e Initial load
duke
parents:
diff changeset
    32
import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition;
06bc494ca11e Initial load
duke
parents:
diff changeset
    33
06bc494ca11e Initial load
duke
parents:
diff changeset
    34
06bc494ca11e Initial load
duke
parents:
diff changeset
    35
/**
06bc494ca11e Initial load
duke
parents:
diff changeset
    36
 * A handler to process mandatory warnings, setting up a deferred diagnostic
06bc494ca11e Initial load
duke
parents:
diff changeset
    37
 * to be printed at the end of the compilation if some warnings get suppressed
06bc494ca11e Initial load
duke
parents:
diff changeset
    38
 * because too many warnings have already been generated.
06bc494ca11e Initial load
duke
parents:
diff changeset
    39
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    40
 * Note that the SuppressWarnings annotation can be used to suppress warnings
06bc494ca11e Initial load
duke
parents:
diff changeset
    41
 * about conditions that would otherwise merit a warning. Such processing
06bc494ca11e Initial load
duke
parents:
diff changeset
    42
 * is done when the condition is detected, and in those cases, no call is
06bc494ca11e Initial load
duke
parents:
diff changeset
    43
 * made on any API to generate a warning at all. In consequence, this handler only
06bc494ca11e Initial load
duke
parents:
diff changeset
    44
 * gets to handle those warnings that JLS says must be generated.
06bc494ca11e Initial load
duke
parents:
diff changeset
    45
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    46
 *  <p><b>This is NOT part of any API supported by Sun Microsystems.  If
06bc494ca11e Initial load
duke
parents:
diff changeset
    47
 *  you write code that depends on this, you do so at your own risk.
06bc494ca11e Initial load
duke
parents:
diff changeset
    48
 *  This code and its internal interfaces are subject to change or
06bc494ca11e Initial load
duke
parents:
diff changeset
    49
 *  deletion without notice.</b>
06bc494ca11e Initial load
duke
parents:
diff changeset
    50
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    51
public class MandatoryWarningHandler {
06bc494ca11e Initial load
duke
parents:
diff changeset
    52
06bc494ca11e Initial load
duke
parents:
diff changeset
    53
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
    54
     * The kinds of different deferred diagnostics that might be generated
06bc494ca11e Initial load
duke
parents:
diff changeset
    55
     * if a mandatory warning is suppressed because too many warnings have
06bc494ca11e Initial load
duke
parents:
diff changeset
    56
     * already been output.
06bc494ca11e Initial load
duke
parents:
diff changeset
    57
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
    58
     * The parameter is a fragment used to build an I18N message key for Log.
06bc494ca11e Initial load
duke
parents:
diff changeset
    59
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    60
    private enum DeferredDiagnosticKind {
06bc494ca11e Initial load
duke
parents:
diff changeset
    61
        /**
06bc494ca11e Initial load
duke
parents:
diff changeset
    62
         * This kind is used when a single specific file is found to have warnings
06bc494ca11e Initial load
duke
parents:
diff changeset
    63
         * and no similar warnings have already been given.
06bc494ca11e Initial load
duke
parents:
diff changeset
    64
         * It generates a message like:
06bc494ca11e Initial load
duke
parents:
diff changeset
    65
         *      FILE has ISSUES
06bc494ca11e Initial load
duke
parents:
diff changeset
    66
         */
06bc494ca11e Initial load
duke
parents:
diff changeset
    67
        IN_FILE(".filename"),
06bc494ca11e Initial load
duke
parents:
diff changeset
    68
        /**
06bc494ca11e Initial load
duke
parents:
diff changeset
    69
         * This kind is used when a single specific file is found to have warnings
06bc494ca11e Initial load
duke
parents:
diff changeset
    70
         * and when similar warnings have already been reported for the file.
06bc494ca11e Initial load
duke
parents:
diff changeset
    71
         * It generates a message like:
06bc494ca11e Initial load
duke
parents:
diff changeset
    72
         *      FILE has additional ISSUES
06bc494ca11e Initial load
duke
parents:
diff changeset
    73
         */
06bc494ca11e Initial load
duke
parents:
diff changeset
    74
        ADDITIONAL_IN_FILE(".filename.additional"),
06bc494ca11e Initial load
duke
parents:
diff changeset
    75
        /**
06bc494ca11e Initial load
duke
parents:
diff changeset
    76
         * This kind is used when multiple files have been found to have warnings,
06bc494ca11e Initial load
duke
parents:
diff changeset
    77
         * and none of them have had any similar warnings.
06bc494ca11e Initial load
duke
parents:
diff changeset
    78
         * It generates a message like:
06bc494ca11e Initial load
duke
parents:
diff changeset
    79
         *      Some files have ISSUES
06bc494ca11e Initial load
duke
parents:
diff changeset
    80
         */
06bc494ca11e Initial load
duke
parents:
diff changeset
    81
        IN_FILES(".plural"),
06bc494ca11e Initial load
duke
parents:
diff changeset
    82
        /**
06bc494ca11e Initial load
duke
parents:
diff changeset
    83
         * This kind is used when multiple files have been found to have warnings,
06bc494ca11e Initial load
duke
parents:
diff changeset
    84
         * and some of them have had already had specific similar warnings.
06bc494ca11e Initial load
duke
parents:
diff changeset
    85
         * It generates a message like:
06bc494ca11e Initial load
duke
parents:
diff changeset
    86
         *      Some files have additional ISSUES
06bc494ca11e Initial load
duke
parents:
diff changeset
    87
         */
06bc494ca11e Initial load
duke
parents:
diff changeset
    88
        ADDITIONAL_IN_FILES(".plural.additional");
06bc494ca11e Initial load
duke
parents:
diff changeset
    89
06bc494ca11e Initial load
duke
parents:
diff changeset
    90
        DeferredDiagnosticKind(String v) { value = v; }
06bc494ca11e Initial load
duke
parents:
diff changeset
    91
        String getKey(String prefix) { return prefix + value; }
06bc494ca11e Initial load
duke
parents:
diff changeset
    92
06bc494ca11e Initial load
duke
parents:
diff changeset
    93
        private String value;
06bc494ca11e Initial load
duke
parents:
diff changeset
    94
    }
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
     * Create a handler for mandatory warnings.
06bc494ca11e Initial load
duke
parents:
diff changeset
    99
     * @param log     The log on which to generate any diagnostics
06bc494ca11e Initial load
duke
parents:
diff changeset
   100
     * @param verbose Specify whether or not detailed messages about
06bc494ca11e Initial load
duke
parents:
diff changeset
   101
     *                individual instances should be given, or whether an aggregate
06bc494ca11e Initial load
duke
parents:
diff changeset
   102
     *                message should be generated at the end of the compilation.
06bc494ca11e Initial load
duke
parents:
diff changeset
   103
     *                Typically set via  -Xlint:option.
06bc494ca11e Initial load
duke
parents:
diff changeset
   104
     * @param prefix  A common prefix for the set of message keys for
06bc494ca11e Initial load
duke
parents:
diff changeset
   105
     *                the messages that may be generated.
06bc494ca11e Initial load
duke
parents:
diff changeset
   106
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   107
    public MandatoryWarningHandler(Log log, boolean verbose, String prefix) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   108
        this.log = log;
06bc494ca11e Initial load
duke
parents:
diff changeset
   109
        this.verbose = verbose;
06bc494ca11e Initial load
duke
parents:
diff changeset
   110
        this.prefix = prefix;
06bc494ca11e Initial load
duke
parents:
diff changeset
   111
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   112
06bc494ca11e Initial load
duke
parents:
diff changeset
   113
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   114
     * Report a mandatory warning.
06bc494ca11e Initial load
duke
parents:
diff changeset
   115
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   116
    public void report(DiagnosticPosition pos, String msg, Object... args) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   117
        JavaFileObject currentSource = log.currentSource();
06bc494ca11e Initial load
duke
parents:
diff changeset
   118
06bc494ca11e Initial load
duke
parents:
diff changeset
   119
        if (verbose) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   120
            if (sourcesWithReportedWarnings == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   121
                sourcesWithReportedWarnings = new HashSet<JavaFileObject>();
06bc494ca11e Initial load
duke
parents:
diff changeset
   122
06bc494ca11e Initial load
duke
parents:
diff changeset
   123
            if (log.nwarnings < log.MaxWarnings) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   124
                // generate message and remember the source file
06bc494ca11e Initial load
duke
parents:
diff changeset
   125
                log.mandatoryWarning(pos, msg, args);
06bc494ca11e Initial load
duke
parents:
diff changeset
   126
                sourcesWithReportedWarnings.add(currentSource);
06bc494ca11e Initial load
duke
parents:
diff changeset
   127
            } else if (deferredDiagnosticKind == null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   128
                // set up deferred message
06bc494ca11e Initial load
duke
parents:
diff changeset
   129
                if (sourcesWithReportedWarnings.contains(currentSource)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   130
                    // more errors in a file that already has reported warnings
06bc494ca11e Initial load
duke
parents:
diff changeset
   131
                    deferredDiagnosticKind = DeferredDiagnosticKind.ADDITIONAL_IN_FILE;
06bc494ca11e Initial load
duke
parents:
diff changeset
   132
                } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   133
                    // warnings in a new source file
06bc494ca11e Initial load
duke
parents:
diff changeset
   134
                    deferredDiagnosticKind = DeferredDiagnosticKind.IN_FILE;
06bc494ca11e Initial load
duke
parents:
diff changeset
   135
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   136
                deferredDiagnosticSource = currentSource;
06bc494ca11e Initial load
duke
parents:
diff changeset
   137
                deferredDiagnosticArg = currentSource;
06bc494ca11e Initial load
duke
parents:
diff changeset
   138
            } else if ((deferredDiagnosticKind == DeferredDiagnosticKind.IN_FILE
06bc494ca11e Initial load
duke
parents:
diff changeset
   139
                        || deferredDiagnosticKind == DeferredDiagnosticKind.ADDITIONAL_IN_FILE)
06bc494ca11e Initial load
duke
parents:
diff changeset
   140
                       && !equal(deferredDiagnosticSource, currentSource)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   141
                // additional errors in more than one source file
06bc494ca11e Initial load
duke
parents:
diff changeset
   142
                deferredDiagnosticKind = DeferredDiagnosticKind.ADDITIONAL_IN_FILES;
06bc494ca11e Initial load
duke
parents:
diff changeset
   143
                deferredDiagnosticArg = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   144
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   145
        } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   146
            if (deferredDiagnosticKind == null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   147
                // warnings in a single source
06bc494ca11e Initial load
duke
parents:
diff changeset
   148
                deferredDiagnosticKind = DeferredDiagnosticKind.IN_FILE;
06bc494ca11e Initial load
duke
parents:
diff changeset
   149
                deferredDiagnosticSource = currentSource;
06bc494ca11e Initial load
duke
parents:
diff changeset
   150
                deferredDiagnosticArg = currentSource;
06bc494ca11e Initial load
duke
parents:
diff changeset
   151
            }  else if (deferredDiagnosticKind == DeferredDiagnosticKind.IN_FILE &&
06bc494ca11e Initial load
duke
parents:
diff changeset
   152
                        !equal(deferredDiagnosticSource, currentSource)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   153
                // warnings in multiple source files
06bc494ca11e Initial load
duke
parents:
diff changeset
   154
                deferredDiagnosticKind = DeferredDiagnosticKind.IN_FILES;
06bc494ca11e Initial load
duke
parents:
diff changeset
   155
                deferredDiagnosticArg = null;
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
     * Report any diagnostic that might have been deferred by previous calls of report().
06bc494ca11e Initial load
duke
parents:
diff changeset
   162
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   163
    public void reportDeferredDiagnostic() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   164
        if (deferredDiagnosticKind != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   165
            if (deferredDiagnosticArg == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   166
                log.mandatoryNote(deferredDiagnosticSource, deferredDiagnosticKind.getKey(prefix));
06bc494ca11e Initial load
duke
parents:
diff changeset
   167
            else
06bc494ca11e Initial load
duke
parents:
diff changeset
   168
                log.mandatoryNote(deferredDiagnosticSource, deferredDiagnosticKind.getKey(prefix), deferredDiagnosticArg);
06bc494ca11e Initial load
duke
parents:
diff changeset
   169
06bc494ca11e Initial load
duke
parents:
diff changeset
   170
            if (!verbose)
06bc494ca11e Initial load
duke
parents:
diff changeset
   171
                log.mandatoryNote(deferredDiagnosticSource, prefix + ".recompile");
06bc494ca11e Initial load
duke
parents:
diff changeset
   172
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   173
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   174
06bc494ca11e Initial load
duke
parents:
diff changeset
   175
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   176
     * Check two objects, each possibly null, are either both null or are equal.
06bc494ca11e Initial load
duke
parents:
diff changeset
   177
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   178
    private static boolean equal(Object o1, Object o2) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   179
        return ((o1 == null || o2 == null) ? (o1 == o2) : o1.equals(o2));
06bc494ca11e Initial load
duke
parents:
diff changeset
   180
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   181
06bc494ca11e Initial load
duke
parents:
diff changeset
   182
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   183
     * The log to which to report warnings.
06bc494ca11e Initial load
duke
parents:
diff changeset
   184
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   185
    private Log log;
06bc494ca11e Initial load
duke
parents:
diff changeset
   186
06bc494ca11e Initial load
duke
parents:
diff changeset
   187
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   188
     * Whether or not to report individual warnings, or simply to report a
06bc494ca11e Initial load
duke
parents:
diff changeset
   189
     * single aggregate warning at the end of the compilation.
06bc494ca11e Initial load
duke
parents:
diff changeset
   190
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   191
    private boolean verbose;
06bc494ca11e Initial load
duke
parents:
diff changeset
   192
06bc494ca11e Initial load
duke
parents:
diff changeset
   193
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   194
     * The common prefix for all I18N message keys generated by this handler.
06bc494ca11e Initial load
duke
parents:
diff changeset
   195
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   196
    private String prefix;
06bc494ca11e Initial load
duke
parents:
diff changeset
   197
06bc494ca11e Initial load
duke
parents:
diff changeset
   198
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   199
     * A set containing the names of the source files for which specific
06bc494ca11e Initial load
duke
parents:
diff changeset
   200
     * warnings have been generated -- i.e. in verbose mode.  If a source name
06bc494ca11e Initial load
duke
parents:
diff changeset
   201
     * appears in this list, then deferred diagnostics will be phrased to
06bc494ca11e Initial load
duke
parents:
diff changeset
   202
     * include "additionally"...
06bc494ca11e Initial load
duke
parents:
diff changeset
   203
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   204
    private Set<JavaFileObject> sourcesWithReportedWarnings;
06bc494ca11e Initial load
duke
parents:
diff changeset
   205
06bc494ca11e Initial load
duke
parents:
diff changeset
   206
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   207
     * A variable indicating the latest best guess at what the final
06bc494ca11e Initial load
duke
parents:
diff changeset
   208
     * deferred diagnostic will be. Initially as specific and helpful
06bc494ca11e Initial load
duke
parents:
diff changeset
   209
     * as possible, as more warnings are reported, the scope of the
06bc494ca11e Initial load
duke
parents:
diff changeset
   210
     * diagnostic will be broadened.
06bc494ca11e Initial load
duke
parents:
diff changeset
   211
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   212
    private DeferredDiagnosticKind deferredDiagnosticKind;
06bc494ca11e Initial load
duke
parents:
diff changeset
   213
06bc494ca11e Initial load
duke
parents:
diff changeset
   214
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   215
     * If deferredDiagnosticKind is IN_FILE or ADDITIONAL_IN_FILE, this variable
06bc494ca11e Initial load
duke
parents:
diff changeset
   216
     * gives the value of log.currentSource() for the file in question.
06bc494ca11e Initial load
duke
parents:
diff changeset
   217
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   218
    private JavaFileObject deferredDiagnosticSource;
06bc494ca11e Initial load
duke
parents:
diff changeset
   219
06bc494ca11e Initial load
duke
parents:
diff changeset
   220
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   221
     * An optional argument to be used when constructing the
06bc494ca11e Initial load
duke
parents:
diff changeset
   222
     * deferred diagnostic message, based on deferredDiagnosticKind.
06bc494ca11e Initial load
duke
parents:
diff changeset
   223
     * This variable should normally be set/updated whenever
06bc494ca11e Initial load
duke
parents:
diff changeset
   224
     * deferredDiagnosticKind is updated.
06bc494ca11e Initial load
duke
parents:
diff changeset
   225
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   226
    private Object deferredDiagnosticArg;
06bc494ca11e Initial load
duke
parents:
diff changeset
   227
}