jjg@1455: /* jjg@1455: * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved. jjg@1455: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. jjg@1455: * jjg@1455: * This code is free software; you can redistribute it and/or modify it jjg@1455: * under the terms of the GNU General Public License version 2 only, as jjg@1455: * published by the Free Software Foundation. Oracle designates this jjg@1455: * particular file as subject to the "Classpath" exception as provided jjg@1455: * by Oracle in the LICENSE file that accompanied this code. jjg@1455: * jjg@1455: * This code is distributed in the hope that it will be useful, but WITHOUT jjg@1455: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or jjg@1455: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License jjg@1455: * version 2 for more details (a copy is included in the LICENSE file that jjg@1455: * accompanied this code). jjg@1455: * jjg@1455: * You should have received a copy of the GNU General Public License version jjg@1455: * 2 along with this work; if not, write to the Free Software Foundation, jjg@1455: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. jjg@1455: * jjg@1455: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA jjg@1455: * or visit www.oracle.com if you need additional information or have any jjg@1455: * questions. jjg@1455: */ jjg@1455: jjg@1455: package com.sun.tools.doclint; jjg@1455: jjg@1455: import java.io.PrintWriter; jjg@1455: import java.text.MessageFormat; jjg@1455: import java.util.Comparator; jjg@1455: import java.util.HashMap; jjg@1455: import java.util.Locale; jjg@1455: import java.util.Map; jjg@1455: import java.util.MissingResourceException; jjg@1455: import java.util.ResourceBundle; jjg@1455: import java.util.Set; jjg@1455: import java.util.TreeMap; jjg@1455: import java.util.TreeSet; jjg@1455: jjg@1455: import javax.tools.Diagnostic; jjg@1455: jjg@1455: import com.sun.source.doctree.DocTree; jjg@1455: import com.sun.source.tree.Tree; jjg@1455: import com.sun.tools.doclint.Env.AccessKind; jjg@1455: jjg@1455: /** jjg@1455: * Message reporting for DocLint. jjg@1455: * jjg@1455: * Options are used to filter out messages based on group and access level. jjg@1455: * Support can be enabled for accumulating statistics of different kinds of jjg@1455: * messages. jjg@1455: * jjg@1455: *

This is NOT part of any supported API. jjg@1455: * If you write code that depends on this, you do so at your own jjg@1455: * risk. This code and its internal interfaces are subject to change jjg@1455: * or deletion without notice.

jjg@1455: */ jjg@1455: public class Messages { jjg@1455: /** jjg@1455: * Groups used to categorize messages, so that messages in each group jjg@1455: * can be enabled or disabled via options. jjg@1455: */ jjg@1455: public enum Group { jjg@1455: ACCESSIBILITY, jjg@1455: HTML, jjg@1455: MISSING, jjg@1455: SYNTAX, jjg@1455: REFERENCE; jjg@1455: jjg@1455: String optName() { return name().toLowerCase(); } jjg@1455: String notOptName() { return "-" + optName(); } jjg@1455: jjg@1455: static boolean accepts(String opt) { jjg@1455: for (Group g: values()) jjg@1455: if (opt.equals(g.optName())) return true; jjg@1455: return false; jjg@1455: } jjg@1455: }; jjg@1455: jjg@1455: private final Options options; jjg@1455: private final Stats stats; jjg@1455: jjg@1455: ResourceBundle bundle; jjg@1455: Env env; jjg@1455: jjg@1455: Messages(Env env) { jjg@1455: this.env = env; jjg@1455: String name = getClass().getPackage().getName() + ".resources.doclint"; jjg@1455: bundle = ResourceBundle.getBundle(name, Locale.ENGLISH); jjg@1455: jjg@1455: stats = new Stats(bundle); jjg@1455: options = new Options(stats); jjg@1455: } jjg@1455: jjg@1455: void error(Group group, DocTree tree, String code, Object... args) { jjg@1455: report(group, Diagnostic.Kind.ERROR, tree, code, args); jjg@1455: } jjg@1455: jjg@1455: void warning(Group group, DocTree tree, String code, Object... args) { jjg@1455: report(group, Diagnostic.Kind.WARNING, tree, code, args); jjg@1455: } jjg@1455: jjg@1455: void setOptions(String opts) { jjg@1455: options.setOptions(opts); jjg@1455: } jjg@1455: jjg@1455: void setStatsEnabled(boolean b) { jjg@1455: stats.setEnabled(b); jjg@1455: } jjg@1455: jjg@1455: void reportStats(PrintWriter out) { jjg@1455: stats.report(out); jjg@1455: } jjg@1455: jjg@1455: protected void report(Group group, Diagnostic.Kind dkind, DocTree tree, String code, Object... args) { jjg@1455: if (options.isEnabled(group, env.currAccess)) { jjg@1455: String msg = (code == null) ? (String) args[0] : localize(code, args); jjg@1455: env.trees.printMessage(dkind, msg, tree, jjg@1455: env.currDocComment, env.currPath.getCompilationUnit()); jjg@1455: jjg@1455: stats.record(group, dkind, code); jjg@1455: } jjg@1455: } jjg@1455: jjg@1455: protected void report(Group group, Diagnostic.Kind dkind, Tree tree, String code, Object... args) { jjg@1455: if (options.isEnabled(group, env.currAccess)) { jjg@1455: String msg = localize(code, args); jjg@1455: env.trees.printMessage(dkind, msg, tree, env.currPath.getCompilationUnit()); jjg@1455: jjg@1455: stats.record(group, dkind, code); jjg@1455: } jjg@1455: } jjg@1455: jjg@1455: String localize(String code, Object... args) { jjg@1455: String msg = bundle.getString(code); jjg@1455: if (msg == null) { jjg@1455: StringBuilder sb = new StringBuilder(); jjg@1455: sb.append("message file broken: code=").append(code); jjg@1455: if (args.length > 0) { jjg@1455: sb.append(" arguments={0}"); jjg@1455: for (int i = 1; i < args.length; i++) { jjg@1455: sb.append(", {").append(i).append("}"); jjg@1455: } jjg@1455: } jjg@1455: msg = sb.toString(); jjg@1455: } jjg@1455: return MessageFormat.format(msg, args); jjg@1455: } jjg@1455: jjg@1455: // jjg@1455: jjg@1455: /** jjg@1455: * Handler for (sub)options specific to message handling. jjg@1455: */ jjg@1455: static class Options { jjg@1455: Map map = new HashMap(); jjg@1455: private final Stats stats; jjg@1455: jjg@1455: static boolean isValidOptions(String opts) { jjg@1455: for (String opt: opts.split(",")) { jjg@1455: if (!isValidOption(opt.trim().toLowerCase())) jjg@1455: return false; jjg@1455: } jjg@1455: return true; jjg@1455: } jjg@1455: jjg@1455: private static boolean isValidOption(String opt) { jjg@1455: if (opt.equals("none") || opt.equals(Stats.OPT)) jjg@1455: return true; jjg@1455: jjg@1455: int begin = opt.startsWith("-") ? 1 : 0; jjg@1455: int sep = opt.indexOf("/"); jjg@1455: String grp = opt.substring(begin, (sep != -1) ? sep : opt.length()); jjg@1455: return ((begin == 0 && grp.equals("all")) || Group.accepts(grp)) jjg@1455: && ((sep == -1) || AccessKind.accepts(opt.substring(sep + 1))); jjg@1455: } jjg@1455: jjg@1455: Options(Stats stats) { jjg@1455: this.stats = stats; jjg@1455: } jjg@1455: jjg@1455: /** Determine if a message group is enabled for a particular access level. */ jjg@1455: boolean isEnabled(Group g, Env.AccessKind access) { jjg@1455: if (map.isEmpty()) jjg@1455: map.put("all", Env.AccessKind.PROTECTED); jjg@1455: jjg@1455: Env.AccessKind ak = map.get(g.optName()); jjg@1455: if (ak != null && access.compareTo(ak) >= 0) jjg@1455: return true; jjg@1455: jjg@1455: ak = map.get(ALL); jjg@1455: if (ak != null && access.compareTo(ak) >= 0) { jjg@1455: ak = map.get(g.notOptName()); jjg@1455: if (ak == null || access.compareTo(ak) > 0) // note >, not >= jjg@1455: return true; jjg@1455: } jjg@1455: jjg@1455: return false; jjg@1455: } jjg@1455: jjg@1455: void setOptions(String opts) { jjg@1455: if (opts == null) jjg@1455: setOption(ALL, Env.AccessKind.PRIVATE); jjg@1455: else { jjg@1455: for (String opt: opts.split(",")) jjg@1455: setOption(opt.trim().toLowerCase()); jjg@1455: } jjg@1455: } jjg@1455: jjg@1455: private void setOption(String arg) throws IllegalArgumentException { jjg@1455: if (arg.equals(Stats.OPT)) { jjg@1455: stats.setEnabled(true); jjg@1455: return; jjg@1455: } jjg@1455: jjg@1455: int sep = arg.indexOf("/"); jjg@1455: if (sep > 0) { jjg@1455: Env.AccessKind ak = Env.AccessKind.valueOf(arg.substring(sep + 1).toUpperCase()); jjg@1455: setOption(arg.substring(0, sep), ak); jjg@1455: } else { jjg@1455: setOption(arg, null); jjg@1455: } jjg@1455: } jjg@1455: jjg@1455: private void setOption(String opt, Env.AccessKind ak) { jjg@1455: map.put(opt, (ak != null) ? ak jjg@1455: : opt.startsWith("-") ? Env.AccessKind.PUBLIC : Env.AccessKind.PRIVATE); jjg@1455: } jjg@1455: jjg@1455: private static final String ALL = "all"; jjg@1455: } jjg@1455: jjg@1455: // jjg@1455: jjg@1455: // jjg@1455: jjg@1455: /** jjg@1455: * Optionally record statistics of different kinds of message. jjg@1455: */ jjg@1455: static class Stats { jjg@1455: public static final String OPT = "stats"; jjg@1455: public static final String NO_CODE = ""; jjg@1455: final ResourceBundle bundle; jjg@1455: jjg@1455: // tables only initialized if enabled jjg@1455: int[] groupCounts; jjg@1455: int[] dkindCounts; jjg@1455: Map codeCounts; jjg@1455: jjg@1455: Stats(ResourceBundle bundle) { jjg@1455: this.bundle = bundle; jjg@1455: } jjg@1455: jjg@1455: void setEnabled(boolean b) { jjg@1455: if (b) { jjg@1455: groupCounts = new int[Messages.Group.values().length]; jjg@1455: dkindCounts = new int[Diagnostic.Kind.values().length]; jjg@1455: codeCounts = new HashMap(); jjg@1455: } else { jjg@1455: groupCounts = null; jjg@1455: dkindCounts = null; jjg@1455: codeCounts = null; jjg@1455: } jjg@1455: } jjg@1455: jjg@1455: void record(Messages.Group g, Diagnostic.Kind dkind, String code) { jjg@1455: if (codeCounts == null) { jjg@1455: return; jjg@1455: } jjg@1455: groupCounts[g.ordinal()]++; jjg@1455: dkindCounts[dkind.ordinal()]++; jjg@1455: if (code == null) { jjg@1455: code = NO_CODE; jjg@1455: } jjg@1455: Integer i = codeCounts.get(code); jjg@1455: codeCounts.put(code, (i == null) ? 1 : i + 1); jjg@1455: } jjg@1455: jjg@1455: void report(PrintWriter out) { jjg@1455: if (codeCounts == null) { jjg@1455: return; jjg@1455: } jjg@1455: out.println("By group..."); jjg@1455: Table groupTable = new Table(); jjg@1455: for (Messages.Group g : Messages.Group.values()) { jjg@1455: groupTable.put(g.optName(), groupCounts[g.ordinal()]); jjg@1455: } jjg@1455: groupTable.print(out); jjg@1455: out.println(); jjg@1455: out.println("By diagnostic kind..."); jjg@1455: Table dkindTable = new Table(); jjg@1455: for (Diagnostic.Kind k : Diagnostic.Kind.values()) { jjg@1455: dkindTable.put(k.toString().toLowerCase(), dkindCounts[k.ordinal()]); jjg@1455: } jjg@1455: dkindTable.print(out); jjg@1455: out.println(); jjg@1455: out.println("By message kind..."); jjg@1455: Table codeTable = new Table(); jjg@1455: for (Map.Entry e : codeCounts.entrySet()) { jjg@1455: String code = e.getKey(); jjg@1455: String msg; jjg@1455: try { jjg@1455: msg = code.equals(NO_CODE) ? "OTHER" : bundle.getString(code); jjg@1455: } catch (MissingResourceException ex) { jjg@1455: msg = code; jjg@1455: } jjg@1455: codeTable.put(msg, e.getValue()); jjg@1455: } jjg@1455: codeTable.print(out); jjg@1455: } jjg@1455: jjg@1455: /** jjg@1455: * A table of (int, String) sorted by decreasing int. jjg@1455: */ jjg@1455: private static class Table { jjg@1455: jjg@1455: private static final Comparator DECREASING = new Comparator() { jjg@1455: jjg@1455: public int compare(Integer o1, Integer o2) { jjg@1455: return o2.compareTo(o1); jjg@1455: } jjg@1455: }; jjg@1455: private final TreeMap> map = new TreeMap>(DECREASING); jjg@1455: jjg@1455: void put(String label, int n) { jjg@1455: if (n == 0) { jjg@1455: return; jjg@1455: } jjg@1455: Set labels = map.get(n); jjg@1455: if (labels == null) { jjg@1455: map.put(n, labels = new TreeSet()); jjg@1455: } jjg@1455: labels.add(label); jjg@1455: } jjg@1455: jjg@1455: void print(PrintWriter out) { jjg@1455: for (Map.Entry> e : map.entrySet()) { jjg@1455: int count = e.getKey(); jjg@1455: Set labels = e.getValue(); jjg@1455: for (String label : labels) { jjg@1455: out.println(String.format("%6d: %s", count, label)); jjg@1455: } jjg@1455: } jjg@1455: } jjg@1455: } jjg@1455: } jjg@1455: // jjg@1455: }