src/share/classes/com/sun/tools/doclint/Messages.java

Tue, 17 Sep 2013 14:17:13 -0700

author
jjg
date
Tue, 17 Sep 2013 14:17:13 -0700
changeset 2033
fdfbc5f0c4ed
parent 1455
75ab654b5cd5
child 2413
fe033d997ddf
permissions
-rw-r--r--

8024538: -Xdoclint + -Xprefer:source + incremental compilation == FAIL
Reviewed-by: darcy

jjg@1455 1 /*
jjg@1455 2 * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
jjg@1455 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
jjg@1455 4 *
jjg@1455 5 * This code is free software; you can redistribute it and/or modify it
jjg@1455 6 * under the terms of the GNU General Public License version 2 only, as
jjg@1455 7 * published by the Free Software Foundation. Oracle designates this
jjg@1455 8 * particular file as subject to the "Classpath" exception as provided
jjg@1455 9 * by Oracle in the LICENSE file that accompanied this code.
jjg@1455 10 *
jjg@1455 11 * This code is distributed in the hope that it will be useful, but WITHOUT
jjg@1455 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
jjg@1455 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
jjg@1455 14 * version 2 for more details (a copy is included in the LICENSE file that
jjg@1455 15 * accompanied this code).
jjg@1455 16 *
jjg@1455 17 * You should have received a copy of the GNU General Public License version
jjg@1455 18 * 2 along with this work; if not, write to the Free Software Foundation,
jjg@1455 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
jjg@1455 20 *
jjg@1455 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
jjg@1455 22 * or visit www.oracle.com if you need additional information or have any
jjg@1455 23 * questions.
jjg@1455 24 */
jjg@1455 25
jjg@1455 26 package com.sun.tools.doclint;
jjg@1455 27
jjg@1455 28 import java.io.PrintWriter;
jjg@1455 29 import java.text.MessageFormat;
jjg@1455 30 import java.util.Comparator;
jjg@1455 31 import java.util.HashMap;
jjg@1455 32 import java.util.Locale;
jjg@1455 33 import java.util.Map;
jjg@1455 34 import java.util.MissingResourceException;
jjg@1455 35 import java.util.ResourceBundle;
jjg@1455 36 import java.util.Set;
jjg@1455 37 import java.util.TreeMap;
jjg@1455 38 import java.util.TreeSet;
jjg@1455 39
jjg@1455 40 import javax.tools.Diagnostic;
jjg@1455 41
jjg@1455 42 import com.sun.source.doctree.DocTree;
jjg@1455 43 import com.sun.source.tree.Tree;
jjg@1455 44 import com.sun.tools.doclint.Env.AccessKind;
jjg@1455 45
jjg@1455 46 /**
jjg@1455 47 * Message reporting for DocLint.
jjg@1455 48 *
jjg@1455 49 * Options are used to filter out messages based on group and access level.
jjg@1455 50 * Support can be enabled for accumulating statistics of different kinds of
jjg@1455 51 * messages.
jjg@1455 52 *
jjg@1455 53 * <p><b>This is NOT part of any supported API.
jjg@1455 54 * If you write code that depends on this, you do so at your own
jjg@1455 55 * risk. This code and its internal interfaces are subject to change
jjg@1455 56 * or deletion without notice.</b></p>
jjg@1455 57 */
jjg@1455 58 public class Messages {
jjg@1455 59 /**
jjg@1455 60 * Groups used to categorize messages, so that messages in each group
jjg@1455 61 * can be enabled or disabled via options.
jjg@1455 62 */
jjg@1455 63 public enum Group {
jjg@1455 64 ACCESSIBILITY,
jjg@1455 65 HTML,
jjg@1455 66 MISSING,
jjg@1455 67 SYNTAX,
jjg@1455 68 REFERENCE;
jjg@1455 69
jjg@1455 70 String optName() { return name().toLowerCase(); }
jjg@1455 71 String notOptName() { return "-" + optName(); }
jjg@1455 72
jjg@1455 73 static boolean accepts(String opt) {
jjg@1455 74 for (Group g: values())
jjg@1455 75 if (opt.equals(g.optName())) return true;
jjg@1455 76 return false;
jjg@1455 77 }
jjg@1455 78 };
jjg@1455 79
jjg@1455 80 private final Options options;
jjg@1455 81 private final Stats stats;
jjg@1455 82
jjg@1455 83 ResourceBundle bundle;
jjg@1455 84 Env env;
jjg@1455 85
jjg@1455 86 Messages(Env env) {
jjg@1455 87 this.env = env;
jjg@1455 88 String name = getClass().getPackage().getName() + ".resources.doclint";
jjg@1455 89 bundle = ResourceBundle.getBundle(name, Locale.ENGLISH);
jjg@1455 90
jjg@1455 91 stats = new Stats(bundle);
jjg@1455 92 options = new Options(stats);
jjg@1455 93 }
jjg@1455 94
jjg@1455 95 void error(Group group, DocTree tree, String code, Object... args) {
jjg@1455 96 report(group, Diagnostic.Kind.ERROR, tree, code, args);
jjg@1455 97 }
jjg@1455 98
jjg@1455 99 void warning(Group group, DocTree tree, String code, Object... args) {
jjg@1455 100 report(group, Diagnostic.Kind.WARNING, tree, code, args);
jjg@1455 101 }
jjg@1455 102
jjg@1455 103 void setOptions(String opts) {
jjg@1455 104 options.setOptions(opts);
jjg@1455 105 }
jjg@1455 106
jjg@1455 107 void setStatsEnabled(boolean b) {
jjg@1455 108 stats.setEnabled(b);
jjg@1455 109 }
jjg@1455 110
jjg@1455 111 void reportStats(PrintWriter out) {
jjg@1455 112 stats.report(out);
jjg@1455 113 }
jjg@1455 114
jjg@1455 115 protected void report(Group group, Diagnostic.Kind dkind, DocTree tree, String code, Object... args) {
jjg@1455 116 if (options.isEnabled(group, env.currAccess)) {
jjg@1455 117 String msg = (code == null) ? (String) args[0] : localize(code, args);
jjg@1455 118 env.trees.printMessage(dkind, msg, tree,
jjg@1455 119 env.currDocComment, env.currPath.getCompilationUnit());
jjg@1455 120
jjg@1455 121 stats.record(group, dkind, code);
jjg@1455 122 }
jjg@1455 123 }
jjg@1455 124
jjg@1455 125 protected void report(Group group, Diagnostic.Kind dkind, Tree tree, String code, Object... args) {
jjg@1455 126 if (options.isEnabled(group, env.currAccess)) {
jjg@1455 127 String msg = localize(code, args);
jjg@1455 128 env.trees.printMessage(dkind, msg, tree, env.currPath.getCompilationUnit());
jjg@1455 129
jjg@1455 130 stats.record(group, dkind, code);
jjg@1455 131 }
jjg@1455 132 }
jjg@1455 133
jjg@1455 134 String localize(String code, Object... args) {
jjg@1455 135 String msg = bundle.getString(code);
jjg@1455 136 if (msg == null) {
jjg@1455 137 StringBuilder sb = new StringBuilder();
jjg@1455 138 sb.append("message file broken: code=").append(code);
jjg@1455 139 if (args.length > 0) {
jjg@1455 140 sb.append(" arguments={0}");
jjg@1455 141 for (int i = 1; i < args.length; i++) {
jjg@1455 142 sb.append(", {").append(i).append("}");
jjg@1455 143 }
jjg@1455 144 }
jjg@1455 145 msg = sb.toString();
jjg@1455 146 }
jjg@1455 147 return MessageFormat.format(msg, args);
jjg@1455 148 }
jjg@1455 149
jjg@1455 150 // <editor-fold defaultstate="collapsed" desc="Options">
jjg@1455 151
jjg@1455 152 /**
jjg@1455 153 * Handler for (sub)options specific to message handling.
jjg@1455 154 */
jjg@1455 155 static class Options {
jjg@1455 156 Map<String, Env.AccessKind> map = new HashMap<String, Env.AccessKind>();
jjg@1455 157 private final Stats stats;
jjg@1455 158
jjg@1455 159 static boolean isValidOptions(String opts) {
jjg@1455 160 for (String opt: opts.split(",")) {
jjg@1455 161 if (!isValidOption(opt.trim().toLowerCase()))
jjg@1455 162 return false;
jjg@1455 163 }
jjg@1455 164 return true;
jjg@1455 165 }
jjg@1455 166
jjg@1455 167 private static boolean isValidOption(String opt) {
jjg@1455 168 if (opt.equals("none") || opt.equals(Stats.OPT))
jjg@1455 169 return true;
jjg@1455 170
jjg@1455 171 int begin = opt.startsWith("-") ? 1 : 0;
jjg@1455 172 int sep = opt.indexOf("/");
jjg@1455 173 String grp = opt.substring(begin, (sep != -1) ? sep : opt.length());
jjg@1455 174 return ((begin == 0 && grp.equals("all")) || Group.accepts(grp))
jjg@1455 175 && ((sep == -1) || AccessKind.accepts(opt.substring(sep + 1)));
jjg@1455 176 }
jjg@1455 177
jjg@1455 178 Options(Stats stats) {
jjg@1455 179 this.stats = stats;
jjg@1455 180 }
jjg@1455 181
jjg@1455 182 /** Determine if a message group is enabled for a particular access level. */
jjg@1455 183 boolean isEnabled(Group g, Env.AccessKind access) {
jjg@1455 184 if (map.isEmpty())
jjg@1455 185 map.put("all", Env.AccessKind.PROTECTED);
jjg@1455 186
jjg@1455 187 Env.AccessKind ak = map.get(g.optName());
jjg@1455 188 if (ak != null && access.compareTo(ak) >= 0)
jjg@1455 189 return true;
jjg@1455 190
jjg@1455 191 ak = map.get(ALL);
jjg@1455 192 if (ak != null && access.compareTo(ak) >= 0) {
jjg@1455 193 ak = map.get(g.notOptName());
jjg@1455 194 if (ak == null || access.compareTo(ak) > 0) // note >, not >=
jjg@1455 195 return true;
jjg@1455 196 }
jjg@1455 197
jjg@1455 198 return false;
jjg@1455 199 }
jjg@1455 200
jjg@1455 201 void setOptions(String opts) {
jjg@1455 202 if (opts == null)
jjg@1455 203 setOption(ALL, Env.AccessKind.PRIVATE);
jjg@1455 204 else {
jjg@1455 205 for (String opt: opts.split(","))
jjg@1455 206 setOption(opt.trim().toLowerCase());
jjg@1455 207 }
jjg@1455 208 }
jjg@1455 209
jjg@1455 210 private void setOption(String arg) throws IllegalArgumentException {
jjg@1455 211 if (arg.equals(Stats.OPT)) {
jjg@1455 212 stats.setEnabled(true);
jjg@1455 213 return;
jjg@1455 214 }
jjg@1455 215
jjg@1455 216 int sep = arg.indexOf("/");
jjg@1455 217 if (sep > 0) {
jjg@1455 218 Env.AccessKind ak = Env.AccessKind.valueOf(arg.substring(sep + 1).toUpperCase());
jjg@1455 219 setOption(arg.substring(0, sep), ak);
jjg@1455 220 } else {
jjg@1455 221 setOption(arg, null);
jjg@1455 222 }
jjg@1455 223 }
jjg@1455 224
jjg@1455 225 private void setOption(String opt, Env.AccessKind ak) {
jjg@1455 226 map.put(opt, (ak != null) ? ak
jjg@1455 227 : opt.startsWith("-") ? Env.AccessKind.PUBLIC : Env.AccessKind.PRIVATE);
jjg@1455 228 }
jjg@1455 229
jjg@1455 230 private static final String ALL = "all";
jjg@1455 231 }
jjg@1455 232
jjg@1455 233 // </editor-fold>
jjg@1455 234
jjg@1455 235 // <editor-fold defaultstate="collapsed" desc="Statistics">
jjg@1455 236
jjg@1455 237 /**
jjg@1455 238 * Optionally record statistics of different kinds of message.
jjg@1455 239 */
jjg@1455 240 static class Stats {
jjg@1455 241 public static final String OPT = "stats";
jjg@1455 242 public static final String NO_CODE = "";
jjg@1455 243 final ResourceBundle bundle;
jjg@1455 244
jjg@1455 245 // tables only initialized if enabled
jjg@1455 246 int[] groupCounts;
jjg@1455 247 int[] dkindCounts;
jjg@1455 248 Map<String, Integer> codeCounts;
jjg@1455 249
jjg@1455 250 Stats(ResourceBundle bundle) {
jjg@1455 251 this.bundle = bundle;
jjg@1455 252 }
jjg@1455 253
jjg@1455 254 void setEnabled(boolean b) {
jjg@1455 255 if (b) {
jjg@1455 256 groupCounts = new int[Messages.Group.values().length];
jjg@1455 257 dkindCounts = new int[Diagnostic.Kind.values().length];
jjg@1455 258 codeCounts = new HashMap<String, Integer>();
jjg@1455 259 } else {
jjg@1455 260 groupCounts = null;
jjg@1455 261 dkindCounts = null;
jjg@1455 262 codeCounts = null;
jjg@1455 263 }
jjg@1455 264 }
jjg@1455 265
jjg@1455 266 void record(Messages.Group g, Diagnostic.Kind dkind, String code) {
jjg@1455 267 if (codeCounts == null) {
jjg@1455 268 return;
jjg@1455 269 }
jjg@1455 270 groupCounts[g.ordinal()]++;
jjg@1455 271 dkindCounts[dkind.ordinal()]++;
jjg@1455 272 if (code == null) {
jjg@1455 273 code = NO_CODE;
jjg@1455 274 }
jjg@1455 275 Integer i = codeCounts.get(code);
jjg@1455 276 codeCounts.put(code, (i == null) ? 1 : i + 1);
jjg@1455 277 }
jjg@1455 278
jjg@1455 279 void report(PrintWriter out) {
jjg@1455 280 if (codeCounts == null) {
jjg@1455 281 return;
jjg@1455 282 }
jjg@1455 283 out.println("By group...");
jjg@1455 284 Table groupTable = new Table();
jjg@1455 285 for (Messages.Group g : Messages.Group.values()) {
jjg@1455 286 groupTable.put(g.optName(), groupCounts[g.ordinal()]);
jjg@1455 287 }
jjg@1455 288 groupTable.print(out);
jjg@1455 289 out.println();
jjg@1455 290 out.println("By diagnostic kind...");
jjg@1455 291 Table dkindTable = new Table();
jjg@1455 292 for (Diagnostic.Kind k : Diagnostic.Kind.values()) {
jjg@1455 293 dkindTable.put(k.toString().toLowerCase(), dkindCounts[k.ordinal()]);
jjg@1455 294 }
jjg@1455 295 dkindTable.print(out);
jjg@1455 296 out.println();
jjg@1455 297 out.println("By message kind...");
jjg@1455 298 Table codeTable = new Table();
jjg@1455 299 for (Map.Entry<String, Integer> e : codeCounts.entrySet()) {
jjg@1455 300 String code = e.getKey();
jjg@1455 301 String msg;
jjg@1455 302 try {
jjg@1455 303 msg = code.equals(NO_CODE) ? "OTHER" : bundle.getString(code);
jjg@1455 304 } catch (MissingResourceException ex) {
jjg@1455 305 msg = code;
jjg@1455 306 }
jjg@1455 307 codeTable.put(msg, e.getValue());
jjg@1455 308 }
jjg@1455 309 codeTable.print(out);
jjg@1455 310 }
jjg@1455 311
jjg@1455 312 /**
jjg@1455 313 * A table of (int, String) sorted by decreasing int.
jjg@1455 314 */
jjg@1455 315 private static class Table {
jjg@1455 316
jjg@1455 317 private static final Comparator<Integer> DECREASING = new Comparator<Integer>() {
jjg@1455 318
jjg@1455 319 public int compare(Integer o1, Integer o2) {
jjg@1455 320 return o2.compareTo(o1);
jjg@1455 321 }
jjg@1455 322 };
jjg@1455 323 private final TreeMap<Integer, Set<String>> map = new TreeMap<Integer, Set<String>>(DECREASING);
jjg@1455 324
jjg@1455 325 void put(String label, int n) {
jjg@1455 326 if (n == 0) {
jjg@1455 327 return;
jjg@1455 328 }
jjg@1455 329 Set<String> labels = map.get(n);
jjg@1455 330 if (labels == null) {
jjg@1455 331 map.put(n, labels = new TreeSet<String>());
jjg@1455 332 }
jjg@1455 333 labels.add(label);
jjg@1455 334 }
jjg@1455 335
jjg@1455 336 void print(PrintWriter out) {
jjg@1455 337 for (Map.Entry<Integer, Set<String>> e : map.entrySet()) {
jjg@1455 338 int count = e.getKey();
jjg@1455 339 Set<String> labels = e.getValue();
jjg@1455 340 for (String label : labels) {
jjg@1455 341 out.println(String.format("%6d: %s", count, label));
jjg@1455 342 }
jjg@1455 343 }
jjg@1455 344 }
jjg@1455 345 }
jjg@1455 346 }
jjg@1455 347 // </editor-fold>
jjg@1455 348 }

mercurial