duke@1: /* ohair@798: * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved. duke@1: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. duke@1: * duke@1: * This code is free software; you can redistribute it and/or modify it duke@1: * under the terms of the GNU General Public License version 2 only, as ohair@554: * published by the Free Software Foundation. Oracle designates this duke@1: * particular file as subject to the "Classpath" exception as provided ohair@554: * by Oracle in the LICENSE file that accompanied this code. duke@1: * duke@1: * This code is distributed in the hope that it will be useful, but WITHOUT duke@1: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or duke@1: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License duke@1: * version 2 for more details (a copy is included in the LICENSE file that duke@1: * accompanied this code). duke@1: * duke@1: * You should have received a copy of the GNU General Public License version duke@1: * 2 along with this work; if not, write to the Free Software Foundation, duke@1: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. duke@1: * ohair@554: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA ohair@554: * or visit www.oracle.com if you need additional information or have any ohair@554: * questions. duke@1: */ duke@1: duke@1: package com.sun.tools.javac.util; duke@1: duke@1: import java.util.HashSet; duke@1: import java.util.Set; duke@1: import javax.tools.JavaFileObject; duke@1: jjg@612: import com.sun.tools.javac.code.Lint.LintCategory; duke@1: import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition; duke@1: duke@1: duke@1: /** duke@1: * A handler to process mandatory warnings, setting up a deferred diagnostic duke@1: * to be printed at the end of the compilation if some warnings get suppressed duke@1: * because too many warnings have already been generated. duke@1: * duke@1: * Note that the SuppressWarnings annotation can be used to suppress warnings duke@1: * about conditions that would otherwise merit a warning. Such processing duke@1: * is done when the condition is detected, and in those cases, no call is duke@1: * made on any API to generate a warning at all. In consequence, this handler only duke@1: * gets to handle those warnings that JLS says must be generated. duke@1: * jjg@581: *
This is NOT part of any supported API.
jjg@581: * If you write code that depends on this, you do so at your own risk.
duke@1: * This code and its internal interfaces are subject to change or
duke@1: * deletion without notice.
duke@1: */
duke@1: public class MandatoryWarningHandler {
duke@1:
duke@1: /**
duke@1: * The kinds of different deferred diagnostics that might be generated
duke@1: * if a mandatory warning is suppressed because too many warnings have
duke@1: * already been output.
duke@1: *
duke@1: * The parameter is a fragment used to build an I18N message key for Log.
duke@1: */
duke@1: private enum DeferredDiagnosticKind {
duke@1: /**
duke@1: * This kind is used when a single specific file is found to have warnings
duke@1: * and no similar warnings have already been given.
duke@1: * It generates a message like:
duke@1: * FILE has ISSUES
duke@1: */
duke@1: IN_FILE(".filename"),
duke@1: /**
duke@1: * This kind is used when a single specific file is found to have warnings
duke@1: * and when similar warnings have already been reported for the file.
duke@1: * It generates a message like:
duke@1: * FILE has additional ISSUES
duke@1: */
duke@1: ADDITIONAL_IN_FILE(".filename.additional"),
duke@1: /**
duke@1: * This kind is used when multiple files have been found to have warnings,
duke@1: * and none of them have had any similar warnings.
duke@1: * It generates a message like:
duke@1: * Some files have ISSUES
duke@1: */
duke@1: IN_FILES(".plural"),
duke@1: /**
duke@1: * This kind is used when multiple files have been found to have warnings,
duke@1: * and some of them have had already had specific similar warnings.
duke@1: * It generates a message like:
duke@1: * Some files have additional ISSUES
duke@1: */
duke@1: ADDITIONAL_IN_FILES(".plural.additional");
duke@1:
duke@1: DeferredDiagnosticKind(String v) { value = v; }
duke@1: String getKey(String prefix) { return prefix + value; }
duke@1:
duke@1: private String value;
duke@1: }
duke@1:
duke@1:
duke@1: /**
duke@1: * Create a handler for mandatory warnings.
duke@1: * @param log The log on which to generate any diagnostics
duke@1: * @param verbose Specify whether or not detailed messages about
duke@1: * individual instances should be given, or whether an aggregate
duke@1: * message should be generated at the end of the compilation.
duke@1: * Typically set via -Xlint:option.
jjg@60: * @param enforceMandatory
jjg@60: * True if mandatory warnings and notes are being enforced.
duke@1: * @param prefix A common prefix for the set of message keys for
duke@1: * the messages that may be generated.
jjg@612: * @param lc An associated lint category for the warnings, or null if none.
duke@1: */
jjg@60: public MandatoryWarningHandler(Log log, boolean verbose,
jjg@612: boolean enforceMandatory, String prefix,
jjg@612: LintCategory lc) {
duke@1: this.log = log;
duke@1: this.verbose = verbose;
duke@1: this.prefix = prefix;
jjg@60: this.enforceMandatory = enforceMandatory;
jjg@612: this.lintCategory = lc;
duke@1: }
duke@1:
duke@1: /**
duke@1: * Report a mandatory warning.
duke@1: */
duke@1: public void report(DiagnosticPosition pos, String msg, Object... args) {
mcimadamore@168: JavaFileObject currentSource = log.currentSourceFile();
duke@1:
duke@1: if (verbose) {
duke@1: if (sourcesWithReportedWarnings == null)
duke@1: sourcesWithReportedWarnings = new HashSet