test/tools/javac/diags/CheckResourceKeys.java

changeset 597
d2b7ecf33b35
child 605
0e1fab5cffc8
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/test/tools/javac/diags/CheckResourceKeys.java	Wed Jun 30 18:06:29 2010 -0700
     1.3 @@ -0,0 +1,362 @@
     1.4 +/*
     1.5 + * Copyright (c) 2010, 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.
    1.11 + *
    1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.14 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.15 + * version 2 for more details (a copy is included in the LICENSE file that
    1.16 + * accompanied this code).
    1.17 + *
    1.18 + * You should have received a copy of the GNU General Public License version
    1.19 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.21 + *
    1.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.23 + * or visit www.oracle.com if you need additional information or have any
    1.24 + * questions.
    1.25 + */
    1.26 +
    1.27 +/*
    1.28 + * @test
    1.29 + * @bug 6964768 6964461 6964469 6964487 6964460 6964481
    1.30 + * @summary need test program to validate javac resource bundles
    1.31 + */
    1.32 +
    1.33 +import java.io.*;
    1.34 +import java.util.*;
    1.35 +import javax.tools.*;
    1.36 +import com.sun.tools.classfile.*;
    1.37 +
    1.38 +/**
    1.39 + * Compare string constants in javac classes against keys in javac resource bundles.
    1.40 + */
    1.41 +public class CheckResourceKeys {
    1.42 +    /**
    1.43 +     * Main program.
    1.44 +     * Options:
    1.45 +     * -finddeadkeys
    1.46 +     *      look for keys in resource bundles that are no longer required
    1.47 +     * -findmissingkeys
    1.48 +     *      look for keys in resource bundles that are missing
    1.49 +     *
    1.50 +     * @throws Exception if invoked by jtreg and errors occur
    1.51 +     */
    1.52 +    public static void main(String... args) throws Exception {
    1.53 +        CheckResourceKeys c = new CheckResourceKeys();
    1.54 +        if (c.run(args))
    1.55 +            return;
    1.56 +
    1.57 +        if (is_jtreg())
    1.58 +            throw new Exception(c.errors + " errors occurred");
    1.59 +        else
    1.60 +            System.exit(1);
    1.61 +    }
    1.62 +
    1.63 +    static boolean is_jtreg() {
    1.64 +        return (System.getProperty("test.src") != null);
    1.65 +    }
    1.66 +
    1.67 +    /**
    1.68 +     * Main entry point.
    1.69 +     */
    1.70 +    boolean run(String... args) throws Exception {
    1.71 +        boolean findDeadKeys = false;
    1.72 +        boolean findMissingKeys = false;
    1.73 +
    1.74 +        if (args.length == 0) {
    1.75 +            if (is_jtreg()) {
    1.76 +                findDeadKeys = true;
    1.77 +                findMissingKeys = true;
    1.78 +            } else {
    1.79 +                System.err.println("Usage: java CheckResourceKeys <options>");
    1.80 +                System.err.println("where options include");
    1.81 +                System.err.println("  -finddeadkeys      find keys in resource bundles which are no longer required");
    1.82 +                System.err.println("  -findmissingkeys   find keys in resource bundles that are required but missing");
    1.83 +                return true;
    1.84 +            }
    1.85 +        } else {
    1.86 +            for (String arg: args) {
    1.87 +                if (arg.equalsIgnoreCase("-finddeadkeys"))
    1.88 +                    findDeadKeys = true;
    1.89 +                else if (arg.equalsIgnoreCase("-findmissingkeys"))
    1.90 +                    findMissingKeys = true;
    1.91 +                else
    1.92 +                    error("bad option: " + arg);
    1.93 +            }
    1.94 +        }
    1.95 +
    1.96 +        if (errors > 0)
    1.97 +            return false;
    1.98 +
    1.99 +        Set<String> codeStrings = getCodeStrings();
   1.100 +        Set<String> resourceKeys = getResourceKeys();
   1.101 +
   1.102 +        if (findDeadKeys)
   1.103 +            findDeadKeys(codeStrings, resourceKeys);
   1.104 +
   1.105 +        if (findMissingKeys)
   1.106 +            findMissingKeys(codeStrings, resourceKeys);
   1.107 +
   1.108 +        return (errors == 0);
   1.109 +    }
   1.110 +
   1.111 +    /**
   1.112 +     * Find keys in resource bundles which are probably no longer required.
   1.113 +     * A key is probably required if there is a string fragment in the code
   1.114 +     * that is part of the resource key, or if the key is well-known
   1.115 +     * according to various pragmatic rules.
   1.116 +     */
   1.117 +    void findDeadKeys(Set<String> codeStrings, Set<String> resourceKeys) {
   1.118 +        String[] prefixes = {
   1.119 +            "compiler.err.", "compiler.warn.", "compiler.note.", "compiler.misc.",
   1.120 +            "javac."
   1.121 +        };
   1.122 +        for (String rk: resourceKeys) {
   1.123 +            // some keys are used directly, without a prefix.
   1.124 +            if (codeStrings.contains(rk))
   1.125 +                continue;
   1.126 +
   1.127 +            // remove standard prefix
   1.128 +            String s = null;
   1.129 +            for (int i = 0; i < prefixes.length && s == null; i++) {
   1.130 +                if (rk.startsWith(prefixes[i])) {
   1.131 +                    s = rk.substring(prefixes[i].length());
   1.132 +                }
   1.133 +            }
   1.134 +            if (s == null) {
   1.135 +                error("Resource key does not start with a standard prefix: " + rk);
   1.136 +                continue;
   1.137 +            }
   1.138 +
   1.139 +            if (codeStrings.contains(s))
   1.140 +                continue;
   1.141 +
   1.142 +            // keys ending in .1 are often synthesized
   1.143 +            if (s.endsWith(".1") && codeStrings.contains(s.substring(0, s.length() - 2)))
   1.144 +                continue;
   1.145 +
   1.146 +            // verbose keys are generated by ClassReader.printVerbose
   1.147 +            if (s.startsWith("verbose.") && codeStrings.contains(s.substring(8)))
   1.148 +                continue;
   1.149 +
   1.150 +            // mandatory warning messages are synthesized with no characteristic substring
   1.151 +            if (isMandatoryWarningString(s))
   1.152 +                continue;
   1.153 +
   1.154 +            // check known (valid) exceptions
   1.155 +            if (knownRequired.contains(rk))
   1.156 +                continue;
   1.157 +
   1.158 +            // check known suspects
   1.159 +            if (needToInvestigate.contains(rk))
   1.160 +                continue;
   1.161 +
   1.162 +            error("Resource key not found in code: " + rk);
   1.163 +        }
   1.164 +    }
   1.165 +
   1.166 +    /**
   1.167 +     * The keys for mandatory warning messages are all synthesized and do not
   1.168 +     * have a significant recognizable substring to look for.
   1.169 +     */
   1.170 +    private boolean isMandatoryWarningString(String s) {
   1.171 +        String[] bases = { "deprecated", "unchecked", "varargs", "sunapi" };
   1.172 +        String[] tails = { ".filename", ".filename.additional", ".plural", ".plural.additional", ".recompile" };
   1.173 +        for (String b: bases) {
   1.174 +            if (s.startsWith(b)) {
   1.175 +                String tail = s.substring(b.length());
   1.176 +                for (String t: tails) {
   1.177 +                    if (tail.equals(t))
   1.178 +                        return true;
   1.179 +                }
   1.180 +            }
   1.181 +        }
   1.182 +        return false;
   1.183 +    }
   1.184 +
   1.185 +    Set<String> knownRequired = new TreeSet<String>(Arrays.asList(
   1.186 +        // See Resolve.getErrorKey
   1.187 +        "compiler.err.cant.resolve.args",
   1.188 +        "compiler.err.cant.resolve.args.params",
   1.189 +        "compiler.err.cant.resolve.location.args",
   1.190 +        "compiler.err.cant.resolve.location.args.params",
   1.191 +        // JavaCompiler, reports #errors and #warnings
   1.192 +        "compiler.misc.count.error",
   1.193 +        "compiler.misc.count.error.plural",
   1.194 +        "compiler.misc.count.warn",
   1.195 +        "compiler.misc.count.warn.plural",
   1.196 +        // Used for LintCategory
   1.197 +        "compiler.warn.lintOption",
   1.198 +        // Other
   1.199 +        "compiler.misc.base.membership"                                 // (sic)
   1.200 +        ));
   1.201 +
   1.202 +
   1.203 +    Set<String> needToInvestigate = new TreeSet<String>(Arrays.asList(
   1.204 +        "compiler.err.cant.read.file",                      // UNUSED
   1.205 +        "compiler.err.illegal.self.ref",                    // UNUSED
   1.206 +        "compiler.err.io.exception",                        // UNUSED
   1.207 +        "compiler.err.limit.pool.in.class",                 // UNUSED
   1.208 +        "compiler.err.name.reserved.for.internal.use",      // UNUSED
   1.209 +        "compiler.err.no.match.entry",                      // UNUSED
   1.210 +        "compiler.err.not.within.bounds.explain",           // UNUSED
   1.211 +        "compiler.err.signature.doesnt.match.intf",         // UNUSED
   1.212 +        "compiler.err.signature.doesnt.match.supertype",    // UNUSED
   1.213 +        "compiler.err.type.var.more.than.once",             // UNUSED
   1.214 +        "compiler.err.type.var.more.than.once.in.result",   // UNUSED
   1.215 +        "compiler.misc.ccf.found.later.version",            // UNUSED
   1.216 +        "compiler.misc.non.denotable.type",                 // UNUSED
   1.217 +        "compiler.misc.unnamed.package",                    // should be required, CR 6964147
   1.218 +        "compiler.misc.verbose.retro",                      // UNUSED
   1.219 +        "compiler.misc.verbose.retro.with",                 // UNUSED
   1.220 +        "compiler.misc.verbose.retro.with.list",            // UNUSED
   1.221 +        "compiler.warn.proc.type.already.exists",           // TODO in JavacFiler
   1.222 +        "javac.err.invalid.arg",                            // UNUSED ??
   1.223 +        "javac.opt.arg.class",                              // UNUSED ??
   1.224 +        "javac.opt.arg.pathname",                           // UNUSED ??
   1.225 +        "javac.opt.moreinfo",                               // option commented out
   1.226 +        "javac.opt.nogj",                                   // UNUSED
   1.227 +        "javac.opt.printflat",                              // option commented out
   1.228 +        "javac.opt.printsearch",                            // option commented out
   1.229 +        "javac.opt.prompt",                                 // option commented out
   1.230 +        "javac.opt.retrofit",                               // UNUSED
   1.231 +        "javac.opt.s",                                      // option commented out
   1.232 +        "javac.opt.scramble",                               // option commented out
   1.233 +        "javac.opt.scrambleall"                             // option commented out
   1.234 +        ));
   1.235 +
   1.236 +    /**
   1.237 +     * For all strings in the code that look like they might be fragments of
   1.238 +     * a resource key, verify that a key exists.
   1.239 +     */
   1.240 +    void findMissingKeys(Set<String> codeStrings, Set<String> resourceKeys) {
   1.241 +        for (String cs: codeStrings) {
   1.242 +            if (cs.matches("[A-Za-z][^.]*\\..*")) {
   1.243 +                // ignore filenames (i.e. in SourceFile attribute
   1.244 +                if (cs.matches(".*\\.java"))
   1.245 +                    continue;
   1.246 +                // ignore package and class names
   1.247 +                if (cs.matches("(com|java|javax|sun)\\.[A-Za-z.]+"))
   1.248 +                    continue;
   1.249 +                // explicit known exceptions
   1.250 +                if (noResourceRequired.contains(cs))
   1.251 +                    continue;
   1.252 +                // look for matching resource
   1.253 +                if (hasMatch(resourceKeys, cs))
   1.254 +                    continue;
   1.255 +                error("no match for \"" + cs + "\"");
   1.256 +            }
   1.257 +        }
   1.258 +    }
   1.259 +    // where
   1.260 +    private Set<String> noResourceRequired = new HashSet<String>(Arrays.asList(
   1.261 +            // system properties
   1.262 +            "application.home", // in Paths.java
   1.263 +            "env.class.path",
   1.264 +            "line.separator",
   1.265 +            "user.dir",
   1.266 +            // file names
   1.267 +            "ct.sym",
   1.268 +            "rt.jar",
   1.269 +            "tools.jar",
   1.270 +            // -XD option names
   1.271 +            "process.packages",
   1.272 +            "ignore.symbol.file",
   1.273 +            // prefix/embedded strings
   1.274 +            "compiler.",
   1.275 +            "compiler.misc.",
   1.276 +            "count.",
   1.277 +            "illegal.",
   1.278 +            "javac.",
   1.279 +            "verbose."
   1.280 +    ));
   1.281 +
   1.282 +    /**
   1.283 +     * Look for a resource that ends in this string fragment.
   1.284 +     */
   1.285 +    boolean hasMatch(Set<String> resourceKeys, String s) {
   1.286 +        for (String rk: resourceKeys) {
   1.287 +            if (rk.endsWith(s))
   1.288 +                return true;
   1.289 +        }
   1.290 +        return false;
   1.291 +    }
   1.292 +
   1.293 +    /**
   1.294 +     * Get the set of strings from (most of) the javac classfiles.
   1.295 +     */
   1.296 +    Set<String> getCodeStrings() throws IOException {
   1.297 +        Set<String> results = new TreeSet<String>();
   1.298 +        JavaCompiler c = ToolProvider.getSystemJavaCompiler();
   1.299 +        JavaFileManager fm = c.getStandardFileManager(null, null, null);
   1.300 +        String[] pkgs = {
   1.301 +            "javax.annotation.processing",
   1.302 +            "javax.lang.model",
   1.303 +            "javax.tools",
   1.304 +            "com.sun.source",
   1.305 +            "com.sun.tools.javac"
   1.306 +        };
   1.307 +        for (String pkg: pkgs) {
   1.308 +            for (JavaFileObject fo: fm.list(StandardLocation.PLATFORM_CLASS_PATH,
   1.309 +                    pkg, EnumSet.of(JavaFileObject.Kind.CLASS), true)) {
   1.310 +                String name = fo.getName();
   1.311 +                // ignore resource files, and files which are not really part of javac
   1.312 +                if (name.contains("resources")
   1.313 +                        || name.contains("Launcher.class")
   1.314 +                        || name.contains("CreateSymbols.class"))
   1.315 +                    continue;
   1.316 +                scan(fo, results);
   1.317 +            }
   1.318 +        }
   1.319 +        return results;
   1.320 +    }
   1.321 +
   1.322 +    /**
   1.323 +     * Get the set of strings from a class file.
   1.324 +     * Only strings that look like they might be a resource key are returned.
   1.325 +     */
   1.326 +    void scan(JavaFileObject fo, Set<String> results) throws IOException {
   1.327 +        InputStream in = fo.openInputStream();
   1.328 +        try {
   1.329 +            ClassFile cf = ClassFile.read(in);
   1.330 +            for (ConstantPool.CPInfo cpinfo: cf.constant_pool.entries()) {
   1.331 +                if (cpinfo.getTag() == ConstantPool.CONSTANT_Utf8) {
   1.332 +                    String v = ((ConstantPool.CONSTANT_Utf8_info) cpinfo).value;
   1.333 +                    if (v.matches("[A-Za-z0-9-_.]+"))
   1.334 +                        results.add(v);
   1.335 +                }
   1.336 +            }
   1.337 +        } catch (ConstantPoolException ignore) {
   1.338 +        } finally {
   1.339 +            in.close();
   1.340 +        }
   1.341 +    }
   1.342 +
   1.343 +    /**
   1.344 +     * Get the set of keys from the javac resource bundles.
   1.345 +     */
   1.346 +    Set<String> getResourceKeys() {
   1.347 +        Set<String> results = new TreeSet<String>();
   1.348 +        for (String name : new String[]{"javac", "compiler"}) {
   1.349 +            ResourceBundle b =
   1.350 +                    ResourceBundle.getBundle("com.sun.tools.javac.resources." + name);
   1.351 +            results.addAll(b.keySet());
   1.352 +        }
   1.353 +        return results;
   1.354 +    }
   1.355 +
   1.356 +    /**
   1.357 +     * Report an error.
   1.358 +     */
   1.359 +    void error(String msg) {
   1.360 +        System.err.println("Error: " + msg);
   1.361 +        errors++;
   1.362 +    }
   1.363 +
   1.364 +    int errors;
   1.365 +}

mercurial