Path: blob/master/src/jdk.javadoc/share/classes/jdk/javadoc/internal/doclint/Messages.java
41161 views
/*1* Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package jdk.javadoc.internal.doclint;2627import java.io.PrintWriter;28import java.text.MessageFormat;29import java.util.Comparator;30import java.util.HashMap;31import java.util.Locale;32import java.util.Map;33import java.util.MissingResourceException;34import java.util.ResourceBundle;35import java.util.Set;36import java.util.TreeMap;37import java.util.TreeSet;3839import javax.tools.Diagnostic;4041import com.sun.source.doctree.DocTree;42import com.sun.source.tree.Tree;43import com.sun.tools.javac.util.StringUtils;44import jdk.javadoc.internal.doclint.Env.AccessKind;4546/**47* Message reporting for DocLint.48*49* Options are used to filter out messages based on group and access level.50* Support can be enabled for accumulating statistics of different kinds of51* messages.52*53* <p><b>This is NOT part of any supported API.54* If you write code that depends on this, you do so at your own55* risk. This code and its internal interfaces are subject to change56* or deletion without notice.</b></p>57*/58public class Messages {59/**60* Groups used to categorize messages, so that messages in each group61* can be enabled or disabled via options.62*/63public enum Group {64ACCESSIBILITY,65HTML,66MISSING,67SYNTAX,68REFERENCE;6970String optName() { return StringUtils.toLowerCase(name()); }71String notOptName() { return "-" + optName(); }7273static boolean accepts(String opt) {74for (Group g: values())75if (opt.equals(g.optName())) return true;76return false;77}78}7980private final Options options;81private final Stats stats;8283ResourceBundle bundle;84Env env;8586Messages(Env env) {87this.env = env;88String name = getClass().getPackage().getName() + ".resources.doclint";89bundle = ResourceBundle.getBundle(name, Locale.ENGLISH);9091stats = new Stats(bundle);92options = new Options(stats);93}9495void error(Group group, DocTree tree, String code, Object... args) {96report(group, Diagnostic.Kind.ERROR, tree, code, args);97}9899void warning(Group group, DocTree tree, String code, Object... args) {100report(group, Diagnostic.Kind.WARNING, tree, code, args);101}102103void setOptions(String opts) {104options.setOptions(opts);105}106107void setStatsEnabled(boolean b) {108stats.setEnabled(b);109}110111void reportStats(PrintWriter out) {112stats.report(out);113}114115protected void report(Group group, Diagnostic.Kind dkind, DocTree tree, String code, Object... args) {116if (options.isEnabled(group, env.currAccess)) {117String msg = (code == null) ? (String) args[0] : localize(code, args);118env.trees.printMessage(dkind, msg, tree,119env.currDocComment, env.currPath.getCompilationUnit());120121stats.record(group, dkind, code);122}123}124125protected void report(Group group, Diagnostic.Kind dkind, Tree tree, String code, Object... args) {126if (options.isEnabled(group, env.currAccess)) {127String msg = localize(code, args);128env.trees.printMessage(dkind, msg, tree, env.currPath.getCompilationUnit());129130stats.record(group, dkind, code);131}132}133134String localize(String code, Object... args) {135String msg = bundle.getString(code);136if (msg == null) {137StringBuilder sb = new StringBuilder();138sb.append("message file broken: code=").append(code);139if (args.length > 0) {140sb.append(" arguments={0}");141for (int i = 1; i < args.length; i++) {142sb.append(", {").append(i).append("}");143}144}145msg = sb.toString();146}147return MessageFormat.format(msg, args);148}149150// <editor-fold defaultstate="collapsed" desc="Options">151152/**153* Handler for (sub)options specific to message handling.154*/155static class Options {156Map<String, Env.AccessKind> map = new HashMap<>();157private final Stats stats;158159static boolean isValidOptions(String opts) {160for (String opt: opts.split(",")) {161if (!isValidOption(StringUtils.toLowerCase(opt.trim())))162return false;163}164return true;165}166167private static boolean isValidOption(String opt) {168if (opt.equals("none") || opt.equals(Stats.OPT))169return true;170171int begin = opt.startsWith("-") ? 1 : 0;172int sep = opt.indexOf("/");173String grp = opt.substring(begin, (sep != -1) ? sep : opt.length());174return ((begin == 0 && grp.equals("all")) || Group.accepts(grp))175&& ((sep == -1) || AccessKind.accepts(opt.substring(sep + 1)));176}177178Options(Stats stats) {179this.stats = stats;180}181182/** Determine if a message group is enabled for a particular access level. */183boolean isEnabled(Group g, Env.AccessKind access) {184if (map.isEmpty())185map.put("all", Env.AccessKind.PROTECTED);186187Env.AccessKind ak = map.get(g.optName());188if (ak != null && access.compareTo(ak) >= 0)189return true;190191ak = map.get(ALL);192if (ak != null && access.compareTo(ak) >= 0) {193ak = map.get(g.notOptName());194if (ak == null || access.compareTo(ak) > 0) // note >, not >=195return true;196}197198return false;199}200201void setOptions(String opts) {202if (opts == null)203setOption(ALL, Env.AccessKind.PRIVATE);204else {205for (String opt: opts.split(","))206setOption(StringUtils.toLowerCase(opt.trim()));207}208}209210private void setOption(String arg) throws IllegalArgumentException {211if (arg.equals(Stats.OPT)) {212stats.setEnabled(true);213return;214}215216int sep = arg.indexOf("/");217if (sep > 0) {218Env.AccessKind ak = Env.AccessKind.valueOf(StringUtils.toUpperCase(arg.substring(sep + 1)));219setOption(arg.substring(0, sep), ak);220} else {221setOption(arg, null);222}223}224225private void setOption(String opt, Env.AccessKind ak) {226map.put(opt, (ak != null) ? ak227: opt.startsWith("-") ? Env.AccessKind.PUBLIC : Env.AccessKind.PRIVATE);228}229230private static final String ALL = "all";231}232233// </editor-fold>234235// <editor-fold defaultstate="collapsed" desc="Statistics">236237/**238* Optionally record statistics of different kinds of message.239*/240static class Stats {241public static final String OPT = "stats";242public static final String NO_CODE = "";243final ResourceBundle bundle;244245// tables only initialized if enabled246int[] groupCounts;247int[] dkindCounts;248Map<String, Integer> codeCounts;249250Stats(ResourceBundle bundle) {251this.bundle = bundle;252}253254void setEnabled(boolean b) {255if (b) {256groupCounts = new int[Messages.Group.values().length];257dkindCounts = new int[Diagnostic.Kind.values().length];258codeCounts = new HashMap<>();259} else {260groupCounts = null;261dkindCounts = null;262codeCounts = null;263}264}265266void record(Messages.Group g, Diagnostic.Kind dkind, String code) {267if (codeCounts == null) {268return;269}270groupCounts[g.ordinal()]++;271dkindCounts[dkind.ordinal()]++;272if (code == null) {273code = NO_CODE;274}275Integer i = codeCounts.get(code);276codeCounts.put(code, (i == null) ? 1 : i + 1);277}278279void report(PrintWriter out) {280if (codeCounts == null) {281return;282}283out.println("By group...");284Table groupTable = new Table();285for (Messages.Group g : Messages.Group.values()) {286groupTable.put(g.optName(), groupCounts[g.ordinal()]);287}288groupTable.print(out);289out.println();290out.println("By diagnostic kind...");291Table dkindTable = new Table();292for (Diagnostic.Kind k : Diagnostic.Kind.values()) {293dkindTable.put(StringUtils.toLowerCase(k.toString()), dkindCounts[k.ordinal()]);294}295dkindTable.print(out);296out.println();297out.println("By message kind...");298Table codeTable = new Table();299for (Map.Entry<String, Integer> e : codeCounts.entrySet()) {300String code = e.getKey();301String msg;302try {303msg = code.equals(NO_CODE) ? "OTHER" : bundle.getString(code);304} catch (MissingResourceException ex) {305msg = code;306}307codeTable.put(msg, e.getValue());308}309codeTable.print(out);310}311312/**313* A table of (int, String) sorted by decreasing int.314*/315private static class Table {316317private static final Comparator<Integer> DECREASING = (o1, o2) -> o2.compareTo(o1);318private final TreeMap<Integer, Set<String>> map = new TreeMap<>(DECREASING);319320void put(String label, int n) {321if (n == 0) {322return;323}324Set<String> labels = map.get(n);325if (labels == null) {326map.put(n, labels = new TreeSet<>());327}328labels.add(label);329}330331void print(PrintWriter out) {332for (Map.Entry<Integer, Set<String>> e : map.entrySet()) {333int count = e.getKey();334Set<String> labels = e.getValue();335for (String label : labels) {336out.println(String.format("%6d: %s", count, label));337}338}339}340}341}342// </editor-fold>343}344345346