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

Wed, 27 Apr 2016 01:34:52 +0800

author
aoqi
date
Wed, 27 Apr 2016 01:34:52 +0800
changeset 0
959103a6100f
child 2525
2eb010b6cb22
permissions
-rw-r--r--

Initial load
http://hg.openjdk.java.net/jdk8u/jdk8u/langtools/
changeset: 2573:53ca196be1ae
tag: jdk8u25-b17

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

mercurial