1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/test/tools/javac/diags/CheckResourceKeys.java Wed Apr 27 01:34:52 2016 +0800 1.3 @@ -0,0 +1,383 @@ 1.4 +/* 1.5 + * Copyright (c) 2010, 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. 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 6980021 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 + "compiler.misc.cant.resolve.location.args", 1.192 + "compiler.misc.cant.resolve.location.args.params", 1.193 + // JavaCompiler, reports #errors and #warnings 1.194 + "compiler.misc.count.error", 1.195 + "compiler.misc.count.error.plural", 1.196 + "compiler.misc.count.warn", 1.197 + "compiler.misc.count.warn.plural", 1.198 + // Used for LintCategory 1.199 + "compiler.warn.lintOption", 1.200 + // Other 1.201 + "compiler.misc.base.membership" // (sic) 1.202 + )); 1.203 + 1.204 + 1.205 + Set<String> needToInvestigate = new TreeSet<String>(Arrays.asList( 1.206 + "compiler.misc.fatal.err.cant.close.loader", // Supressed by JSR308 1.207 + "compiler.err.cant.read.file", // UNUSED 1.208 + "compiler.err.illegal.self.ref", // UNUSED 1.209 + "compiler.err.io.exception", // UNUSED 1.210 + "compiler.err.limit.pool.in.class", // UNUSED 1.211 + "compiler.err.name.reserved.for.internal.use", // UNUSED 1.212 + "compiler.err.no.match.entry", // UNUSED 1.213 + "compiler.err.not.within.bounds.explain", // UNUSED 1.214 + "compiler.err.signature.doesnt.match.intf", // UNUSED 1.215 + "compiler.err.signature.doesnt.match.supertype", // UNUSED 1.216 + "compiler.err.type.var.more.than.once", // UNUSED 1.217 + "compiler.err.type.var.more.than.once.in.result", // UNUSED 1.218 + "compiler.misc.ccf.found.later.version", // UNUSED 1.219 + "compiler.misc.non.denotable.type", // UNUSED 1.220 + "compiler.misc.unnamed.package", // should be required, CR 6964147 1.221 + "compiler.misc.verbose.retro", // UNUSED 1.222 + "compiler.misc.verbose.retro.with", // UNUSED 1.223 + "compiler.misc.verbose.retro.with.list", // UNUSED 1.224 + "compiler.warn.proc.type.already.exists", // TODO in JavacFiler 1.225 + "javac.err.invalid.arg", // UNUSED ?? 1.226 + "javac.opt.arg.class", // UNUSED ?? 1.227 + "javac.opt.arg.pathname", // UNUSED ?? 1.228 + "javac.opt.moreinfo", // option commented out 1.229 + "javac.opt.nogj", // UNUSED 1.230 + "javac.opt.printflat", // option commented out 1.231 + "javac.opt.printsearch", // option commented out 1.232 + "javac.opt.prompt", // option commented out 1.233 + "javac.opt.retrofit", // UNUSED 1.234 + "javac.opt.s", // option commented out 1.235 + "javac.opt.scramble", // option commented out 1.236 + "javac.opt.scrambleall" // option commented out 1.237 + )); 1.238 + 1.239 + /** 1.240 + * For all strings in the code that look like they might be fragments of 1.241 + * a resource key, verify that a key exists. 1.242 + */ 1.243 + void findMissingKeys(Set<String> codeStrings, Set<String> resourceKeys) { 1.244 + for (String cs: codeStrings) { 1.245 + if (cs.matches("[A-Za-z][^.]*\\..*")) { 1.246 + // ignore filenames (i.e. in SourceFile attribute 1.247 + if (cs.matches(".*\\.java")) 1.248 + continue; 1.249 + // ignore package and class names 1.250 + if (cs.matches("(com|java|javax|sun)\\.[A-Za-z.]+")) 1.251 + continue; 1.252 + // explicit known exceptions 1.253 + if (noResourceRequired.contains(cs)) 1.254 + continue; 1.255 + // look for matching resource 1.256 + if (hasMatch(resourceKeys, cs)) 1.257 + continue; 1.258 + error("no match for \"" + cs + "\""); 1.259 + } 1.260 + } 1.261 + } 1.262 + // where 1.263 + private Set<String> noResourceRequired = new HashSet<String>(Arrays.asList( 1.264 + // system properties 1.265 + "application.home", // in Paths.java 1.266 + "env.class.path", 1.267 + "line.separator", 1.268 + "os.name", 1.269 + "user.dir", 1.270 + // file names 1.271 + "ct.sym", 1.272 + "rt.jar", 1.273 + "tools.jar", 1.274 + // -XD option names 1.275 + "process.packages", 1.276 + "ignore.symbol.file", 1.277 + // prefix/embedded strings 1.278 + "compiler.", 1.279 + "compiler.misc.", 1.280 + "count.", 1.281 + "illegal.", 1.282 + "javac.", 1.283 + "verbose." 1.284 + )); 1.285 + 1.286 + /** 1.287 + * Look for a resource that ends in this string fragment. 1.288 + */ 1.289 + boolean hasMatch(Set<String> resourceKeys, String s) { 1.290 + for (String rk: resourceKeys) { 1.291 + if (rk.endsWith(s)) 1.292 + return true; 1.293 + } 1.294 + return false; 1.295 + } 1.296 + 1.297 + /** 1.298 + * Get the set of strings from (most of) the javac classfiles. 1.299 + */ 1.300 + Set<String> getCodeStrings() throws IOException { 1.301 + Set<String> results = new TreeSet<String>(); 1.302 + JavaCompiler c = ToolProvider.getSystemJavaCompiler(); 1.303 + JavaFileManager fm = c.getStandardFileManager(null, null, null); 1.304 + JavaFileManager.Location javacLoc = findJavacLocation(fm); 1.305 + String[] pkgs = { 1.306 + "javax.annotation.processing", 1.307 + "javax.lang.model", 1.308 + "javax.tools", 1.309 + "com.sun.source", 1.310 + "com.sun.tools.javac" 1.311 + }; 1.312 + for (String pkg: pkgs) { 1.313 + for (JavaFileObject fo: fm.list(javacLoc, 1.314 + pkg, EnumSet.of(JavaFileObject.Kind.CLASS), true)) { 1.315 + String name = fo.getName(); 1.316 + // ignore resource files, and files which are not really part of javac 1.317 + if (name.matches(".*resources.[A-Za-z_0-9]+\\.class.*") 1.318 + || name.matches(".*CreateSymbols\\.class.*")) 1.319 + continue; 1.320 + scan(fo, results); 1.321 + } 1.322 + } 1.323 + return results; 1.324 + } 1.325 + 1.326 + // depending on how the test is run, javac may be on bootclasspath or classpath 1.327 + JavaFileManager.Location findJavacLocation(JavaFileManager fm) { 1.328 + JavaFileManager.Location[] locns = 1.329 + { StandardLocation.PLATFORM_CLASS_PATH, StandardLocation.CLASS_PATH }; 1.330 + try { 1.331 + for (JavaFileManager.Location l: locns) { 1.332 + JavaFileObject fo = fm.getJavaFileForInput(l, 1.333 + "com.sun.tools.javac.Main", JavaFileObject.Kind.CLASS); 1.334 + if (fo != null) 1.335 + return l; 1.336 + } 1.337 + } catch (IOException e) { 1.338 + throw new Error(e); 1.339 + } 1.340 + throw new IllegalStateException("Cannot find javac"); 1.341 + } 1.342 + 1.343 + /** 1.344 + * Get the set of strings from a class file. 1.345 + * Only strings that look like they might be a resource key are returned. 1.346 + */ 1.347 + void scan(JavaFileObject fo, Set<String> results) throws IOException { 1.348 + InputStream in = fo.openInputStream(); 1.349 + try { 1.350 + ClassFile cf = ClassFile.read(in); 1.351 + for (ConstantPool.CPInfo cpinfo: cf.constant_pool.entries()) { 1.352 + if (cpinfo.getTag() == ConstantPool.CONSTANT_Utf8) { 1.353 + String v = ((ConstantPool.CONSTANT_Utf8_info) cpinfo).value; 1.354 + if (v.matches("[A-Za-z0-9-_.]+")) 1.355 + results.add(v); 1.356 + } 1.357 + } 1.358 + } catch (ConstantPoolException ignore) { 1.359 + } finally { 1.360 + in.close(); 1.361 + } 1.362 + } 1.363 + 1.364 + /** 1.365 + * Get the set of keys from the javac resource bundles. 1.366 + */ 1.367 + Set<String> getResourceKeys() { 1.368 + Set<String> results = new TreeSet<String>(); 1.369 + for (String name : new String[]{"javac", "compiler"}) { 1.370 + ResourceBundle b = 1.371 + ResourceBundle.getBundle("com.sun.tools.javac.resources." + name); 1.372 + results.addAll(b.keySet()); 1.373 + } 1.374 + return results; 1.375 + } 1.376 + 1.377 + /** 1.378 + * Report an error. 1.379 + */ 1.380 + void error(String msg) { 1.381 + System.err.println("Error: " + msg); 1.382 + errors++; 1.383 + } 1.384 + 1.385 + int errors; 1.386 +}