test/tools/javadoc/CheckResourceKeys.java

Thu, 31 Aug 2017 15:17:03 +0800

author
aoqi
date
Thu, 31 Aug 2017 15:17:03 +0800
changeset 2525
2eb010b6cb22
parent 1413
bdcef2ef52d2
parent 0
959103a6100f
permissions
-rw-r--r--

merge

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 2010, 2012, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation.
aoqi@0 8 *
aoqi@0 9 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 12 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 13 * accompanied this code).
aoqi@0 14 *
aoqi@0 15 * You should have received a copy of the GNU General Public License version
aoqi@0 16 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 18 *
aoqi@0 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 20 * or visit www.oracle.com if you need additional information or have any
aoqi@0 21 * questions.
aoqi@0 22 */
aoqi@0 23
aoqi@0 24 /*
aoqi@0 25 * @test
aoqi@0 26 * @bug 8000612
aoqi@0 27 * @summary need test program to validate javadoc resource bundles
aoqi@0 28 */
aoqi@0 29
aoqi@0 30 import java.io.*;
aoqi@0 31 import java.util.*;
aoqi@0 32 import javax.tools.*;
aoqi@0 33 import com.sun.tools.classfile.*;
aoqi@0 34
aoqi@0 35 /**
aoqi@0 36 * Compare string constants in javadoc classes against keys in javadoc resource bundles.
aoqi@0 37 */
aoqi@0 38 public class CheckResourceKeys {
aoqi@0 39 /**
aoqi@0 40 * Main program.
aoqi@0 41 * Options:
aoqi@0 42 * -finddeadkeys
aoqi@0 43 * look for keys in resource bundles that are no longer required
aoqi@0 44 * -findmissingkeys
aoqi@0 45 * look for keys in resource bundles that are missing
aoqi@0 46 *
aoqi@0 47 * @throws Exception if invoked by jtreg and errors occur
aoqi@0 48 */
aoqi@0 49 public static void main(String... args) throws Exception {
aoqi@0 50 CheckResourceKeys c = new CheckResourceKeys();
aoqi@0 51 if (c.run(args))
aoqi@0 52 return;
aoqi@0 53
aoqi@0 54 if (is_jtreg())
aoqi@0 55 throw new Exception(c.errors + " errors occurred");
aoqi@0 56 else
aoqi@0 57 System.exit(1);
aoqi@0 58 }
aoqi@0 59
aoqi@0 60 static boolean is_jtreg() {
aoqi@0 61 return (System.getProperty("test.src") != null);
aoqi@0 62 }
aoqi@0 63
aoqi@0 64 /**
aoqi@0 65 * Main entry point.
aoqi@0 66 */
aoqi@0 67 boolean run(String... args) throws Exception {
aoqi@0 68 boolean findDeadKeys = false;
aoqi@0 69 boolean findMissingKeys = false;
aoqi@0 70
aoqi@0 71 if (args.length == 0) {
aoqi@0 72 if (is_jtreg()) {
aoqi@0 73 findDeadKeys = true;
aoqi@0 74 findMissingKeys = true;
aoqi@0 75 } else {
aoqi@0 76 System.err.println("Usage: java CheckResourceKeys <options>");
aoqi@0 77 System.err.println("where options include");
aoqi@0 78 System.err.println(" -finddeadkeys find keys in resource bundles which are no longer required");
aoqi@0 79 System.err.println(" -findmissingkeys find keys in resource bundles that are required but missing");
aoqi@0 80 return true;
aoqi@0 81 }
aoqi@0 82 } else {
aoqi@0 83 for (String arg: args) {
aoqi@0 84 if (arg.equalsIgnoreCase("-finddeadkeys"))
aoqi@0 85 findDeadKeys = true;
aoqi@0 86 else if (arg.equalsIgnoreCase("-findmissingkeys"))
aoqi@0 87 findMissingKeys = true;
aoqi@0 88 else
aoqi@0 89 error("bad option: " + arg);
aoqi@0 90 }
aoqi@0 91 }
aoqi@0 92
aoqi@0 93 if (errors > 0)
aoqi@0 94 return false;
aoqi@0 95
aoqi@0 96 Set<String> codeKeys = getCodeKeys();
aoqi@0 97 Set<String> resourceKeys = getResourceKeys();
aoqi@0 98
aoqi@0 99 System.err.println("found " + codeKeys.size() + " keys in code");
aoqi@0 100 System.err.println("found " + resourceKeys.size() + " keys in resource bundles");
aoqi@0 101
aoqi@0 102 if (findDeadKeys)
aoqi@0 103 findDeadKeys(codeKeys, resourceKeys);
aoqi@0 104
aoqi@0 105 if (findMissingKeys)
aoqi@0 106 findMissingKeys(codeKeys, resourceKeys);
aoqi@0 107
aoqi@0 108 return (errors == 0);
aoqi@0 109 }
aoqi@0 110
aoqi@0 111 /**
aoqi@0 112 * Find keys in resource bundles which are probably no longer required.
aoqi@0 113 * A key is required if there is a string in the code that is a resource key,
aoqi@0 114 * or if the key is well-known according to various pragmatic rules.
aoqi@0 115 */
aoqi@0 116 void findDeadKeys(Set<String> codeKeys, Set<String> resourceKeys) {
aoqi@0 117 for (String rk: resourceKeys) {
aoqi@0 118 if (codeKeys.contains(rk))
aoqi@0 119 continue;
aoqi@0 120
aoqi@0 121 error("Resource key not found in code: " + rk);
aoqi@0 122 }
aoqi@0 123 }
aoqi@0 124
aoqi@0 125 /**
aoqi@0 126 * For all strings in the code that look like they might be
aoqi@0 127 * a resource key, verify that a key exists.
aoqi@0 128 */
aoqi@0 129 void findMissingKeys(Set<String> codeKeys, Set<String> resourceKeys) {
aoqi@0 130 for (String ck: codeKeys) {
aoqi@0 131 if (resourceKeys.contains(ck))
aoqi@0 132 continue;
aoqi@0 133 error("No resource for \"" + ck + "\"");
aoqi@0 134 }
aoqi@0 135 }
aoqi@0 136
aoqi@0 137 /**
aoqi@0 138 * Get the set of strings from (most of) the javadoc classfiles.
aoqi@0 139 */
aoqi@0 140 Set<String> getCodeKeys() throws IOException {
aoqi@0 141 Set<String> results = new TreeSet<String>();
aoqi@0 142 JavaCompiler c = ToolProvider.getSystemJavaCompiler();
aoqi@0 143 JavaFileManager fm = c.getStandardFileManager(null, null, null);
aoqi@0 144 JavaFileManager.Location javadocLoc = findJavadocLocation(fm);
aoqi@0 145 String[] pkgs = {
aoqi@0 146 "com.sun.tools.doclets",
aoqi@0 147 "com.sun.tools.javadoc"
aoqi@0 148 };
aoqi@0 149 for (String pkg: pkgs) {
aoqi@0 150 for (JavaFileObject fo: fm.list(javadocLoc,
aoqi@0 151 pkg, EnumSet.of(JavaFileObject.Kind.CLASS), true)) {
aoqi@0 152 String name = fo.getName();
aoqi@0 153 // ignore resource files
aoqi@0 154 if (name.matches(".*resources.[A-Za-z_0-9]+\\.class.*"))
aoqi@0 155 continue;
aoqi@0 156 scan(fo, results);
aoqi@0 157 }
aoqi@0 158 }
aoqi@0 159
aoqi@0 160 // special handling for code strings synthesized in
aoqi@0 161 // com.sun.tools.doclets.internal.toolkit.util.Util.getTypeName
aoqi@0 162 String[] extras = {
aoqi@0 163 "AnnotationType", "Class", "Enum", "Error", "Exception", "Interface"
aoqi@0 164 };
aoqi@0 165 for (String s: extras) {
aoqi@0 166 if (results.contains("doclet." + s))
aoqi@0 167 results.add("doclet." + s.toLowerCase());
aoqi@0 168 }
aoqi@0 169
aoqi@0 170 // special handling for code strings synthesized in
aoqi@0 171 // com.sun.tools.javadoc.Messager
aoqi@0 172 results.add("javadoc.error.msg");
aoqi@0 173 results.add("javadoc.note.msg");
aoqi@0 174 results.add("javadoc.note.pos.msg");
aoqi@0 175 results.add("javadoc.warning.msg");
aoqi@0 176
aoqi@0 177 return results;
aoqi@0 178 }
aoqi@0 179
aoqi@0 180 // depending on how the test is run, javadoc may be on bootclasspath or classpath
aoqi@0 181 JavaFileManager.Location findJavadocLocation(JavaFileManager fm) {
aoqi@0 182 JavaFileManager.Location[] locns =
aoqi@0 183 { StandardLocation.PLATFORM_CLASS_PATH, StandardLocation.CLASS_PATH };
aoqi@0 184 try {
aoqi@0 185 for (JavaFileManager.Location l: locns) {
aoqi@0 186 JavaFileObject fo = fm.getJavaFileForInput(l,
aoqi@0 187 "com.sun.tools.javadoc.Main", JavaFileObject.Kind.CLASS);
aoqi@0 188 if (fo != null) {
aoqi@0 189 System.err.println("found javadoc in " + l);
aoqi@0 190 return l;
aoqi@0 191 }
aoqi@0 192 }
aoqi@0 193 } catch (IOException e) {
aoqi@0 194 throw new Error(e);
aoqi@0 195 }
aoqi@0 196 throw new IllegalStateException("Cannot find javadoc");
aoqi@0 197 }
aoqi@0 198
aoqi@0 199 /**
aoqi@0 200 * Get the set of strings from a class file.
aoqi@0 201 * Only strings that look like they might be a resource key are returned.
aoqi@0 202 */
aoqi@0 203 void scan(JavaFileObject fo, Set<String> results) throws IOException {
aoqi@0 204 //System.err.println("scan " + fo.getName());
aoqi@0 205 InputStream in = fo.openInputStream();
aoqi@0 206 try {
aoqi@0 207 ClassFile cf = ClassFile.read(in);
aoqi@0 208 for (ConstantPool.CPInfo cpinfo: cf.constant_pool.entries()) {
aoqi@0 209 if (cpinfo.getTag() == ConstantPool.CONSTANT_Utf8) {
aoqi@0 210 String v = ((ConstantPool.CONSTANT_Utf8_info) cpinfo).value;
aoqi@0 211 if (v.matches("(doclet|main|javadoc|tag)\\.[A-Za-z0-9-_.]+"))
aoqi@0 212 results.add(v);
aoqi@0 213 }
aoqi@0 214 }
aoqi@0 215 } catch (ConstantPoolException ignore) {
aoqi@0 216 } finally {
aoqi@0 217 in.close();
aoqi@0 218 }
aoqi@0 219 }
aoqi@0 220
aoqi@0 221 /**
aoqi@0 222 * Get the set of keys from the javadoc resource bundles.
aoqi@0 223 */
aoqi@0 224 Set<String> getResourceKeys() {
aoqi@0 225 String[] names = {
aoqi@0 226 "com.sun.tools.doclets.formats.html.resources.standard",
aoqi@0 227 "com.sun.tools.doclets.internal.toolkit.resources.doclets",
aoqi@0 228 "com.sun.tools.javadoc.resources.javadoc",
aoqi@0 229 };
aoqi@0 230 Set<String> results = new TreeSet<String>();
aoqi@0 231 for (String name : names) {
aoqi@0 232 ResourceBundle b = ResourceBundle.getBundle(name);
aoqi@0 233 results.addAll(b.keySet());
aoqi@0 234 }
aoqi@0 235 return results;
aoqi@0 236 }
aoqi@0 237
aoqi@0 238 /**
aoqi@0 239 * Report an error.
aoqi@0 240 */
aoqi@0 241 void error(String msg) {
aoqi@0 242 System.err.println("Error: " + msg);
aoqi@0 243 errors++;
aoqi@0 244 }
aoqi@0 245
aoqi@0 246 int errors;
aoqi@0 247 }

mercurial