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

Wed, 12 Nov 2008 14:17:03 +0000

author
mcimadamore
date
Wed, 12 Nov 2008 14:17:03 +0000
changeset 168
4cdaaf4c5dca
parent 60
29d2485c1085
child 174
fdfed22db054
permissions
-rw-r--r--

6768932: Add support for multiline diagnostics
Summary: Added basic support for multiline/tabular diagnostics
Reviewed-by: jjg

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.
jjg@60 104 * @param enforceMandatory
jjg@60 105 * True if mandatory warnings and notes are being enforced.
duke@1 106 * @param prefix A common prefix for the set of message keys for
duke@1 107 * the messages that may be generated.
duke@1 108 */
jjg@60 109 public MandatoryWarningHandler(Log log, boolean verbose,
jjg@60 110 boolean enforceMandatory, String prefix) {
duke@1 111 this.log = log;
duke@1 112 this.verbose = verbose;
duke@1 113 this.prefix = prefix;
jjg@60 114 this.enforceMandatory = enforceMandatory;
duke@1 115 }
duke@1 116
duke@1 117 /**
duke@1 118 * Report a mandatory warning.
duke@1 119 */
duke@1 120 public void report(DiagnosticPosition pos, String msg, Object... args) {
mcimadamore@168 121 JavaFileObject currentSource = log.currentSourceFile();
duke@1 122
duke@1 123 if (verbose) {
duke@1 124 if (sourcesWithReportedWarnings == null)
duke@1 125 sourcesWithReportedWarnings = new HashSet<JavaFileObject>();
duke@1 126
duke@1 127 if (log.nwarnings < log.MaxWarnings) {
duke@1 128 // generate message and remember the source file
jjg@60 129 logMandatoryWarning(pos, msg, args);
duke@1 130 sourcesWithReportedWarnings.add(currentSource);
duke@1 131 } else if (deferredDiagnosticKind == null) {
duke@1 132 // set up deferred message
duke@1 133 if (sourcesWithReportedWarnings.contains(currentSource)) {
duke@1 134 // more errors in a file that already has reported warnings
duke@1 135 deferredDiagnosticKind = DeferredDiagnosticKind.ADDITIONAL_IN_FILE;
duke@1 136 } else {
duke@1 137 // warnings in a new source file
duke@1 138 deferredDiagnosticKind = DeferredDiagnosticKind.IN_FILE;
duke@1 139 }
duke@1 140 deferredDiagnosticSource = currentSource;
duke@1 141 deferredDiagnosticArg = currentSource;
duke@1 142 } else if ((deferredDiagnosticKind == DeferredDiagnosticKind.IN_FILE
duke@1 143 || deferredDiagnosticKind == DeferredDiagnosticKind.ADDITIONAL_IN_FILE)
duke@1 144 && !equal(deferredDiagnosticSource, currentSource)) {
duke@1 145 // additional errors in more than one source file
duke@1 146 deferredDiagnosticKind = DeferredDiagnosticKind.ADDITIONAL_IN_FILES;
duke@1 147 deferredDiagnosticArg = null;
duke@1 148 }
duke@1 149 } else {
duke@1 150 if (deferredDiagnosticKind == null) {
duke@1 151 // warnings in a single source
duke@1 152 deferredDiagnosticKind = DeferredDiagnosticKind.IN_FILE;
duke@1 153 deferredDiagnosticSource = currentSource;
duke@1 154 deferredDiagnosticArg = currentSource;
duke@1 155 } else if (deferredDiagnosticKind == DeferredDiagnosticKind.IN_FILE &&
duke@1 156 !equal(deferredDiagnosticSource, currentSource)) {
duke@1 157 // warnings in multiple source files
duke@1 158 deferredDiagnosticKind = DeferredDiagnosticKind.IN_FILES;
duke@1 159 deferredDiagnosticArg = null;
duke@1 160 }
duke@1 161 }
duke@1 162 }
duke@1 163
duke@1 164 /**
duke@1 165 * Report any diagnostic that might have been deferred by previous calls of report().
duke@1 166 */
duke@1 167 public void reportDeferredDiagnostic() {
duke@1 168 if (deferredDiagnosticKind != null) {
duke@1 169 if (deferredDiagnosticArg == null)
jjg@60 170 logMandatoryNote(deferredDiagnosticSource, deferredDiagnosticKind.getKey(prefix));
duke@1 171 else
jjg@60 172 logMandatoryNote(deferredDiagnosticSource, deferredDiagnosticKind.getKey(prefix), deferredDiagnosticArg);
duke@1 173
duke@1 174 if (!verbose)
jjg@60 175 logMandatoryNote(deferredDiagnosticSource, prefix + ".recompile");
duke@1 176 }
duke@1 177 }
duke@1 178
duke@1 179 /**
duke@1 180 * Check two objects, each possibly null, are either both null or are equal.
duke@1 181 */
duke@1 182 private static boolean equal(Object o1, Object o2) {
duke@1 183 return ((o1 == null || o2 == null) ? (o1 == o2) : o1.equals(o2));
duke@1 184 }
duke@1 185
duke@1 186 /**
duke@1 187 * The log to which to report warnings.
duke@1 188 */
duke@1 189 private Log log;
duke@1 190
duke@1 191 /**
duke@1 192 * Whether or not to report individual warnings, or simply to report a
duke@1 193 * single aggregate warning at the end of the compilation.
duke@1 194 */
duke@1 195 private boolean verbose;
duke@1 196
duke@1 197 /**
duke@1 198 * The common prefix for all I18N message keys generated by this handler.
duke@1 199 */
duke@1 200 private String prefix;
duke@1 201
duke@1 202 /**
duke@1 203 * A set containing the names of the source files for which specific
duke@1 204 * warnings have been generated -- i.e. in verbose mode. If a source name
duke@1 205 * appears in this list, then deferred diagnostics will be phrased to
duke@1 206 * include "additionally"...
duke@1 207 */
duke@1 208 private Set<JavaFileObject> sourcesWithReportedWarnings;
duke@1 209
duke@1 210 /**
duke@1 211 * A variable indicating the latest best guess at what the final
duke@1 212 * deferred diagnostic will be. Initially as specific and helpful
duke@1 213 * as possible, as more warnings are reported, the scope of the
duke@1 214 * diagnostic will be broadened.
duke@1 215 */
duke@1 216 private DeferredDiagnosticKind deferredDiagnosticKind;
duke@1 217
duke@1 218 /**
duke@1 219 * If deferredDiagnosticKind is IN_FILE or ADDITIONAL_IN_FILE, this variable
duke@1 220 * gives the value of log.currentSource() for the file in question.
duke@1 221 */
duke@1 222 private JavaFileObject deferredDiagnosticSource;
duke@1 223
duke@1 224 /**
duke@1 225 * An optional argument to be used when constructing the
duke@1 226 * deferred diagnostic message, based on deferredDiagnosticKind.
duke@1 227 * This variable should normally be set/updated whenever
duke@1 228 * deferredDiagnosticKind is updated.
duke@1 229 */
duke@1 230 private Object deferredDiagnosticArg;
jjg@60 231
jjg@60 232 /**
jjg@60 233 * True if mandatory warnings and notes are being enforced.
jjg@60 234 */
jjg@60 235 private final boolean enforceMandatory;
jjg@60 236
jjg@60 237 /**
jjg@60 238 * Reports a mandatory warning to the log. If mandatory warnings
jjg@60 239 * are not being enforced, treat this as an ordinary warning.
jjg@60 240 */
jjg@60 241 private void logMandatoryWarning(DiagnosticPosition pos, String msg,
jjg@60 242 Object... args) {
jjg@60 243 if (enforceMandatory)
jjg@60 244 log.mandatoryWarning(pos, msg, args);
jjg@60 245 else
jjg@60 246 log.warning(pos, msg, args);
jjg@60 247 }
jjg@60 248
jjg@60 249 /**
jjg@60 250 * Reports a mandatory note to the log. If mandatory notes are
jjg@60 251 * not being enforced, treat this as an ordinary note.
jjg@60 252 */
jjg@60 253 private void logMandatoryNote(JavaFileObject file, String msg, Object... args) {
jjg@60 254 if (enforceMandatory)
jjg@60 255 log.mandatoryNote(file, msg, args);
jjg@60 256 else
jjg@60 257 log.note(file, msg, args);
jjg@60 258 }
duke@1 259 }

mercurial