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

Mon, 26 Jul 2010 14:25:56 -0700

author
jjg
date
Mon, 26 Jul 2010 14:25:56 -0700
changeset 612
d1bd93028447
parent 581
f2fdd52e4e87
child 798
4868a36f6fd8
permissions
-rw-r--r--

6957438: improve code for generating warning messages containing option names
Reviewed-by: mcimadamore

duke@1 1 /*
ohair@554 2 * Copyright (c) 2005, 2008, Oracle and/or its affiliates. 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
ohair@554 7 * published by the Free Software Foundation. Oracle designates this
duke@1 8 * particular file as subject to the "Classpath" exception as provided
ohair@554 9 * by Oracle 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 *
ohair@554 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ohair@554 22 * or visit www.oracle.com if you need additional information or have any
ohair@554 23 * 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
jjg@612 32 import com.sun.tools.javac.code.Lint.LintCategory;
duke@1 33 import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition;
duke@1 34
duke@1 35
duke@1 36 /**
duke@1 37 * A handler to process mandatory warnings, setting up a deferred diagnostic
duke@1 38 * to be printed at the end of the compilation if some warnings get suppressed
duke@1 39 * because too many warnings have already been generated.
duke@1 40 *
duke@1 41 * Note that the SuppressWarnings annotation can be used to suppress warnings
duke@1 42 * about conditions that would otherwise merit a warning. Such processing
duke@1 43 * is done when the condition is detected, and in those cases, no call is
duke@1 44 * made on any API to generate a warning at all. In consequence, this handler only
duke@1 45 * gets to handle those warnings that JLS says must be generated.
duke@1 46 *
jjg@581 47 * <p><b>This is NOT part of any supported API.
jjg@581 48 * If you write code that depends on this, you do so at your own risk.
duke@1 49 * This code and its internal interfaces are subject to change or
duke@1 50 * deletion without notice.</b>
duke@1 51 */
duke@1 52 public class MandatoryWarningHandler {
duke@1 53
duke@1 54 /**
duke@1 55 * The kinds of different deferred diagnostics that might be generated
duke@1 56 * if a mandatory warning is suppressed because too many warnings have
duke@1 57 * already been output.
duke@1 58 *
duke@1 59 * The parameter is a fragment used to build an I18N message key for Log.
duke@1 60 */
duke@1 61 private enum DeferredDiagnosticKind {
duke@1 62 /**
duke@1 63 * This kind is used when a single specific file is found to have warnings
duke@1 64 * and no similar warnings have already been given.
duke@1 65 * It generates a message like:
duke@1 66 * FILE has ISSUES
duke@1 67 */
duke@1 68 IN_FILE(".filename"),
duke@1 69 /**
duke@1 70 * This kind is used when a single specific file is found to have warnings
duke@1 71 * and when similar warnings have already been reported for the file.
duke@1 72 * It generates a message like:
duke@1 73 * FILE has additional ISSUES
duke@1 74 */
duke@1 75 ADDITIONAL_IN_FILE(".filename.additional"),
duke@1 76 /**
duke@1 77 * This kind is used when multiple files have been found to have warnings,
duke@1 78 * and none of them have had any similar warnings.
duke@1 79 * It generates a message like:
duke@1 80 * Some files have ISSUES
duke@1 81 */
duke@1 82 IN_FILES(".plural"),
duke@1 83 /**
duke@1 84 * This kind is used when multiple files have been found to have warnings,
duke@1 85 * and some of them have had already had specific similar warnings.
duke@1 86 * It generates a message like:
duke@1 87 * Some files have additional ISSUES
duke@1 88 */
duke@1 89 ADDITIONAL_IN_FILES(".plural.additional");
duke@1 90
duke@1 91 DeferredDiagnosticKind(String v) { value = v; }
duke@1 92 String getKey(String prefix) { return prefix + value; }
duke@1 93
duke@1 94 private String value;
duke@1 95 }
duke@1 96
duke@1 97
duke@1 98 /**
duke@1 99 * Create a handler for mandatory warnings.
duke@1 100 * @param log The log on which to generate any diagnostics
duke@1 101 * @param verbose Specify whether or not detailed messages about
duke@1 102 * individual instances should be given, or whether an aggregate
duke@1 103 * message should be generated at the end of the compilation.
duke@1 104 * Typically set via -Xlint:option.
jjg@60 105 * @param enforceMandatory
jjg@60 106 * True if mandatory warnings and notes are being enforced.
duke@1 107 * @param prefix A common prefix for the set of message keys for
duke@1 108 * the messages that may be generated.
jjg@612 109 * @param lc An associated lint category for the warnings, or null if none.
duke@1 110 */
jjg@60 111 public MandatoryWarningHandler(Log log, boolean verbose,
jjg@612 112 boolean enforceMandatory, String prefix,
jjg@612 113 LintCategory lc) {
duke@1 114 this.log = log;
duke@1 115 this.verbose = verbose;
duke@1 116 this.prefix = prefix;
jjg@60 117 this.enforceMandatory = enforceMandatory;
jjg@612 118 this.lintCategory = lc;
duke@1 119 }
duke@1 120
duke@1 121 /**
duke@1 122 * Report a mandatory warning.
duke@1 123 */
duke@1 124 public void report(DiagnosticPosition pos, String msg, Object... args) {
mcimadamore@168 125 JavaFileObject currentSource = log.currentSourceFile();
duke@1 126
duke@1 127 if (verbose) {
duke@1 128 if (sourcesWithReportedWarnings == null)
duke@1 129 sourcesWithReportedWarnings = new HashSet<JavaFileObject>();
duke@1 130
duke@1 131 if (log.nwarnings < log.MaxWarnings) {
duke@1 132 // generate message and remember the source file
jjg@60 133 logMandatoryWarning(pos, msg, args);
duke@1 134 sourcesWithReportedWarnings.add(currentSource);
duke@1 135 } else if (deferredDiagnosticKind == null) {
duke@1 136 // set up deferred message
duke@1 137 if (sourcesWithReportedWarnings.contains(currentSource)) {
duke@1 138 // more errors in a file that already has reported warnings
duke@1 139 deferredDiagnosticKind = DeferredDiagnosticKind.ADDITIONAL_IN_FILE;
duke@1 140 } else {
duke@1 141 // warnings in a new source file
duke@1 142 deferredDiagnosticKind = DeferredDiagnosticKind.IN_FILE;
duke@1 143 }
duke@1 144 deferredDiagnosticSource = currentSource;
duke@1 145 deferredDiagnosticArg = currentSource;
duke@1 146 } else if ((deferredDiagnosticKind == DeferredDiagnosticKind.IN_FILE
duke@1 147 || deferredDiagnosticKind == DeferredDiagnosticKind.ADDITIONAL_IN_FILE)
duke@1 148 && !equal(deferredDiagnosticSource, currentSource)) {
duke@1 149 // additional errors in more than one source file
duke@1 150 deferredDiagnosticKind = DeferredDiagnosticKind.ADDITIONAL_IN_FILES;
duke@1 151 deferredDiagnosticArg = null;
duke@1 152 }
duke@1 153 } else {
duke@1 154 if (deferredDiagnosticKind == null) {
duke@1 155 // warnings in a single source
duke@1 156 deferredDiagnosticKind = DeferredDiagnosticKind.IN_FILE;
duke@1 157 deferredDiagnosticSource = currentSource;
duke@1 158 deferredDiagnosticArg = currentSource;
duke@1 159 } else if (deferredDiagnosticKind == DeferredDiagnosticKind.IN_FILE &&
duke@1 160 !equal(deferredDiagnosticSource, currentSource)) {
duke@1 161 // warnings in multiple source files
duke@1 162 deferredDiagnosticKind = DeferredDiagnosticKind.IN_FILES;
duke@1 163 deferredDiagnosticArg = null;
duke@1 164 }
duke@1 165 }
duke@1 166 }
duke@1 167
duke@1 168 /**
duke@1 169 * Report any diagnostic that might have been deferred by previous calls of report().
duke@1 170 */
duke@1 171 public void reportDeferredDiagnostic() {
duke@1 172 if (deferredDiagnosticKind != null) {
duke@1 173 if (deferredDiagnosticArg == null)
jjg@60 174 logMandatoryNote(deferredDiagnosticSource, deferredDiagnosticKind.getKey(prefix));
duke@1 175 else
jjg@60 176 logMandatoryNote(deferredDiagnosticSource, deferredDiagnosticKind.getKey(prefix), deferredDiagnosticArg);
duke@1 177
duke@1 178 if (!verbose)
jjg@60 179 logMandatoryNote(deferredDiagnosticSource, prefix + ".recompile");
duke@1 180 }
duke@1 181 }
duke@1 182
duke@1 183 /**
duke@1 184 * Check two objects, each possibly null, are either both null or are equal.
duke@1 185 */
duke@1 186 private static boolean equal(Object o1, Object o2) {
duke@1 187 return ((o1 == null || o2 == null) ? (o1 == o2) : o1.equals(o2));
duke@1 188 }
duke@1 189
duke@1 190 /**
duke@1 191 * The log to which to report warnings.
duke@1 192 */
duke@1 193 private Log log;
duke@1 194
duke@1 195 /**
duke@1 196 * Whether or not to report individual warnings, or simply to report a
duke@1 197 * single aggregate warning at the end of the compilation.
duke@1 198 */
duke@1 199 private boolean verbose;
duke@1 200
duke@1 201 /**
duke@1 202 * The common prefix for all I18N message keys generated by this handler.
duke@1 203 */
duke@1 204 private String prefix;
duke@1 205
duke@1 206 /**
duke@1 207 * A set containing the names of the source files for which specific
duke@1 208 * warnings have been generated -- i.e. in verbose mode. If a source name
duke@1 209 * appears in this list, then deferred diagnostics will be phrased to
duke@1 210 * include "additionally"...
duke@1 211 */
duke@1 212 private Set<JavaFileObject> sourcesWithReportedWarnings;
duke@1 213
duke@1 214 /**
duke@1 215 * A variable indicating the latest best guess at what the final
duke@1 216 * deferred diagnostic will be. Initially as specific and helpful
duke@1 217 * as possible, as more warnings are reported, the scope of the
duke@1 218 * diagnostic will be broadened.
duke@1 219 */
duke@1 220 private DeferredDiagnosticKind deferredDiagnosticKind;
duke@1 221
duke@1 222 /**
duke@1 223 * If deferredDiagnosticKind is IN_FILE or ADDITIONAL_IN_FILE, this variable
duke@1 224 * gives the value of log.currentSource() for the file in question.
duke@1 225 */
duke@1 226 private JavaFileObject deferredDiagnosticSource;
duke@1 227
duke@1 228 /**
duke@1 229 * An optional argument to be used when constructing the
duke@1 230 * deferred diagnostic message, based on deferredDiagnosticKind.
duke@1 231 * This variable should normally be set/updated whenever
duke@1 232 * deferredDiagnosticKind is updated.
duke@1 233 */
duke@1 234 private Object deferredDiagnosticArg;
jjg@60 235
jjg@60 236 /**
jjg@60 237 * True if mandatory warnings and notes are being enforced.
jjg@60 238 */
jjg@60 239 private final boolean enforceMandatory;
jjg@60 240
jjg@60 241 /**
jjg@612 242 * A LintCategory to be included in point-of-use diagnostics to indicate
jjg@612 243 * how messages might be suppressed (i.e. with @SuppressWarnings).
jjg@612 244 */
jjg@612 245 private final LintCategory lintCategory;
jjg@612 246
jjg@612 247 /**
jjg@60 248 * Reports a mandatory warning to the log. If mandatory warnings
jjg@60 249 * are not being enforced, treat this as an ordinary warning.
jjg@60 250 */
jjg@60 251 private void logMandatoryWarning(DiagnosticPosition pos, String msg,
jjg@60 252 Object... args) {
jjg@612 253 // Note: the following log methods are safe if lintCategory is null.
jjg@60 254 if (enforceMandatory)
jjg@612 255 log.mandatoryWarning(lintCategory, pos, msg, args);
jjg@60 256 else
jjg@612 257 log.warning(lintCategory, pos, msg, args);
jjg@60 258 }
jjg@60 259
jjg@60 260 /**
jjg@60 261 * Reports a mandatory note to the log. If mandatory notes are
jjg@60 262 * not being enforced, treat this as an ordinary note.
jjg@60 263 */
jjg@60 264 private void logMandatoryNote(JavaFileObject file, String msg, Object... args) {
jjg@60 265 if (enforceMandatory)
jjg@60 266 log.mandatoryNote(file, msg, args);
jjg@60 267 else
jjg@60 268 log.note(file, msg, args);
jjg@60 269 }
duke@1 270 }

mercurial