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

changeset 0
959103a6100f
child 2525
2eb010b6cb22
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/share/classes/com/sun/tools/doclint/Messages.java	Wed Apr 27 01:34:52 2016 +0800
     1.3 @@ -0,0 +1,349 @@
     1.4 +/*
     1.5 + * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.  Oracle designates this
    1.11 + * particular file as subject to the "Classpath" exception as provided
    1.12 + * by Oracle in the LICENSE file that accompanied this code.
    1.13 + *
    1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.16 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.17 + * version 2 for more details (a copy is included in the LICENSE file that
    1.18 + * accompanied this code).
    1.19 + *
    1.20 + * You should have received a copy of the GNU General Public License version
    1.21 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.23 + *
    1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.25 + * or visit www.oracle.com if you need additional information or have any
    1.26 + * questions.
    1.27 + */
    1.28 +
    1.29 +package com.sun.tools.doclint;
    1.30 +
    1.31 +import java.io.PrintWriter;
    1.32 +import java.text.MessageFormat;
    1.33 +import java.util.Comparator;
    1.34 +import java.util.HashMap;
    1.35 +import java.util.Locale;
    1.36 +import java.util.Map;
    1.37 +import java.util.MissingResourceException;
    1.38 +import java.util.ResourceBundle;
    1.39 +import java.util.Set;
    1.40 +import java.util.TreeMap;
    1.41 +import java.util.TreeSet;
    1.42 +
    1.43 +import javax.tools.Diagnostic;
    1.44 +
    1.45 +import com.sun.source.doctree.DocTree;
    1.46 +import com.sun.source.tree.Tree;
    1.47 +import com.sun.tools.doclint.Env.AccessKind;
    1.48 +import com.sun.tools.javac.util.StringUtils;
    1.49 +
    1.50 +/**
    1.51 + * Message reporting for DocLint.
    1.52 + *
    1.53 + * Options are used to filter out messages based on group and access level.
    1.54 + * Support can be enabled for accumulating statistics of different kinds of
    1.55 + * messages.
    1.56 + *
    1.57 + * <p><b>This is NOT part of any supported API.
    1.58 + * If you write code that depends on this, you do so at your own
    1.59 + * risk.  This code and its internal interfaces are subject to change
    1.60 + * or deletion without notice.</b></p>
    1.61 + */
    1.62 +public class Messages {
    1.63 +    /**
    1.64 +     * Groups used to categorize messages, so that messages in each group
    1.65 +     * can be enabled or disabled via options.
    1.66 +     */
    1.67 +    public enum Group {
    1.68 +        ACCESSIBILITY,
    1.69 +        HTML,
    1.70 +        MISSING,
    1.71 +        SYNTAX,
    1.72 +        REFERENCE;
    1.73 +
    1.74 +        String optName() { return StringUtils.toLowerCase(name()); }
    1.75 +        String notOptName() { return "-" + optName(); }
    1.76 +
    1.77 +        static boolean accepts(String opt) {
    1.78 +            for (Group g: values())
    1.79 +                if (opt.equals(g.optName())) return true;
    1.80 +            return false;
    1.81 +        }
    1.82 +    };
    1.83 +
    1.84 +    private final Options options;
    1.85 +    private final Stats stats;
    1.86 +
    1.87 +    ResourceBundle bundle;
    1.88 +    Env env;
    1.89 +
    1.90 +    Messages(Env env) {
    1.91 +        this.env = env;
    1.92 +        String name = getClass().getPackage().getName() + ".resources.doclint";
    1.93 +        bundle = ResourceBundle.getBundle(name, Locale.ENGLISH);
    1.94 +
    1.95 +        stats = new Stats(bundle);
    1.96 +        options = new Options(stats);
    1.97 +    }
    1.98 +
    1.99 +    void error(Group group, DocTree tree, String code, Object... args) {
   1.100 +        report(group, Diagnostic.Kind.ERROR, tree, code, args);
   1.101 +    }
   1.102 +
   1.103 +    void warning(Group group, DocTree tree, String code, Object... args) {
   1.104 +        report(group, Diagnostic.Kind.WARNING, tree, code, args);
   1.105 +    }
   1.106 +
   1.107 +    void setOptions(String opts) {
   1.108 +        options.setOptions(opts);
   1.109 +    }
   1.110 +
   1.111 +    void setStatsEnabled(boolean b) {
   1.112 +        stats.setEnabled(b);
   1.113 +    }
   1.114 +
   1.115 +    void reportStats(PrintWriter out) {
   1.116 +        stats.report(out);
   1.117 +    }
   1.118 +
   1.119 +    protected void report(Group group, Diagnostic.Kind dkind, DocTree tree, String code, Object... args) {
   1.120 +        if (options.isEnabled(group, env.currAccess)) {
   1.121 +            String msg = (code == null) ? (String) args[0] : localize(code, args);
   1.122 +            env.trees.printMessage(dkind, msg, tree,
   1.123 +                    env.currDocComment, env.currPath.getCompilationUnit());
   1.124 +
   1.125 +            stats.record(group, dkind, code);
   1.126 +        }
   1.127 +    }
   1.128 +
   1.129 +    protected void report(Group group, Diagnostic.Kind dkind, Tree tree, String code, Object... args) {
   1.130 +        if (options.isEnabled(group, env.currAccess)) {
   1.131 +            String msg = localize(code, args);
   1.132 +            env.trees.printMessage(dkind, msg, tree, env.currPath.getCompilationUnit());
   1.133 +
   1.134 +            stats.record(group, dkind, code);
   1.135 +        }
   1.136 +    }
   1.137 +
   1.138 +    String localize(String code, Object... args) {
   1.139 +        String msg = bundle.getString(code);
   1.140 +        if (msg == null) {
   1.141 +            StringBuilder sb = new StringBuilder();
   1.142 +            sb.append("message file broken: code=").append(code);
   1.143 +            if (args.length > 0) {
   1.144 +                sb.append(" arguments={0}");
   1.145 +                for (int i = 1; i < args.length; i++) {
   1.146 +                    sb.append(", {").append(i).append("}");
   1.147 +                }
   1.148 +            }
   1.149 +            msg = sb.toString();
   1.150 +        }
   1.151 +        return MessageFormat.format(msg, args);
   1.152 +    }
   1.153 +
   1.154 +    // <editor-fold defaultstate="collapsed" desc="Options">
   1.155 +
   1.156 +    /**
   1.157 +     * Handler for (sub)options specific to message handling.
   1.158 +     */
   1.159 +    static class Options {
   1.160 +        Map<String, Env.AccessKind> map = new HashMap<String, Env.AccessKind>();
   1.161 +        private final Stats stats;
   1.162 +
   1.163 +        static boolean isValidOptions(String opts) {
   1.164 +            for (String opt: opts.split(",")) {
   1.165 +                if (!isValidOption(StringUtils.toLowerCase(opt.trim())))
   1.166 +                    return false;
   1.167 +            }
   1.168 +            return true;
   1.169 +        }
   1.170 +
   1.171 +        private static boolean isValidOption(String opt) {
   1.172 +            if (opt.equals("none") || opt.equals(Stats.OPT))
   1.173 +                return true;
   1.174 +
   1.175 +            int begin = opt.startsWith("-") ? 1 : 0;
   1.176 +            int sep = opt.indexOf("/");
   1.177 +            String grp = opt.substring(begin, (sep != -1) ? sep : opt.length());
   1.178 +            return ((begin == 0 && grp.equals("all")) || Group.accepts(grp))
   1.179 +                    && ((sep == -1) || AccessKind.accepts(opt.substring(sep + 1)));
   1.180 +        }
   1.181 +
   1.182 +        Options(Stats stats) {
   1.183 +            this.stats = stats;
   1.184 +        }
   1.185 +
   1.186 +        /** Determine if a message group is enabled for a particular access level. */
   1.187 +        boolean isEnabled(Group g, Env.AccessKind access) {
   1.188 +            if (map.isEmpty())
   1.189 +                map.put("all", Env.AccessKind.PROTECTED);
   1.190 +
   1.191 +            Env.AccessKind ak = map.get(g.optName());
   1.192 +            if (ak != null && access.compareTo(ak) >= 0)
   1.193 +                return true;
   1.194 +
   1.195 +            ak = map.get(ALL);
   1.196 +            if (ak != null && access.compareTo(ak) >= 0) {
   1.197 +                ak = map.get(g.notOptName());
   1.198 +                if (ak == null || access.compareTo(ak) > 0) // note >, not >=
   1.199 +                    return true;
   1.200 +            }
   1.201 +
   1.202 +            return false;
   1.203 +        }
   1.204 +
   1.205 +        void setOptions(String opts) {
   1.206 +            if (opts == null)
   1.207 +                setOption(ALL, Env.AccessKind.PRIVATE);
   1.208 +            else {
   1.209 +                for (String opt: opts.split(","))
   1.210 +                    setOption(StringUtils.toLowerCase(opt.trim()));
   1.211 +            }
   1.212 +        }
   1.213 +
   1.214 +        private void setOption(String arg) throws IllegalArgumentException {
   1.215 +            if (arg.equals(Stats.OPT)) {
   1.216 +                stats.setEnabled(true);
   1.217 +                return;
   1.218 +            }
   1.219 +
   1.220 +            int sep = arg.indexOf("/");
   1.221 +            if (sep > 0) {
   1.222 +                Env.AccessKind ak = Env.AccessKind.valueOf(StringUtils.toUpperCase(arg.substring(sep + 1)));
   1.223 +                setOption(arg.substring(0, sep), ak);
   1.224 +            } else {
   1.225 +                setOption(arg, null);
   1.226 +            }
   1.227 +        }
   1.228 +
   1.229 +        private void setOption(String opt, Env.AccessKind ak) {
   1.230 +            map.put(opt, (ak != null) ? ak
   1.231 +                    : opt.startsWith("-") ? Env.AccessKind.PUBLIC : Env.AccessKind.PRIVATE);
   1.232 +        }
   1.233 +
   1.234 +        private static final String ALL = "all";
   1.235 +    }
   1.236 +
   1.237 +    // </editor-fold>
   1.238 +
   1.239 +    // <editor-fold defaultstate="collapsed" desc="Statistics">
   1.240 +
   1.241 +    /**
   1.242 +     * Optionally record statistics of different kinds of message.
   1.243 +     */
   1.244 +    static class Stats {
   1.245 +        public static final String OPT = "stats";
   1.246 +        public static final String NO_CODE = "";
   1.247 +        final ResourceBundle bundle;
   1.248 +
   1.249 +        // tables only initialized if enabled
   1.250 +        int[] groupCounts;
   1.251 +        int[] dkindCounts;
   1.252 +        Map<String, Integer> codeCounts;
   1.253 +
   1.254 +        Stats(ResourceBundle bundle) {
   1.255 +            this.bundle = bundle;
   1.256 +        }
   1.257 +
   1.258 +        void setEnabled(boolean b) {
   1.259 +            if (b) {
   1.260 +                groupCounts = new int[Messages.Group.values().length];
   1.261 +                dkindCounts = new int[Diagnostic.Kind.values().length];
   1.262 +                codeCounts = new HashMap<String, Integer>();
   1.263 +            } else {
   1.264 +                groupCounts = null;
   1.265 +                dkindCounts = null;
   1.266 +                codeCounts = null;
   1.267 +            }
   1.268 +        }
   1.269 +
   1.270 +        void record(Messages.Group g, Diagnostic.Kind dkind, String code) {
   1.271 +            if (codeCounts == null) {
   1.272 +                return;
   1.273 +            }
   1.274 +            groupCounts[g.ordinal()]++;
   1.275 +            dkindCounts[dkind.ordinal()]++;
   1.276 +            if (code == null) {
   1.277 +                code = NO_CODE;
   1.278 +            }
   1.279 +            Integer i = codeCounts.get(code);
   1.280 +            codeCounts.put(code, (i == null) ? 1 : i + 1);
   1.281 +        }
   1.282 +
   1.283 +        void report(PrintWriter out) {
   1.284 +            if (codeCounts == null) {
   1.285 +                return;
   1.286 +            }
   1.287 +            out.println("By group...");
   1.288 +            Table groupTable = new Table();
   1.289 +            for (Messages.Group g : Messages.Group.values()) {
   1.290 +                groupTable.put(g.optName(), groupCounts[g.ordinal()]);
   1.291 +            }
   1.292 +            groupTable.print(out);
   1.293 +            out.println();
   1.294 +            out.println("By diagnostic kind...");
   1.295 +            Table dkindTable = new Table();
   1.296 +            for (Diagnostic.Kind k : Diagnostic.Kind.values()) {
   1.297 +                dkindTable.put(StringUtils.toLowerCase(k.toString()), dkindCounts[k.ordinal()]);
   1.298 +            }
   1.299 +            dkindTable.print(out);
   1.300 +            out.println();
   1.301 +            out.println("By message kind...");
   1.302 +            Table codeTable = new Table();
   1.303 +            for (Map.Entry<String, Integer> e : codeCounts.entrySet()) {
   1.304 +                String code = e.getKey();
   1.305 +                String msg;
   1.306 +                try {
   1.307 +                    msg = code.equals(NO_CODE) ? "OTHER" : bundle.getString(code);
   1.308 +                } catch (MissingResourceException ex) {
   1.309 +                    msg = code;
   1.310 +                }
   1.311 +                codeTable.put(msg, e.getValue());
   1.312 +            }
   1.313 +            codeTable.print(out);
   1.314 +        }
   1.315 +
   1.316 +        /**
   1.317 +         * A table of (int, String) sorted by decreasing int.
   1.318 +         */
   1.319 +        private static class Table {
   1.320 +
   1.321 +            private static final Comparator<Integer> DECREASING = new Comparator<Integer>() {
   1.322 +
   1.323 +                public int compare(Integer o1, Integer o2) {
   1.324 +                    return o2.compareTo(o1);
   1.325 +                }
   1.326 +            };
   1.327 +            private final TreeMap<Integer, Set<String>> map = new TreeMap<Integer, Set<String>>(DECREASING);
   1.328 +
   1.329 +            void put(String label, int n) {
   1.330 +                if (n == 0) {
   1.331 +                    return;
   1.332 +                }
   1.333 +                Set<String> labels = map.get(n);
   1.334 +                if (labels == null) {
   1.335 +                    map.put(n, labels = new TreeSet<String>());
   1.336 +                }
   1.337 +                labels.add(label);
   1.338 +            }
   1.339 +
   1.340 +            void print(PrintWriter out) {
   1.341 +                for (Map.Entry<Integer, Set<String>> e : map.entrySet()) {
   1.342 +                    int count = e.getKey();
   1.343 +                    Set<String> labels = e.getValue();
   1.344 +                    for (String label : labels) {
   1.345 +                        out.println(String.format("%6d: %s", count, label));
   1.346 +                    }
   1.347 +                }
   1.348 +            }
   1.349 +        }
   1.350 +    }
   1.351 +    // </editor-fold>
   1.352 +}

mercurial