1 /*
   2  * Copyright (c) 2010, 2016, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25  * @test
  26  * @bug 6964768 6964461 6964469 6964487 6964460 6964481 6980021
  27  * @summary need test program to validate javac resource bundles
  28  * @modules jdk.compiler/com.sun.tools.javac.code
  29  *          jdk.compiler/com.sun.tools.javac.resources:open
  30  *          jdk.jdeps/com.sun.tools.classfile
  31  */
  32 
  33 import java.io.*;
  34 import java.util.*;
  35 import javax.tools.*;
  36 import com.sun.tools.classfile.*;
  37 import com.sun.tools.javac.code.Lint.LintCategory;
  38 
  39 /**
  40  * Compare string constants in javac classes against keys in javac resource bundles.
  41  */
  42 public class CheckResourceKeys {
  43     /**
  44      * Main program.
  45      * Options:
  46      * -finddeadkeys
  47      *      look for keys in resource bundles that are no longer required
  48      * -findmissingkeys
  49      *      look for keys in resource bundles that are missing
  50      *
  51      * @throws Exception if invoked by jtreg and errors occur
  52      */
  53     public static void main(String... args) throws Exception {
  54         CheckResourceKeys c = new CheckResourceKeys();
  55         if (c.run(args))
  56             return;
  57 
  58         if (is_jtreg())
  59             throw new Exception(c.errors + " errors occurred");
  60         else
  61             System.exit(1);
  62     }
  63 
  64     static boolean is_jtreg() {
  65         return (System.getProperty("test.src") != null);
  66     }
  67 
  68     /**
  69      * Main entry point.
  70      */
  71     boolean run(String... args) throws Exception {
  72         boolean findDeadKeys = false;
  73         boolean findMissingKeys = false;
  74 
  75         if (args.length == 0) {
  76             if (is_jtreg()) {
  77                 findDeadKeys = true;
  78                 findMissingKeys = true;
  79             } else {
  80                 System.err.println("Usage: java CheckResourceKeys <options>");
  81                 System.err.println("where options include");
  82                 System.err.println("  -finddeadkeys      find keys in resource bundles which are no longer required");
  83                 System.err.println("  -findmissingkeys   find keys in resource bundles that are required but missing");
  84                 return true;
  85             }
  86         } else {
  87             for (String arg: args) {
  88                 if (arg.equalsIgnoreCase("-finddeadkeys"))
  89                     findDeadKeys = true;
  90                 else if (arg.equalsIgnoreCase("-findmissingkeys"))
  91                     findMissingKeys = true;
  92                 else
  93                     error("bad option: " + arg);
  94             }
  95         }
  96 
  97         if (errors > 0)
  98             return false;
  99 
 100         Set<String> codeStrings = getCodeStrings();
 101         Set<String> resourceKeys = getResourceKeys();
 102 
 103         if (findDeadKeys)
 104             findDeadKeys(codeStrings, resourceKeys);
 105 
 106         if (findMissingKeys)
 107             findMissingKeys(codeStrings, resourceKeys);
 108 
 109         return (errors == 0);
 110     }
 111 
 112     /**
 113      * Find keys in resource bundles which are probably no longer required.
 114      * A key is probably required if there is a string fragment in the code
 115      * that is part of the resource key, or if the key is well-known
 116      * according to various pragmatic rules.
 117      */
 118     void findDeadKeys(Set<String> codeStrings, Set<String> resourceKeys) {
 119         String[] prefixes = {
 120             "compiler.err.", "compiler.warn.", "compiler.note.", "compiler.misc.",
 121             "javac."
 122         };
 123         for (String rk: resourceKeys) {
 124             // some keys are used directly, without a prefix.
 125             if (codeStrings.contains(rk))
 126                 continue;
 127 
 128             // remove standard prefix
 129             String s = null;
 130             for (int i = 0; i < prefixes.length && s == null; i++) {
 131                 if (rk.startsWith(prefixes[i])) {
 132                     s = rk.substring(prefixes[i].length());
 133                 }
 134             }
 135             if (s == null) {
 136                 error("Resource key does not start with a standard prefix: " + rk);
 137                 continue;
 138             }
 139 
 140             if (codeStrings.contains(s))
 141                 continue;
 142 
 143             // keys ending in .1 are often synthesized
 144             if (s.endsWith(".1") && codeStrings.contains(s.substring(0, s.length() - 2)))
 145                 continue;
 146 
 147             // verbose keys are generated by ClassReader.printVerbose
 148             if (s.startsWith("verbose.") && codeStrings.contains(s.substring(8)))
 149                 continue;
 150 
 151             // mandatory warning messages are synthesized with no characteristic substring
 152             if (isMandatoryWarningString(s))
 153                 continue;
 154 
 155             // check known (valid) exceptions
 156             if (knownRequired.contains(rk))
 157                 continue;
 158 
 159             // check known suspects
 160             if (needToInvestigate.contains(rk))
 161                 continue;
 162 
 163             //check lint description keys:
 164             if (s.startsWith("opt.Xlint.desc.")) {
 165                 String option = s.substring(15);
 166                 boolean found = false;
 167 
 168                 for (LintCategory lc : LintCategory.values()) {
 169                     if (option.equals(lc.option))
 170                         found = true;
 171                 }
 172 
 173                 if (found)
 174                     continue;
 175             }
 176 
 177             error("Resource key not found in code: " + rk);
 178         }
 179     }
 180 
 181     /**
 182      * The keys for mandatory warning messages are all synthesized and do not
 183      * have a significant recognizable substring to look for.
 184      */
 185     private boolean isMandatoryWarningString(String s) {
 186         String[] bases = { "deprecated", "unchecked", "varargs" };
 187         String[] tails = { ".filename", ".filename.additional", ".plural", ".plural.additional", ".recompile" };
 188         for (String b: bases) {
 189             if (s.startsWith(b)) {
 190                 String tail = s.substring(b.length());
 191                 for (String t: tails) {
 192                     if (tail.equals(t))
 193                         return true;
 194                 }
 195             }
 196         }
 197         return false;
 198     }
 199 
 200     Set<String> knownRequired = new TreeSet<String>(Arrays.asList(
 201         // See Resolve.getErrorKey
 202         "compiler.err.cant.resolve.args",
 203         "compiler.err.cant.resolve.args.params",
 204         "compiler.err.cant.resolve.location.args",
 205         "compiler.err.cant.resolve.location.args.params",
 206         "compiler.misc.cant.resolve.location.args",
 207         "compiler.misc.cant.resolve.location.args.params",
 208         // JavaCompiler, reports #errors and #warnings
 209         "compiler.misc.count.error",
 210         "compiler.misc.count.error.plural",
 211         "compiler.misc.count.warn",
 212         "compiler.misc.count.warn.plural",
 213         // Used for LintCategory
 214         "compiler.warn.lintOption",
 215         // Other
 216         "compiler.misc.base.membership"                                 // (sic)
 217         ));
 218 
 219 
 220     Set<String> needToInvestigate = new TreeSet<String>(Arrays.asList(
 221         "compiler.misc.fatal.err.cant.close.loader",        // Supressed by JSR308
 222         "compiler.err.cant.read.file",                      // UNUSED
 223         "compiler.err.illegal.self.ref",                    // UNUSED
 224         "compiler.err.io.exception",                        // UNUSED
 225         "compiler.err.limit.pool.in.class",                 // UNUSED
 226         "compiler.err.name.reserved.for.internal.use",      // UNUSED
 227         "compiler.err.no.match.entry",                      // UNUSED
 228         "compiler.err.not.within.bounds.explain",           // UNUSED
 229         "compiler.err.signature.doesnt.match.intf",         // UNUSED
 230         "compiler.err.signature.doesnt.match.supertype",    // UNUSED
 231         "compiler.err.type.var.more.than.once",             // UNUSED
 232         "compiler.err.type.var.more.than.once.in.result",   // UNUSED
 233         "compiler.misc.non.denotable.type",                 // UNUSED
 234         "compiler.misc.unnamed.package",                    // should be required, CR 6964147
 235         "compiler.warn.proc.type.already.exists",           // TODO in JavacFiler
 236         "javac.err.invalid.arg",                            // UNUSED ??
 237         "javac.opt.arg.class",                              // UNUSED ??
 238         "javac.opt.arg.pathname",                           // UNUSED ??
 239         "javac.opt.moreinfo",                               // option commented out
 240         "javac.opt.nogj",                                   // UNUSED
 241         "javac.opt.printsearch",                            // option commented out
 242         "javac.opt.prompt",                                 // option commented out
 243         "javac.opt.s"                                       // option commented out
 244         ));
 245 
 246     /**
 247      * For all strings in the code that look like they might be fragments of
 248      * a resource key, verify that a key exists.
 249      */
 250     void findMissingKeys(Set<String> codeStrings, Set<String> resourceKeys) {
 251         for (String cs: codeStrings) {
 252             if (cs.matches("[A-Za-z][^.]*\\..*")) {
 253                 // ignore filenames (i.e. in SourceFile attribute
 254                 if (cs.matches(".*\\.java"))
 255                     continue;
 256                 // ignore package and class names
 257                 if (cs.matches("(com|java|javax|jdk|sun)\\.[A-Za-z.]+"))
 258                     continue;
 259                 if (cs.matches("(java|javax|sun)\\."))
 260                     continue;
 261                 // ignore debug flag names
 262                 if (cs.startsWith("debug."))
 263                     continue;
 264                 // ignore should-stop flag names
 265                 if (cs.startsWith("should-stop."))
 266                     continue;
 267                 // ignore diagsformat flag names
 268                 if (cs.startsWith("diags."))
 269                     continue;
 270                 // explicit known exceptions
 271                 if (noResourceRequired.contains(cs))
 272                     continue;
 273                 // look for matching resource
 274                 if (hasMatch(resourceKeys, cs))
 275                     continue;
 276                 error("no match for \"" + cs + "\"");
 277             }
 278         }
 279     }
 280     // where
 281     private Set<String> noResourceRequired = new HashSet<String>(Arrays.asList(
 282             // module names
 283             "jdk.compiler",
 284             "jdk.javadoc",
 285             // system properties
 286             "application.home", // in Paths.java
 287             "env.class.path",
 288             "line.separator",
 289             "os.name",
 290             "user.dir",
 291             // file names
 292             "ct.sym",
 293             "rt.jar",
 294             "jfxrt.jar",
 295             "module-info.class",
 296             "jrt-fs.jar",
 297             // -XD option names
 298             "process.packages",
 299             "ignore.symbol.file",
 300             "fileManager.deferClose",
 301             // prefix/embedded strings
 302             "compiler.",
 303             "compiler.misc.",
 304             "opt.Xlint.desc.",
 305             "count.",
 306             "illegal.",
 307             "java.",
 308             "javac.",
 309             "verbose.",
 310             "locn.",
 311             // annotation for minimal value types.
 312             "jvm.internal.value.ValueCapableClass",
 313             "java.lang.__Value"
 314     ));
 315 
 316     /**
 317      * Look for a resource that ends in this string fragment.
 318      */
 319     boolean hasMatch(Set<String> resourceKeys, String s) {
 320         for (String rk: resourceKeys) {
 321             if (rk.endsWith(s))
 322                 return true;
 323         }
 324         return false;
 325     }
 326 
 327     /**
 328      * Get the set of strings from (most of) the javac classfiles.
 329      */
 330     Set<String> getCodeStrings() throws IOException {
 331         Set<String> results = new TreeSet<String>();
 332         JavaCompiler c = ToolProvider.getSystemJavaCompiler();
 333         try (JavaFileManager fm = c.getStandardFileManager(null, null, null)) {
 334             JavaFileManager.Location javacLoc = findJavacLocation(fm);
 335             String[] pkgs = {
 336                 "javax.annotation.processing",
 337                 "javax.lang.model",
 338                 "javax.tools",
 339                 "com.sun.source",
 340                 "com.sun.tools.javac"
 341             };
 342             for (String pkg: pkgs) {
 343                 for (JavaFileObject fo: fm.list(javacLoc,
 344                         pkg, EnumSet.of(JavaFileObject.Kind.CLASS), true)) {
 345                     String name = fo.getName();
 346                     // ignore resource files, and files which are not really part of javac
 347                     if (name.matches(".*resources.[A-Za-z_0-9]+\\.class.*")
 348                             || name.matches(".*CreateSymbols\\.class.*"))
 349                         continue;
 350                     scan(fo, results);
 351                 }
 352             }
 353             return results;
 354         }
 355     }
 356 
 357     // depending on how the test is run, javac may be on bootclasspath or classpath
 358     JavaFileManager.Location findJavacLocation(JavaFileManager fm) {
 359         JavaFileManager.Location[] locns =
 360             { StandardLocation.PLATFORM_CLASS_PATH, StandardLocation.CLASS_PATH };
 361         try {
 362             for (JavaFileManager.Location l: locns) {
 363                 JavaFileObject fo = fm.getJavaFileForInput(l,
 364                     "com.sun.tools.javac.Main", JavaFileObject.Kind.CLASS);
 365                 if (fo != null)
 366                     return l;
 367             }
 368         } catch (IOException e) {
 369             throw new Error(e);
 370         }
 371         throw new IllegalStateException("Cannot find javac");
 372     }
 373 
 374     /**
 375      * Get the set of strings from a class file.
 376      * Only strings that look like they might be a resource key are returned.
 377      */
 378     void scan(JavaFileObject fo, Set<String> results) throws IOException {
 379         InputStream in = fo.openInputStream();
 380         try {
 381             ClassFile cf = ClassFile.read(in);
 382             for (ConstantPool.CPInfo cpinfo: cf.constant_pool.entries()) {
 383                 if (cpinfo.getTag() == ConstantPool.CONSTANT_Utf8) {
 384                     String v = ((ConstantPool.CONSTANT_Utf8_info) cpinfo).value;
 385                     if (v.matches("[A-Za-z0-9-_.]+"))
 386                         results.add(v);
 387                 }
 388             }
 389         } catch (ConstantPoolException ignore) {
 390         } finally {
 391             in.close();
 392         }
 393     }
 394 
 395     /**
 396      * Get the set of keys from the javac resource bundles.
 397      */
 398     Set<String> getResourceKeys() {
 399         Module jdk_compiler = ModuleLayer.boot().findModule("jdk.compiler").get();
 400         Set<String> results = new TreeSet<String>();
 401         for (String name : new String[]{"javac", "compiler"}) {
 402             ResourceBundle b =
 403                     ResourceBundle.getBundle("com.sun.tools.javac.resources." + name, jdk_compiler);
 404             results.addAll(b.keySet());
 405         }
 406         return results;
 407     }
 408 
 409     /**
 410      * Report an error.
 411      */
 412     void error(String msg) {
 413         System.err.println("Error: " + msg);
 414         errors++;
 415     }
 416 
 417     int errors;
 418 }