|
1 /* |
|
2 * Copyright (c) 2010, 2012, 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 8000612 |
|
27 * @summary need test program to validate javadoc resource bundles |
|
28 */ |
|
29 |
|
30 import java.io.*; |
|
31 import java.util.*; |
|
32 import javax.tools.*; |
|
33 import com.sun.tools.classfile.*; |
|
34 |
|
35 /** |
|
36 * Compare string constants in javadoc classes against keys in javadoc resource bundles. |
|
37 */ |
|
38 public class CheckResourceKeys { |
|
39 /** |
|
40 * Main program. |
|
41 * Options: |
|
42 * -finddeadkeys |
|
43 * look for keys in resource bundles that are no longer required |
|
44 * -findmissingkeys |
|
45 * look for keys in resource bundles that are missing |
|
46 * |
|
47 * @throws Exception if invoked by jtreg and errors occur |
|
48 */ |
|
49 public static void main(String... args) throws Exception { |
|
50 CheckResourceKeys c = new CheckResourceKeys(); |
|
51 if (c.run(args)) |
|
52 return; |
|
53 |
|
54 if (is_jtreg()) |
|
55 throw new Exception(c.errors + " errors occurred"); |
|
56 else |
|
57 System.exit(1); |
|
58 } |
|
59 |
|
60 static boolean is_jtreg() { |
|
61 return (System.getProperty("test.src") != null); |
|
62 } |
|
63 |
|
64 /** |
|
65 * Main entry point. |
|
66 */ |
|
67 boolean run(String... args) throws Exception { |
|
68 boolean findDeadKeys = false; |
|
69 boolean findMissingKeys = false; |
|
70 |
|
71 if (args.length == 0) { |
|
72 if (is_jtreg()) { |
|
73 findDeadKeys = true; |
|
74 findMissingKeys = true; |
|
75 } else { |
|
76 System.err.println("Usage: java CheckResourceKeys <options>"); |
|
77 System.err.println("where options include"); |
|
78 System.err.println(" -finddeadkeys find keys in resource bundles which are no longer required"); |
|
79 System.err.println(" -findmissingkeys find keys in resource bundles that are required but missing"); |
|
80 return true; |
|
81 } |
|
82 } else { |
|
83 for (String arg: args) { |
|
84 if (arg.equalsIgnoreCase("-finddeadkeys")) |
|
85 findDeadKeys = true; |
|
86 else if (arg.equalsIgnoreCase("-findmissingkeys")) |
|
87 findMissingKeys = true; |
|
88 else |
|
89 error("bad option: " + arg); |
|
90 } |
|
91 } |
|
92 |
|
93 if (errors > 0) |
|
94 return false; |
|
95 |
|
96 Set<String> codeKeys = getCodeKeys(); |
|
97 Set<String> resourceKeys = getResourceKeys(); |
|
98 |
|
99 System.err.println("found " + codeKeys.size() + " keys in code"); |
|
100 System.err.println("found " + resourceKeys.size() + " keys in resource bundles"); |
|
101 |
|
102 if (findDeadKeys) |
|
103 findDeadKeys(codeKeys, resourceKeys); |
|
104 |
|
105 if (findMissingKeys) |
|
106 findMissingKeys(codeKeys, resourceKeys); |
|
107 |
|
108 return (errors == 0); |
|
109 } |
|
110 |
|
111 /** |
|
112 * Find keys in resource bundles which are probably no longer required. |
|
113 * A key is required if there is a string in the code that is a resource key, |
|
114 * or if the key is well-known according to various pragmatic rules. |
|
115 */ |
|
116 void findDeadKeys(Set<String> codeKeys, Set<String> resourceKeys) { |
|
117 for (String rk: resourceKeys) { |
|
118 if (codeKeys.contains(rk)) |
|
119 continue; |
|
120 |
|
121 error("Resource key not found in code: " + rk); |
|
122 } |
|
123 } |
|
124 |
|
125 /** |
|
126 * For all strings in the code that look like they might be |
|
127 * a resource key, verify that a key exists. |
|
128 */ |
|
129 void findMissingKeys(Set<String> codeKeys, Set<String> resourceKeys) { |
|
130 for (String ck: codeKeys) { |
|
131 if (resourceKeys.contains(ck)) |
|
132 continue; |
|
133 error("No resource for \"" + ck + "\""); |
|
134 } |
|
135 } |
|
136 |
|
137 /** |
|
138 * Get the set of strings from (most of) the javadoc classfiles. |
|
139 */ |
|
140 Set<String> getCodeKeys() throws IOException { |
|
141 Set<String> results = new TreeSet<String>(); |
|
142 JavaCompiler c = ToolProvider.getSystemJavaCompiler(); |
|
143 JavaFileManager fm = c.getStandardFileManager(null, null, null); |
|
144 JavaFileManager.Location javadocLoc = findJavadocLocation(fm); |
|
145 String[] pkgs = { |
|
146 "com.sun.tools.doclets", |
|
147 "com.sun.tools.javadoc" |
|
148 }; |
|
149 for (String pkg: pkgs) { |
|
150 for (JavaFileObject fo: fm.list(javadocLoc, |
|
151 pkg, EnumSet.of(JavaFileObject.Kind.CLASS), true)) { |
|
152 String name = fo.getName(); |
|
153 // ignore resource files |
|
154 if (name.matches(".*resources.[A-Za-z_0-9]+\\.class")) |
|
155 continue; |
|
156 scan(fo, results); |
|
157 } |
|
158 } |
|
159 |
|
160 // special handling for code strings synthesized in |
|
161 // com.sun.tools.doclets.internal.toolkit.util.Util.getTypeName |
|
162 String[] extras = { |
|
163 "AnnotationType", "Class", "Enum", "Error", "Exception", "Interface" |
|
164 }; |
|
165 for (String s: extras) { |
|
166 if (results.contains("doclet." + s)) |
|
167 results.add("doclet." + s.toLowerCase()); |
|
168 } |
|
169 |
|
170 return results; |
|
171 } |
|
172 |
|
173 // depending on how the test is run, javadoc may be on bootclasspath or classpath |
|
174 JavaFileManager.Location findJavadocLocation(JavaFileManager fm) { |
|
175 JavaFileManager.Location[] locns = |
|
176 { StandardLocation.PLATFORM_CLASS_PATH, StandardLocation.CLASS_PATH }; |
|
177 try { |
|
178 for (JavaFileManager.Location l: locns) { |
|
179 JavaFileObject fo = fm.getJavaFileForInput(l, |
|
180 "com.sun.tools.javadoc.Main", JavaFileObject.Kind.CLASS); |
|
181 if (fo != null) { |
|
182 System.err.println("found javadoc in " + l); |
|
183 return l; |
|
184 } |
|
185 } |
|
186 } catch (IOException e) { |
|
187 throw new Error(e); |
|
188 } |
|
189 throw new IllegalStateException("Cannot find javadoc"); |
|
190 } |
|
191 |
|
192 /** |
|
193 * Get the set of strings from a class file. |
|
194 * Only strings that look like they might be a resource key are returned. |
|
195 */ |
|
196 void scan(JavaFileObject fo, Set<String> results) throws IOException { |
|
197 //System.err.println("scan " + fo.getName()); |
|
198 InputStream in = fo.openInputStream(); |
|
199 try { |
|
200 ClassFile cf = ClassFile.read(in); |
|
201 for (ConstantPool.CPInfo cpinfo: cf.constant_pool.entries()) { |
|
202 if (cpinfo.getTag() == ConstantPool.CONSTANT_Utf8) { |
|
203 String v = ((ConstantPool.CONSTANT_Utf8_info) cpinfo).value; |
|
204 if (v.matches("(doclet|main|javadoc|tag)\\.[A-Za-z0-9-_.]+")) |
|
205 results.add(v); |
|
206 } |
|
207 } |
|
208 } catch (ConstantPoolException ignore) { |
|
209 } finally { |
|
210 in.close(); |
|
211 } |
|
212 } |
|
213 |
|
214 /** |
|
215 * Get the set of keys from the javadoc resource bundles. |
|
216 */ |
|
217 Set<String> getResourceKeys() { |
|
218 String[] names = { |
|
219 "com.sun.tools.doclets.formats.html.resources.standard", |
|
220 "com.sun.tools.doclets.internal.toolkit.resources.doclets", |
|
221 "com.sun.tools.javadoc.resources.javadoc", |
|
222 }; |
|
223 Set<String> results = new TreeSet<String>(); |
|
224 for (String name : names) { |
|
225 ResourceBundle b = ResourceBundle.getBundle(name); |
|
226 results.addAll(b.keySet()); |
|
227 } |
|
228 return results; |
|
229 } |
|
230 |
|
231 /** |
|
232 * Report an error. |
|
233 */ |
|
234 void error(String msg) { |
|
235 System.err.println("Error: " + msg); |
|
236 errors++; |
|
237 } |
|
238 |
|
239 int errors; |
|
240 } |