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

Wed, 10 Jun 2015 14:22:04 -0700

author
mfang
date
Wed, 10 Jun 2015 14:22:04 -0700
changeset 2815
d4051d4f5daf
parent 2413
fe033d997ddf
child 2525
2eb010b6cb22
permissions
-rw-r--r--

8083601: jdk8u60 l10n resource file translation update 2
Reviewed-by: ksrini, yhuang

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

mercurial