jjg@1397: /* jjg@1397: * Copyright (c) 2010, 2012, Oracle and/or its affiliates. All rights reserved. jjg@1397: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. jjg@1397: * jjg@1397: * This code is free software; you can redistribute it and/or modify it jjg@1397: * under the terms of the GNU General Public License version 2 only, as jjg@1397: * published by the Free Software Foundation. jjg@1397: * jjg@1397: * This code is distributed in the hope that it will be useful, but WITHOUT jjg@1397: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or jjg@1397: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License jjg@1397: * version 2 for more details (a copy is included in the LICENSE file that jjg@1397: * accompanied this code). jjg@1397: * jjg@1397: * You should have received a copy of the GNU General Public License version jjg@1397: * 2 along with this work; if not, write to the Free Software Foundation, jjg@1397: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. jjg@1397: * jjg@1397: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA jjg@1397: * or visit www.oracle.com if you need additional information or have any jjg@1397: * questions. jjg@1397: */ jjg@1397: jjg@1397: /* jjg@1397: * @test jjg@1397: * @bug 8000612 jjg@1397: * @summary need test program to validate javadoc resource bundles jjg@1397: */ jjg@1397: jjg@1397: import java.io.*; jjg@1397: import java.util.*; jjg@1397: import javax.tools.*; jjg@1397: import com.sun.tools.classfile.*; jjg@1397: jjg@1397: /** jjg@1397: * Compare string constants in javadoc classes against keys in javadoc resource bundles. jjg@1397: */ jjg@1397: public class CheckResourceKeys { jjg@1397: /** jjg@1397: * Main program. jjg@1397: * Options: jjg@1397: * -finddeadkeys jjg@1397: * look for keys in resource bundles that are no longer required jjg@1397: * -findmissingkeys jjg@1397: * look for keys in resource bundles that are missing jjg@1397: * jjg@1397: * @throws Exception if invoked by jtreg and errors occur jjg@1397: */ jjg@1397: public static void main(String... args) throws Exception { jjg@1397: CheckResourceKeys c = new CheckResourceKeys(); jjg@1397: if (c.run(args)) jjg@1397: return; jjg@1397: jjg@1397: if (is_jtreg()) jjg@1397: throw new Exception(c.errors + " errors occurred"); jjg@1397: else jjg@1397: System.exit(1); jjg@1397: } jjg@1397: jjg@1397: static boolean is_jtreg() { jjg@1397: return (System.getProperty("test.src") != null); jjg@1397: } jjg@1397: jjg@1397: /** jjg@1397: * Main entry point. jjg@1397: */ jjg@1397: boolean run(String... args) throws Exception { jjg@1397: boolean findDeadKeys = false; jjg@1397: boolean findMissingKeys = false; jjg@1397: jjg@1397: if (args.length == 0) { jjg@1397: if (is_jtreg()) { jjg@1397: findDeadKeys = true; jjg@1397: findMissingKeys = true; jjg@1397: } else { jjg@1397: System.err.println("Usage: java CheckResourceKeys "); jjg@1397: System.err.println("where options include"); jjg@1397: System.err.println(" -finddeadkeys find keys in resource bundles which are no longer required"); jjg@1397: System.err.println(" -findmissingkeys find keys in resource bundles that are required but missing"); jjg@1397: return true; jjg@1397: } jjg@1397: } else { jjg@1397: for (String arg: args) { jjg@1397: if (arg.equalsIgnoreCase("-finddeadkeys")) jjg@1397: findDeadKeys = true; jjg@1397: else if (arg.equalsIgnoreCase("-findmissingkeys")) jjg@1397: findMissingKeys = true; jjg@1397: else jjg@1397: error("bad option: " + arg); jjg@1397: } jjg@1397: } jjg@1397: jjg@1397: if (errors > 0) jjg@1397: return false; jjg@1397: jjg@1397: Set codeKeys = getCodeKeys(); jjg@1397: Set resourceKeys = getResourceKeys(); jjg@1397: jjg@1397: System.err.println("found " + codeKeys.size() + " keys in code"); jjg@1397: System.err.println("found " + resourceKeys.size() + " keys in resource bundles"); jjg@1397: jjg@1397: if (findDeadKeys) jjg@1397: findDeadKeys(codeKeys, resourceKeys); jjg@1397: jjg@1397: if (findMissingKeys) jjg@1397: findMissingKeys(codeKeys, resourceKeys); jjg@1397: jjg@1397: return (errors == 0); jjg@1397: } jjg@1397: jjg@1397: /** jjg@1397: * Find keys in resource bundles which are probably no longer required. jjg@1397: * A key is required if there is a string in the code that is a resource key, jjg@1397: * or if the key is well-known according to various pragmatic rules. jjg@1397: */ jjg@1397: void findDeadKeys(Set codeKeys, Set resourceKeys) { jjg@1397: for (String rk: resourceKeys) { jjg@1397: if (codeKeys.contains(rk)) jjg@1397: continue; jjg@1397: jjg@1397: error("Resource key not found in code: " + rk); jjg@1397: } jjg@1397: } jjg@1397: jjg@1397: /** jjg@1397: * For all strings in the code that look like they might be jjg@1397: * a resource key, verify that a key exists. jjg@1397: */ jjg@1397: void findMissingKeys(Set codeKeys, Set resourceKeys) { jjg@1397: for (String ck: codeKeys) { jjg@1397: if (resourceKeys.contains(ck)) jjg@1397: continue; jjg@1397: error("No resource for \"" + ck + "\""); jjg@1397: } jjg@1397: } jjg@1397: jjg@1397: /** jjg@1397: * Get the set of strings from (most of) the javadoc classfiles. jjg@1397: */ jjg@1397: Set getCodeKeys() throws IOException { jjg@1397: Set results = new TreeSet(); jjg@1397: JavaCompiler c = ToolProvider.getSystemJavaCompiler(); jjg@1397: JavaFileManager fm = c.getStandardFileManager(null, null, null); jjg@1397: JavaFileManager.Location javadocLoc = findJavadocLocation(fm); jjg@1397: String[] pkgs = { jjg@1397: "com.sun.tools.doclets", jjg@1397: "com.sun.tools.javadoc" jjg@1397: }; jjg@1397: for (String pkg: pkgs) { jjg@1397: for (JavaFileObject fo: fm.list(javadocLoc, jjg@1397: pkg, EnumSet.of(JavaFileObject.Kind.CLASS), true)) { jjg@1397: String name = fo.getName(); jjg@1397: // ignore resource files jjg@1402: if (name.matches(".*resources.[A-Za-z_0-9]+\\.class.*")) jjg@1397: continue; jjg@1397: scan(fo, results); jjg@1397: } jjg@1397: } jjg@1397: jjg@1397: // special handling for code strings synthesized in jjg@1397: // com.sun.tools.doclets.internal.toolkit.util.Util.getTypeName jjg@1397: String[] extras = { jjg@1397: "AnnotationType", "Class", "Enum", "Error", "Exception", "Interface" jjg@1397: }; jjg@1397: for (String s: extras) { jjg@1397: if (results.contains("doclet." + s)) jjg@1397: results.add("doclet." + s.toLowerCase()); jjg@1397: } jjg@1397: jjg@1413: // special handling for code strings synthesized in jjg@1413: // com.sun.tools.javadoc.Messager jjg@1413: results.add("javadoc.error.msg"); jjg@1413: results.add("javadoc.note.msg"); jjg@1413: results.add("javadoc.note.pos.msg"); jjg@1413: results.add("javadoc.warning.msg"); jjg@1413: jjg@1397: return results; jjg@1397: } jjg@1397: jjg@1397: // depending on how the test is run, javadoc may be on bootclasspath or classpath jjg@1397: JavaFileManager.Location findJavadocLocation(JavaFileManager fm) { jjg@1397: JavaFileManager.Location[] locns = jjg@1397: { StandardLocation.PLATFORM_CLASS_PATH, StandardLocation.CLASS_PATH }; jjg@1397: try { jjg@1397: for (JavaFileManager.Location l: locns) { jjg@1397: JavaFileObject fo = fm.getJavaFileForInput(l, jjg@1397: "com.sun.tools.javadoc.Main", JavaFileObject.Kind.CLASS); jjg@1397: if (fo != null) { jjg@1397: System.err.println("found javadoc in " + l); jjg@1397: return l; jjg@1397: } jjg@1397: } jjg@1397: } catch (IOException e) { jjg@1397: throw new Error(e); jjg@1397: } jjg@1397: throw new IllegalStateException("Cannot find javadoc"); jjg@1397: } jjg@1397: jjg@1397: /** jjg@1397: * Get the set of strings from a class file. jjg@1397: * Only strings that look like they might be a resource key are returned. jjg@1397: */ jjg@1397: void scan(JavaFileObject fo, Set results) throws IOException { jjg@1397: //System.err.println("scan " + fo.getName()); jjg@1397: InputStream in = fo.openInputStream(); jjg@1397: try { jjg@1397: ClassFile cf = ClassFile.read(in); jjg@1397: for (ConstantPool.CPInfo cpinfo: cf.constant_pool.entries()) { jjg@1397: if (cpinfo.getTag() == ConstantPool.CONSTANT_Utf8) { jjg@1397: String v = ((ConstantPool.CONSTANT_Utf8_info) cpinfo).value; jjg@1397: if (v.matches("(doclet|main|javadoc|tag)\\.[A-Za-z0-9-_.]+")) jjg@1397: results.add(v); jjg@1397: } jjg@1397: } jjg@1397: } catch (ConstantPoolException ignore) { jjg@1397: } finally { jjg@1397: in.close(); jjg@1397: } jjg@1397: } jjg@1397: jjg@1397: /** jjg@1397: * Get the set of keys from the javadoc resource bundles. jjg@1397: */ jjg@1397: Set getResourceKeys() { jjg@1397: String[] names = { jjg@1397: "com.sun.tools.doclets.formats.html.resources.standard", jjg@1397: "com.sun.tools.doclets.internal.toolkit.resources.doclets", jjg@1397: "com.sun.tools.javadoc.resources.javadoc", jjg@1397: }; jjg@1397: Set results = new TreeSet(); jjg@1397: for (String name : names) { jjg@1397: ResourceBundle b = ResourceBundle.getBundle(name); jjg@1397: results.addAll(b.keySet()); jjg@1397: } jjg@1397: return results; jjg@1397: } jjg@1397: jjg@1397: /** jjg@1397: * Report an error. jjg@1397: */ jjg@1397: void error(String msg) { jjg@1397: System.err.println("Error: " + msg); jjg@1397: errors++; jjg@1397: } jjg@1397: jjg@1397: int errors; jjg@1397: }