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