Path: blob/aarch64-shenandoah-jdk8u272-b10/langtools/test/tools/javadoc/CheckResourceKeys.java
32285 views
/*1* Copyright (c) 2010, 2012, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 800061226* @summary need test program to validate javadoc resource bundles27*/2829import java.io.*;30import java.util.*;31import javax.tools.*;32import com.sun.tools.classfile.*;3334/**35* Compare string constants in javadoc classes against keys in javadoc resource bundles.36*/37public class CheckResourceKeys {38/**39* Main program.40* Options:41* -finddeadkeys42* look for keys in resource bundles that are no longer required43* -findmissingkeys44* look for keys in resource bundles that are missing45*46* @throws Exception if invoked by jtreg and errors occur47*/48public static void main(String... args) throws Exception {49CheckResourceKeys c = new CheckResourceKeys();50if (c.run(args))51return;5253if (is_jtreg())54throw new Exception(c.errors + " errors occurred");55else56System.exit(1);57}5859static boolean is_jtreg() {60return (System.getProperty("test.src") != null);61}6263/**64* Main entry point.65*/66boolean run(String... args) throws Exception {67boolean findDeadKeys = false;68boolean findMissingKeys = false;6970if (args.length == 0) {71if (is_jtreg()) {72findDeadKeys = true;73findMissingKeys = true;74} else {75System.err.println("Usage: java CheckResourceKeys <options>");76System.err.println("where options include");77System.err.println(" -finddeadkeys find keys in resource bundles which are no longer required");78System.err.println(" -findmissingkeys find keys in resource bundles that are required but missing");79return true;80}81} else {82for (String arg: args) {83if (arg.equalsIgnoreCase("-finddeadkeys"))84findDeadKeys = true;85else if (arg.equalsIgnoreCase("-findmissingkeys"))86findMissingKeys = true;87else88error("bad option: " + arg);89}90}9192if (errors > 0)93return false;9495Set<String> codeKeys = getCodeKeys();96Set<String> resourceKeys = getResourceKeys();9798System.err.println("found " + codeKeys.size() + " keys in code");99System.err.println("found " + resourceKeys.size() + " keys in resource bundles");100101if (findDeadKeys)102findDeadKeys(codeKeys, resourceKeys);103104if (findMissingKeys)105findMissingKeys(codeKeys, resourceKeys);106107return (errors == 0);108}109110/**111* Find keys in resource bundles which are probably no longer required.112* A key is required if there is a string in the code that is a resource key,113* or if the key is well-known according to various pragmatic rules.114*/115void findDeadKeys(Set<String> codeKeys, Set<String> resourceKeys) {116for (String rk: resourceKeys) {117if (codeKeys.contains(rk))118continue;119120error("Resource key not found in code: " + rk);121}122}123124/**125* For all strings in the code that look like they might be126* a resource key, verify that a key exists.127*/128void findMissingKeys(Set<String> codeKeys, Set<String> resourceKeys) {129for (String ck: codeKeys) {130if (resourceKeys.contains(ck))131continue;132error("No resource for \"" + ck + "\"");133}134}135136/**137* Get the set of strings from (most of) the javadoc classfiles.138*/139Set<String> getCodeKeys() throws IOException {140Set<String> results = new TreeSet<String>();141JavaCompiler c = ToolProvider.getSystemJavaCompiler();142JavaFileManager fm = c.getStandardFileManager(null, null, null);143JavaFileManager.Location javadocLoc = findJavadocLocation(fm);144String[] pkgs = {145"com.sun.tools.doclets",146"com.sun.tools.javadoc"147};148for (String pkg: pkgs) {149for (JavaFileObject fo: fm.list(javadocLoc,150pkg, EnumSet.of(JavaFileObject.Kind.CLASS), true)) {151String name = fo.getName();152// ignore resource files153if (name.matches(".*resources.[A-Za-z_0-9]+\\.class.*"))154continue;155scan(fo, results);156}157}158159// special handling for code strings synthesized in160// com.sun.tools.doclets.internal.toolkit.util.Util.getTypeName161String[] extras = {162"AnnotationType", "Class", "Enum", "Error", "Exception", "Interface"163};164for (String s: extras) {165if (results.contains("doclet." + s))166results.add("doclet." + s.toLowerCase());167}168169// special handling for code strings synthesized in170// com.sun.tools.javadoc.Messager171results.add("javadoc.error.msg");172results.add("javadoc.note.msg");173results.add("javadoc.note.pos.msg");174results.add("javadoc.warning.msg");175176return results;177}178179// depending on how the test is run, javadoc may be on bootclasspath or classpath180JavaFileManager.Location findJavadocLocation(JavaFileManager fm) {181JavaFileManager.Location[] locns =182{ StandardLocation.PLATFORM_CLASS_PATH, StandardLocation.CLASS_PATH };183try {184for (JavaFileManager.Location l: locns) {185JavaFileObject fo = fm.getJavaFileForInput(l,186"com.sun.tools.javadoc.Main", JavaFileObject.Kind.CLASS);187if (fo != null) {188System.err.println("found javadoc in " + l);189return l;190}191}192} catch (IOException e) {193throw new Error(e);194}195throw new IllegalStateException("Cannot find javadoc");196}197198/**199* Get the set of strings from a class file.200* Only strings that look like they might be a resource key are returned.201*/202void scan(JavaFileObject fo, Set<String> results) throws IOException {203//System.err.println("scan " + fo.getName());204InputStream in = fo.openInputStream();205try {206ClassFile cf = ClassFile.read(in);207for (ConstantPool.CPInfo cpinfo: cf.constant_pool.entries()) {208if (cpinfo.getTag() == ConstantPool.CONSTANT_Utf8) {209String v = ((ConstantPool.CONSTANT_Utf8_info) cpinfo).value;210if (v.matches("(doclet|main|javadoc|tag)\\.[A-Za-z0-9-_.]+"))211results.add(v);212}213}214} catch (ConstantPoolException ignore) {215} finally {216in.close();217}218}219220/**221* Get the set of keys from the javadoc resource bundles.222*/223Set<String> getResourceKeys() {224String[] names = {225"com.sun.tools.doclets.formats.html.resources.standard",226"com.sun.tools.doclets.internal.toolkit.resources.doclets",227"com.sun.tools.javadoc.resources.javadoc",228};229Set<String> results = new TreeSet<String>();230for (String name : names) {231ResourceBundle b = ResourceBundle.getBundle(name);232results.addAll(b.keySet());233}234return results;235}236237/**238* Report an error.239*/240void error(String msg) {241System.err.println("Error: " + msg);242errors++;243}244245int errors;246}247248249