src/share/classes/com/sun/tools/javac/util/MandatoryWarningHandler.java

Sat, 01 Dec 2007 00:00:00 +0000

author
duke
date
Sat, 01 Dec 2007 00:00:00 +0000
changeset 1
9a66ca7c79fa
child 60
29d2485c1085
permissions
-rw-r--r--

Initial load

duke@1 1 /*
duke@1 2 * Copyright 2005-2006 Sun Microsystems, Inc. All Rights Reserved.
duke@1 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
duke@1 4 *
duke@1 5 * This code is free software; you can redistribute it and/or modify it
duke@1 6 * under the terms of the GNU General Public License version 2 only, as
duke@1 7 * published by the Free Software Foundation. Sun designates this
duke@1 8 * particular file as subject to the "Classpath" exception as provided
duke@1 9 * by Sun in the LICENSE file that accompanied this code.
duke@1 10 *
duke@1 11 * This code is distributed in the hope that it will be useful, but WITHOUT
duke@1 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
duke@1 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
duke@1 14 * version 2 for more details (a copy is included in the LICENSE file that
duke@1 15 * accompanied this code).
duke@1 16 *
duke@1 17 * You should have received a copy of the GNU General Public License version
duke@1 18 * 2 along with this work; if not, write to the Free Software Foundation,
duke@1 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
duke@1 20 *
duke@1 21 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
duke@1 22 * CA 95054 USA or visit www.sun.com if you need additional information or
duke@1 23 * have any questions.
duke@1 24 */
duke@1 25
duke@1 26 package com.sun.tools.javac.util;
duke@1 27
duke@1 28 import java.util.HashSet;
duke@1 29 import java.util.Set;
duke@1 30 import javax.tools.JavaFileObject;
duke@1 31
duke@1 32 import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition;
duke@1 33
duke@1 34
duke@1 35 /**
duke@1 36 * A handler to process mandatory warnings, setting up a deferred diagnostic
duke@1 37 * to be printed at the end of the compilation if some warnings get suppressed
duke@1 38 * because too many warnings have already been generated.
duke@1 39 *
duke@1 40 * Note that the SuppressWarnings annotation can be used to suppress warnings
duke@1 41 * about conditions that would otherwise merit a warning. Such processing
duke@1 42 * is done when the condition is detected, and in those cases, no call is
duke@1 43 * made on any API to generate a warning at all. In consequence, this handler only
duke@1 44 * gets to handle those warnings that JLS says must be generated.
duke@1 45 *
duke@1 46 * <p><b>This is NOT part of any API supported by Sun Microsystems. If
duke@1 47 * you write code that depends on this, you do so at your own risk.
duke@1 48 * This code and its internal interfaces are subject to change or
duke@1 49 * deletion without notice.</b>
duke@1 50 */
duke@1 51 public class MandatoryWarningHandler {
duke@1 52
duke@1 53 /**
duke@1 54 * The kinds of different deferred diagnostics that might be generated
duke@1 55 * if a mandatory warning is suppressed because too many warnings have
duke@1 56 * already been output.
duke@1 57 *
duke@1 58 * The parameter is a fragment used to build an I18N message key for Log.
duke@1 59 */
duke@1 60 private enum DeferredDiagnosticKind {
duke@1 61 /**
duke@1 62 * This kind is used when a single specific file is found to have warnings
duke@1 63 * and no similar warnings have already been given.
duke@1 64 * It generates a message like:
duke@1 65 * FILE has ISSUES
duke@1 66 */
duke@1 67 IN_FILE(".filename"),
duke@1 68 /**
duke@1 69 * This kind is used when a single specific file is found to have warnings
duke@1 70 * and when similar warnings have already been reported for the file.
duke@1 71 * It generates a message like:
duke@1 72 * FILE has additional ISSUES
duke@1 73 */
duke@1 74 ADDITIONAL_IN_FILE(".filename.additional"),
duke@1 75 /**
duke@1 76 * This kind is used when multiple files have been found to have warnings,
duke@1 77 * and none of them have had any similar warnings.
duke@1 78 * It generates a message like:
duke@1 79 * Some files have ISSUES
duke@1 80 */
duke@1 81 IN_FILES(".plural"),
duke@1 82 /**
duke@1 83 * This kind is used when multiple files have been found to have warnings,
duke@1 84 * and some of them have had already had specific similar warnings.
duke@1 85 * It generates a message like:
duke@1 86 * Some files have additional ISSUES
duke@1 87 */
duke@1 88 ADDITIONAL_IN_FILES(".plural.additional");
duke@1 89
duke@1 90 DeferredDiagnosticKind(String v) { value = v; }
duke@1 91 String getKey(String prefix) { return prefix + value; }
duke@1 92
duke@1 93 private String value;
duke@1 94 }
duke@1 95
duke@1 96
duke@1 97 /**
duke@1 98 * Create a handler for mandatory warnings.
duke@1 99 * @param log The log on which to generate any diagnostics
duke@1 100 * @param verbose Specify whether or not detailed messages about
duke@1 101 * individual instances should be given, or whether an aggregate
duke@1 102 * message should be generated at the end of the compilation.
duke@1 103 * Typically set via -Xlint:option.
duke@1 104 * @param prefix A common prefix for the set of message keys for
duke@1 105 * the messages that may be generated.
duke@1 106 */
duke@1 107 public MandatoryWarningHandler(Log log, boolean verbose, String prefix) {
duke@1 108 this.log = log;
duke@1 109 this.verbose = verbose;
duke@1 110 this.prefix = prefix;
duke@1 111 }
duke@1 112
duke@1 113 /**
duke@1 114 * Report a mandatory warning.
duke@1 115 */
duke@1 116 public void report(DiagnosticPosition pos, String msg, Object... args) {
duke@1 117 JavaFileObject currentSource = log.currentSource();
duke@1 118
duke@1 119 if (verbose) {
duke@1 120 if (sourcesWithReportedWarnings == null)
duke@1 121 sourcesWithReportedWarnings = new HashSet<JavaFileObject>();
duke@1 122
duke@1 123 if (log.nwarnings < log.MaxWarnings) {
duke@1 124 // generate message and remember the source file
duke@1 125 log.mandatoryWarning(pos, msg, args);
duke@1 126 sourcesWithReportedWarnings.add(currentSource);
duke@1 127 } else if (deferredDiagnosticKind == null) {
duke@1 128 // set up deferred message
duke@1 129 if (sourcesWithReportedWarnings.contains(currentSource)) {
duke@1 130 // more errors in a file that already has reported warnings
duke@1 131 deferredDiagnosticKind = DeferredDiagnosticKind.ADDITIONAL_IN_FILE;
duke@1 132 } else {
duke@1 133 // warnings in a new source file
duke@1 134 deferredDiagnosticKind = DeferredDiagnosticKind.IN_FILE;
duke@1 135 }
duke@1 136 deferredDiagnosticSource = currentSource;
duke@1 137 deferredDiagnosticArg = currentSource;
duke@1 138 } else if ((deferredDiagnosticKind == DeferredDiagnosticKind.IN_FILE
duke@1 139 || deferredDiagnosticKind == DeferredDiagnosticKind.ADDITIONAL_IN_FILE)
duke@1 140 && !equal(deferredDiagnosticSource, currentSource)) {
duke@1 141 // additional errors in more than one source file
duke@1 142 deferredDiagnosticKind = DeferredDiagnosticKind.ADDITIONAL_IN_FILES;
duke@1 143 deferredDiagnosticArg = null;
duke@1 144 }
duke@1 145 } else {
duke@1 146 if (deferredDiagnosticKind == null) {
duke@1 147 // warnings in a single source
duke@1 148 deferredDiagnosticKind = DeferredDiagnosticKind.IN_FILE;
duke@1 149 deferredDiagnosticSource = currentSource;
duke@1 150 deferredDiagnosticArg = currentSource;
duke@1 151 } else if (deferredDiagnosticKind == DeferredDiagnosticKind.IN_FILE &&
duke@1 152 !equal(deferredDiagnosticSource, currentSource)) {
duke@1 153 // warnings in multiple source files
duke@1 154 deferredDiagnosticKind = DeferredDiagnosticKind.IN_FILES;
duke@1 155 deferredDiagnosticArg = null;
duke@1 156 }
duke@1 157 }
duke@1 158 }
duke@1 159
duke@1 160 /**
duke@1 161 * Report any diagnostic that might have been deferred by previous calls of report().
duke@1 162 */
duke@1 163 public void reportDeferredDiagnostic() {
duke@1 164 if (deferredDiagnosticKind != null) {
duke@1 165 if (deferredDiagnosticArg == null)
duke@1 166 log.mandatoryNote(deferredDiagnosticSource, deferredDiagnosticKind.getKey(prefix));
duke@1 167 else
duke@1 168 log.mandatoryNote(deferredDiagnosticSource, deferredDiagnosticKind.getKey(prefix), deferredDiagnosticArg);
duke@1 169
duke@1 170 if (!verbose)
duke@1 171 log.mandatoryNote(deferredDiagnosticSource, prefix + ".recompile");
duke@1 172 }
duke@1 173 }
duke@1 174
duke@1 175 /**
duke@1 176 * Check two objects, each possibly null, are either both null or are equal.
duke@1 177 */
duke@1 178 private static boolean equal(Object o1, Object o2) {
duke@1 179 return ((o1 == null || o2 == null) ? (o1 == o2) : o1.equals(o2));
duke@1 180 }
duke@1 181
duke@1 182 /**
duke@1 183 * The log to which to report warnings.
duke@1 184 */
duke@1 185 private Log log;
duke@1 186
duke@1 187 /**
duke@1 188 * Whether or not to report individual warnings, or simply to report a
duke@1 189 * single aggregate warning at the end of the compilation.
duke@1 190 */
duke@1 191 private boolean verbose;
duke@1 192
duke@1 193 /**
duke@1 194 * The common prefix for all I18N message keys generated by this handler.
duke@1 195 */
duke@1 196 private String prefix;
duke@1 197
duke@1 198 /**
duke@1 199 * A set containing the names of the source files for which specific
duke@1 200 * warnings have been generated -- i.e. in verbose mode. If a source name
duke@1 201 * appears in this list, then deferred diagnostics will be phrased to
duke@1 202 * include "additionally"...
duke@1 203 */
duke@1 204 private Set<JavaFileObject> sourcesWithReportedWarnings;
duke@1 205
duke@1 206 /**
duke@1 207 * A variable indicating the latest best guess at what the final
duke@1 208 * deferred diagnostic will be. Initially as specific and helpful
duke@1 209 * as possible, as more warnings are reported, the scope of the
duke@1 210 * diagnostic will be broadened.
duke@1 211 */
duke@1 212 private DeferredDiagnosticKind deferredDiagnosticKind;
duke@1 213
duke@1 214 /**
duke@1 215 * If deferredDiagnosticKind is IN_FILE or ADDITIONAL_IN_FILE, this variable
duke@1 216 * gives the value of log.currentSource() for the file in question.
duke@1 217 */
duke@1 218 private JavaFileObject deferredDiagnosticSource;
duke@1 219
duke@1 220 /**
duke@1 221 * An optional argument to be used when constructing the
duke@1 222 * deferred diagnostic message, based on deferredDiagnosticKind.
duke@1 223 * This variable should normally be set/updated whenever
duke@1 224 * deferredDiagnosticKind is updated.
duke@1 225 */
duke@1 226 private Object deferredDiagnosticArg;
duke@1 227 }

mercurial