author | briangoetz |
Wed, 18 Dec 2013 16:05:18 -0500 | |
changeset 22163 | 3651128c74eb |
parent 19921 | 58bcbe156057 |
child 23116 | 4c134ccd6b8d |
permissions | -rw-r--r-- |
10 | 1 |
/* |
19921
58bcbe156057
8023974: Drop 'implements Completer' and 'implements SourceCompleter' from ClassReader resp. JavaCompiler.
jfranck
parents:
14545
diff
changeset
|
2 |
* Copyright (c) 2001, 2013, Oracle and/or its affiliates. All rights reserved. |
10 | 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 |
|
5520 | 7 |
* published by the Free Software Foundation. Oracle designates this |
10 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5520 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
10 | 10 |
* |
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
5520 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
10 | 24 |
*/ |
25 |
||
26 |
package com.sun.tools.javadoc; |
|
27 |
||
1869 | 28 |
import java.io.File; |
29 |
import java.io.IOException; |
|
10 | 30 |
import java.util.Collection; |
1869 | 31 |
import java.util.EnumSet; |
32 |
import java.util.HashMap; |
|
33 |
import java.util.Map; |
|
34 |
import java.util.Set; |
|
35 |
import javax.tools.JavaFileManager.Location; |
|
36 |
import javax.tools.JavaFileObject; |
|
37 |
import javax.tools.StandardJavaFileManager; |
|
38 |
import javax.tools.StandardLocation; |
|
10 | 39 |
|
1869 | 40 |
import com.sun.tools.javac.code.Symbol.CompletionFailure; |
41 |
import com.sun.tools.javac.tree.JCTree; |
|
42 |
import com.sun.tools.javac.tree.JCTree.JCClassDecl; |
|
43 |
import com.sun.tools.javac.tree.JCTree.JCCompilationUnit; |
|
44 |
import com.sun.tools.javac.util.Abort; |
|
45 |
import com.sun.tools.javac.util.Context; |
|
46 |
import com.sun.tools.javac.util.List; |
|
47 |
import com.sun.tools.javac.util.ListBuffer; |
|
48 |
import com.sun.tools.javac.util.Position; |
|
10 | 49 |
|
50 |
||
51 |
/** |
|
52 |
* This class could be the main entry point for Javadoc when Javadoc is used as a |
|
53 |
* component in a larger software system. It provides operations to |
|
54 |
* construct a new javadoc processor, and to run it on a set of source |
|
55 |
* files. |
|
14260
727a84636f12
8000665: fix "internal API" comments on javadoc files
jjg
parents:
14259
diff
changeset
|
56 |
* |
727a84636f12
8000665: fix "internal API" comments on javadoc files
jjg
parents:
14259
diff
changeset
|
57 |
* <p><b>This is NOT part of any supported API. |
727a84636f12
8000665: fix "internal API" comments on javadoc files
jjg
parents:
14259
diff
changeset
|
58 |
* If you write code that depends on this, you do so at your own risk. |
727a84636f12
8000665: fix "internal API" comments on javadoc files
jjg
parents:
14259
diff
changeset
|
59 |
* This code and its internal interfaces are subject to change or |
727a84636f12
8000665: fix "internal API" comments on javadoc files
jjg
parents:
14259
diff
changeset
|
60 |
* deletion without notice.</b> |
727a84636f12
8000665: fix "internal API" comments on javadoc files
jjg
parents:
14259
diff
changeset
|
61 |
* |
10 | 62 |
* @author Neal Gafter |
63 |
*/ |
|
64 |
public class JavadocTool extends com.sun.tools.javac.main.JavaCompiler { |
|
65 |
DocEnv docenv; |
|
66 |
||
67 |
final Messager messager; |
|
14543 | 68 |
final JavadocClassReader javadocReader; |
69 |
final JavadocEnter javadocEnter; |
|
10 | 70 |
|
71 |
/** |
|
72 |
* Construct a new JavaCompiler processor, using appropriately |
|
73 |
* extended phases of the underlying compiler. |
|
74 |
*/ |
|
75 |
protected JavadocTool(Context context) { |
|
76 |
super(context); |
|
77 |
messager = Messager.instance0(context); |
|
14543 | 78 |
javadocReader = JavadocClassReader.instance0(context); |
79 |
javadocEnter = JavadocEnter.instance0(context); |
|
10 | 80 |
} |
81 |
||
82 |
/** |
|
83 |
* For javadoc, the parser needs to keep comments. Overrides method from JavaCompiler. |
|
84 |
*/ |
|
85 |
protected boolean keepComments() { |
|
86 |
return true; |
|
87 |
} |
|
88 |
||
89 |
/** |
|
90 |
* Construct a new javadoc tool. |
|
91 |
*/ |
|
92 |
public static JavadocTool make0(Context context) { |
|
93 |
Messager messager = null; |
|
94 |
try { |
|
95 |
// force the use of Javadoc's class reader |
|
96 |
JavadocClassReader.preRegister(context); |
|
97 |
||
98 |
// force the use of Javadoc's own enter phase |
|
99 |
JavadocEnter.preRegister(context); |
|
100 |
||
101 |
// force the use of Javadoc's own member enter phase |
|
102 |
JavadocMemberEnter.preRegister(context); |
|
103 |
||
104 |
// force the use of Javadoc's own todo phase |
|
105 |
JavadocTodo.preRegister(context); |
|
106 |
||
107 |
// force the use of Messager as a Log |
|
108 |
messager = Messager.instance0(context); |
|
109 |
||
110 |
return new JavadocTool(context); |
|
111 |
} catch (CompletionFailure ex) { |
|
112 |
messager.error(Position.NOPOS, ex.getMessage()); |
|
113 |
return null; |
|
114 |
} |
|
115 |
} |
|
116 |
||
117 |
public RootDocImpl getRootDocImpl(String doclocale, |
|
118 |
String encoding, |
|
119 |
ModifierFilter filter, |
|
120 |
List<String> javaNames, |
|
121 |
List<String[]> options, |
|
14545
2e7bab0639b8
6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents:
14543
diff
changeset
|
122 |
Iterable<? extends JavaFileObject> fileObjects, |
10 | 123 |
boolean breakiterator, |
124 |
List<String> subPackages, |
|
125 |
List<String> excludedPackages, |
|
126 |
boolean docClasses, |
|
127 |
boolean legacyDoclet, |
|
128 |
boolean quiet) throws IOException { |
|
129 |
docenv = DocEnv.instance(context); |
|
130 |
docenv.showAccess = filter; |
|
1869 | 131 |
docenv.quiet = quiet; |
10 | 132 |
docenv.breakiterator = breakiterator; |
133 |
docenv.setLocale(doclocale); |
|
134 |
docenv.setEncoding(encoding); |
|
135 |
docenv.docClasses = docClasses; |
|
136 |
docenv.legacyDoclet = legacyDoclet; |
|
19921
58bcbe156057
8023974: Drop 'implements Completer' and 'implements SourceCompleter' from ClassReader resp. JavaCompiler.
jfranck
parents:
14545
diff
changeset
|
137 |
javadocReader.sourceCompleter = docClasses ? null : thisCompleter; |
10 | 138 |
|
22163 | 139 |
ListBuffer<String> names = new ListBuffer<>(); |
140 |
ListBuffer<JCCompilationUnit> classTrees = new ListBuffer<>(); |
|
141 |
ListBuffer<JCCompilationUnit> packTrees = new ListBuffer<>(); |
|
10 | 142 |
|
143 |
try { |
|
14545
2e7bab0639b8
6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents:
14543
diff
changeset
|
144 |
StandardJavaFileManager fm = docenv.fileManager instanceof StandardJavaFileManager |
2e7bab0639b8
6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents:
14543
diff
changeset
|
145 |
? (StandardJavaFileManager) docenv.fileManager : null; |
10 | 146 |
for (List<String> it = javaNames; it.nonEmpty(); it = it.tail) { |
147 |
String name = it.head; |
|
14545
2e7bab0639b8
6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents:
14543
diff
changeset
|
148 |
if (!docClasses && fm != null && name.endsWith(".java") && new File(name).exists()) { |
1869 | 149 |
JavaFileObject fo = fm.getJavaFileObjects(name).iterator().next(); |
10 | 150 |
docenv.notice("main.Loading_source_file", name); |
1869 | 151 |
JCCompilationUnit tree = parse(fo); |
152 |
classTrees.append(tree); |
|
10 | 153 |
} else if (isValidPackageName(name)) { |
154 |
names = names.append(name); |
|
155 |
} else if (name.endsWith(".java")) { |
|
14545
2e7bab0639b8
6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents:
14543
diff
changeset
|
156 |
if (fm == null) |
2e7bab0639b8
6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents:
14543
diff
changeset
|
157 |
throw new IllegalArgumentException(); |
2e7bab0639b8
6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents:
14543
diff
changeset
|
158 |
else |
14543 | 159 |
docenv.error(null, "main.file_not_found", name); |
10 | 160 |
} else { |
161 |
docenv.error(null, "main.illegal_package_name", name); |
|
162 |
} |
|
163 |
} |
|
14545
2e7bab0639b8
6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents:
14543
diff
changeset
|
164 |
for (JavaFileObject fo: fileObjects) { |
2e7bab0639b8
6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents:
14543
diff
changeset
|
165 |
docenv.notice("main.Loading_source_file", fo.getName()); |
2e7bab0639b8
6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents:
14543
diff
changeset
|
166 |
JCCompilationUnit tree = parse(fo); |
2e7bab0639b8
6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents:
14543
diff
changeset
|
167 |
classTrees.append(tree); |
2e7bab0639b8
6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents:
14543
diff
changeset
|
168 |
} |
10 | 169 |
|
170 |
if (!docClasses) { |
|
171 |
// Recursively search given subpackages. If any packages |
|
172 |
//are found, add them to the list. |
|
1869 | 173 |
Map<String,List<JavaFileObject>> packageFiles = |
174 |
searchSubPackages(subPackages, names, excludedPackages); |
|
10 | 175 |
|
176 |
// Parse the packages |
|
177 |
for (List<String> packs = names.toList(); packs.nonEmpty(); packs = packs.tail) { |
|
178 |
// Parse sources ostensibly belonging to package. |
|
1869 | 179 |
String packageName = packs.head; |
180 |
parsePackageClasses(packageName, packageFiles.get(packageName), packTrees, excludedPackages); |
|
10 | 181 |
} |
182 |
||
183 |
if (messager.nerrors() != 0) return null; |
|
184 |
||
185 |
// Enter symbols for all files |
|
186 |
docenv.notice("main.Building_tree"); |
|
14543 | 187 |
javadocEnter.main(classTrees.toList().appendList(packTrees.toList())); |
10 | 188 |
} |
189 |
} catch (Abort ex) {} |
|
190 |
||
1869 | 191 |
if (messager.nerrors() != 0) |
192 |
return null; |
|
10 | 193 |
|
194 |
if (docClasses) |
|
195 |
return new RootDocImpl(docenv, javaNames, options); |
|
196 |
else |
|
197 |
return new RootDocImpl(docenv, listClasses(classTrees.toList()), names.toList(), options); |
|
198 |
} |
|
199 |
||
200 |
/** Is the given string a valid package name? */ |
|
201 |
boolean isValidPackageName(String s) { |
|
202 |
int index; |
|
203 |
while ((index = s.indexOf('.')) != -1) { |
|
204 |
if (!isValidClassName(s.substring(0, index))) return false; |
|
205 |
s = s.substring(index+1); |
|
206 |
} |
|
207 |
return isValidClassName(s); |
|
208 |
} |
|
209 |
||
210 |
/** |
|
211 |
* search all directories in path for subdirectory name. Add all |
|
212 |
* .java files found in such a directory to args. |
|
213 |
*/ |
|
214 |
private void parsePackageClasses(String name, |
|
1869 | 215 |
Iterable<JavaFileObject> files, |
216 |
ListBuffer<JCCompilationUnit> trees, |
|
217 |
List<String> excludedPackages) |
|
218 |
throws IOException { |
|
10 | 219 |
if (excludedPackages.contains(name)) { |
220 |
return; |
|
221 |
} |
|
1869 | 222 |
|
10 | 223 |
boolean hasFiles = false; |
224 |
docenv.notice("main.Loading_source_files_for_package", name); |
|
1869 | 225 |
|
226 |
if (files == null) { |
|
227 |
Location location = docenv.fileManager.hasLocation(StandardLocation.SOURCE_PATH) |
|
228 |
? StandardLocation.SOURCE_PATH : StandardLocation.CLASS_PATH; |
|
22163 | 229 |
ListBuffer<JavaFileObject> lb = new ListBuffer<>(); |
1869 | 230 |
for (JavaFileObject fo: docenv.fileManager.list( |
231 |
location, name, EnumSet.of(JavaFileObject.Kind.SOURCE), false)) { |
|
232 |
String binaryName = docenv.fileManager.inferBinaryName(location, fo); |
|
233 |
String simpleName = getSimpleName(binaryName); |
|
234 |
if (isValidClassName(simpleName)) { |
|
235 |
lb.append(fo); |
|
10 | 236 |
} |
237 |
} |
|
1869 | 238 |
files = lb.toList(); |
10 | 239 |
} |
1869 | 240 |
|
241 |
for (JavaFileObject fo : files) { |
|
242 |
// messager.notice("main.Loading_source_file", fn); |
|
243 |
trees.append(parse(fo)); |
|
244 |
hasFiles = true; |
|
245 |
} |
|
246 |
||
247 |
if (!hasFiles) { |
|
14543 | 248 |
messager.warning(Messager.NOPOS, "main.no_source_files_for_package", |
1869 | 249 |
name.replace(File.separatorChar, '.')); |
250 |
} |
|
10 | 251 |
} |
252 |
||
253 |
/** |
|
254 |
* Recursively search all directories in path for subdirectory name. |
|
255 |
* Add all packages found in such a directory to packages list. |
|
256 |
*/ |
|
1869 | 257 |
private Map<String,List<JavaFileObject>> searchSubPackages( |
258 |
List<String> subPackages, |
|
259 |
ListBuffer<String> packages, |
|
260 |
List<String> excludedPackages) |
|
261 |
throws IOException { |
|
22163 | 262 |
Map<String,List<JavaFileObject>> packageFiles = new HashMap<>(); |
1869 | 263 |
|
22163 | 264 |
Map<String,Boolean> includedPackages = new HashMap<>(); |
1869 | 265 |
includedPackages.put("", true); |
266 |
for (String p: excludedPackages) |
|
267 |
includedPackages.put(p, false); |
|
268 |
||
10635 | 269 |
StandardLocation path = docenv.fileManager.hasLocation(StandardLocation.SOURCE_PATH) |
270 |
? StandardLocation.SOURCE_PATH : StandardLocation.CLASS_PATH; |
|
271 |
||
272 |
searchSubPackages(subPackages, |
|
273 |
includedPackages, |
|
274 |
packages, packageFiles, |
|
275 |
path, |
|
276 |
EnumSet.of(JavaFileObject.Kind.SOURCE)); |
|
277 |
||
1869 | 278 |
return packageFiles; |
279 |
} |
|
280 |
||
10 | 281 |
private void searchSubPackages(List<String> subPackages, |
1869 | 282 |
Map<String,Boolean> includedPackages, |
283 |
ListBuffer<String> packages, |
|
284 |
Map<String, List<JavaFileObject>> packageFiles, |
|
285 |
StandardLocation location, Set<JavaFileObject.Kind> kinds) |
|
286 |
throws IOException { |
|
287 |
for (String subPackage: subPackages) { |
|
288 |
if (!isIncluded(subPackage, includedPackages)) |
|
289 |
continue; |
|
10 | 290 |
|
1869 | 291 |
for (JavaFileObject fo: docenv.fileManager.list(location, subPackage, kinds, true)) { |
292 |
String binaryName = docenv.fileManager.inferBinaryName(location, fo); |
|
293 |
String packageName = getPackageName(binaryName); |
|
294 |
String simpleName = getSimpleName(binaryName); |
|
295 |
if (isIncluded(packageName, includedPackages) && isValidClassName(simpleName)) { |
|
296 |
List<JavaFileObject> list = packageFiles.get(packageName); |
|
297 |
list = (list == null ? List.of(fo) : list.prepend(fo)); |
|
298 |
packageFiles.put(packageName, list); |
|
299 |
if (!packages.contains(packageName)) |
|
300 |
packages.add(packageName); |
|
301 |
} |
|
302 |
} |
|
303 |
} |
|
304 |
} |
|
305 |
||
306 |
private String getPackageName(String name) { |
|
307 |
int lastDot = name.lastIndexOf("."); |
|
308 |
return (lastDot == -1 ? "" : name.substring(0, lastDot)); |
|
309 |
} |
|
310 |
||
311 |
private String getSimpleName(String name) { |
|
312 |
int lastDot = name.lastIndexOf("."); |
|
313 |
return (lastDot == -1 ? name : name.substring(lastDot + 1)); |
|
314 |
} |
|
315 |
||
316 |
private boolean isIncluded(String packageName, Map<String,Boolean> includedPackages) { |
|
317 |
Boolean b = includedPackages.get(packageName); |
|
318 |
if (b == null) { |
|
319 |
b = isIncluded(getPackageName(packageName), includedPackages); |
|
320 |
includedPackages.put(packageName, b); |
|
321 |
} |
|
322 |
return b; |
|
10 | 323 |
} |
324 |
||
325 |
/** |
|
326 |
* Recursively search all directories in path for subdirectory name. |
|
327 |
* Add all packages found in such a directory to packages list. |
|
328 |
*/ |
|
329 |
private void searchSubPackage(String packageName, |
|
330 |
ListBuffer<String> packages, |
|
331 |
List<String> excludedPackages, |
|
332 |
Collection<File> pathnames) { |
|
333 |
if (excludedPackages.contains(packageName)) |
|
334 |
return; |
|
335 |
||
336 |
String packageFilename = packageName.replace('.', File.separatorChar); |
|
337 |
boolean addedPackage = false; |
|
338 |
for (File pathname : pathnames) { |
|
339 |
File f = new File(pathname, packageFilename); |
|
340 |
String filenames[] = f.list(); |
|
341 |
// if filenames not null, then found directory |
|
342 |
if (filenames != null) { |
|
343 |
for (String filename : filenames) { |
|
344 |
if (!addedPackage |
|
345 |
&& (isValidJavaSourceFile(filename) || |
|
346 |
isValidJavaClassFile(filename)) |
|
347 |
&& !packages.contains(packageName)) { |
|
348 |
packages.append(packageName); |
|
349 |
addedPackage = true; |
|
350 |
} else if (isValidClassName(filename) && |
|
351 |
(new File(f, filename)).isDirectory()) { |
|
352 |
searchSubPackage(packageName + "." + filename, |
|
353 |
packages, excludedPackages, pathnames); |
|
354 |
} |
|
355 |
} |
|
356 |
} |
|
357 |
} |
|
358 |
} |
|
359 |
||
360 |
/** |
|
361 |
* Return true if given file name is a valid class file name. |
|
362 |
* @param file the name of the file to check. |
|
363 |
* @return true if given file name is a valid class file name |
|
364 |
* and false otherwise. |
|
365 |
*/ |
|
366 |
private static boolean isValidJavaClassFile(String file) { |
|
367 |
if (!file.endsWith(".class")) return false; |
|
368 |
String clazzName = file.substring(0, file.length() - ".class".length()); |
|
369 |
return isValidClassName(clazzName); |
|
370 |
} |
|
371 |
||
372 |
/** |
|
373 |
* Return true if given file name is a valid Java source file name. |
|
374 |
* @param file the name of the file to check. |
|
375 |
* @return true if given file name is a valid Java source file name |
|
376 |
* and false otherwise. |
|
377 |
*/ |
|
378 |
private static boolean isValidJavaSourceFile(String file) { |
|
379 |
if (!file.endsWith(".java")) return false; |
|
380 |
String clazzName = file.substring(0, file.length() - ".java".length()); |
|
381 |
return isValidClassName(clazzName); |
|
382 |
} |
|
383 |
||
384 |
/** Are surrogates supported? |
|
385 |
*/ |
|
386 |
final static boolean surrogatesSupported = surrogatesSupported(); |
|
387 |
private static boolean surrogatesSupported() { |
|
388 |
try { |
|
389 |
boolean b = Character.isHighSurrogate('a'); |
|
390 |
return true; |
|
391 |
} catch (NoSuchMethodError ex) { |
|
392 |
return false; |
|
393 |
} |
|
394 |
} |
|
395 |
||
396 |
/** |
|
397 |
* Return true if given file name is a valid class name |
|
398 |
* (including "package-info"). |
|
14259 | 399 |
* @param s the name of the class to check. |
10 | 400 |
* @return true if given class name is a valid class name |
401 |
* and false otherwise. |
|
402 |
*/ |
|
403 |
public static boolean isValidClassName(String s) { |
|
404 |
if (s.length() < 1) return false; |
|
405 |
if (s.equals("package-info")) return true; |
|
406 |
if (surrogatesSupported) { |
|
407 |
int cp = s.codePointAt(0); |
|
408 |
if (!Character.isJavaIdentifierStart(cp)) |
|
409 |
return false; |
|
410 |
for (int j=Character.charCount(cp); j<s.length(); j+=Character.charCount(cp)) { |
|
411 |
cp = s.codePointAt(j); |
|
412 |
if (!Character.isJavaIdentifierPart(cp)) |
|
413 |
return false; |
|
414 |
} |
|
415 |
} else { |
|
416 |
if (!Character.isJavaIdentifierStart(s.charAt(0))) |
|
417 |
return false; |
|
418 |
for (int j=1; j<s.length(); j++) |
|
419 |
if (!Character.isJavaIdentifierPart(s.charAt(j))) |
|
420 |
return false; |
|
421 |
} |
|
422 |
return true; |
|
423 |
} |
|
424 |
||
425 |
/** |
|
426 |
* From a list of top level trees, return the list of contained class definitions |
|
427 |
*/ |
|
428 |
List<JCClassDecl> listClasses(List<JCCompilationUnit> trees) { |
|
22163 | 429 |
ListBuffer<JCClassDecl> result = new ListBuffer<>(); |
10 | 430 |
for (JCCompilationUnit t : trees) { |
431 |
for (JCTree def : t.defs) { |
|
10950 | 432 |
if (def.hasTag(JCTree.Tag.CLASSDEF)) |
10 | 433 |
result.append((JCClassDecl)def); |
434 |
} |
|
435 |
} |
|
436 |
return result.toList(); |
|
437 |
} |
|
438 |
||
439 |
} |