author | erikj |
Tue, 12 Sep 2017 19:03:39 +0200 | |
changeset 47216 | 71c04702a3d5 |
parent 45417 | langtools/src/jdk.javadoc/share/classes/jdk/javadoc/internal/doclets/toolkit/AbstractDoclet.java@f7479ee8de69 |
child 47849 | 0e38db7cf1cc |
permissions | -rw-r--r-- |
10 | 1 |
/* |
45417 | 2 |
* Copyright (c) 2003, 2017, 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 |
||
35426
374342e56a56
8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents:
26269
diff
changeset
|
26 |
package jdk.javadoc.internal.doclets.toolkit; |
374342e56a56
8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents:
26269
diff
changeset
|
27 |
|
374342e56a56
8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents:
26269
diff
changeset
|
28 |
import java.util.SortedSet; |
374342e56a56
8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents:
26269
diff
changeset
|
29 |
import java.util.TreeSet; |
374342e56a56
8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents:
26269
diff
changeset
|
30 |
|
374342e56a56
8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents:
26269
diff
changeset
|
31 |
import javax.lang.model.SourceVersion; |
374342e56a56
8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents:
26269
diff
changeset
|
32 |
import javax.lang.model.element.PackageElement; |
374342e56a56
8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents:
26269
diff
changeset
|
33 |
import javax.lang.model.element.TypeElement; |
10 | 34 |
|
40315
9e994c77db6a
8160137: HTMLDoclet and AbstractDoclet should implement Doclet
jjg
parents:
40303
diff
changeset
|
35 |
import jdk.javadoc.doclet.Doclet; |
35426
374342e56a56
8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents:
26269
diff
changeset
|
36 |
import jdk.javadoc.doclet.DocletEnvironment; |
44189 | 37 |
import jdk.javadoc.doclet.StandardDoclet; |
40587 | 38 |
import jdk.javadoc.internal.doclets.formats.html.HtmlDoclet; |
35426
374342e56a56
8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents:
26269
diff
changeset
|
39 |
import jdk.javadoc.internal.doclets.toolkit.builders.AbstractBuilder; |
374342e56a56
8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents:
26269
diff
changeset
|
40 |
import jdk.javadoc.internal.doclets.toolkit.builders.BuilderFactory; |
374342e56a56
8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents:
26269
diff
changeset
|
41 |
import jdk.javadoc.internal.doclets.toolkit.util.ClassTree; |
40587 | 42 |
import jdk.javadoc.internal.doclets.toolkit.util.DocFileIOException; |
43261 | 43 |
import jdk.javadoc.internal.doclets.toolkit.util.UncheckedDocletException; |
40587 | 44 |
import jdk.javadoc.internal.doclets.toolkit.util.InternalException; |
35426
374342e56a56
8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents:
26269
diff
changeset
|
45 |
import jdk.javadoc.internal.doclets.toolkit.util.PackageListWriter; |
40587 | 46 |
import jdk.javadoc.internal.doclets.toolkit.util.ResourceIOException; |
47 |
import jdk.javadoc.internal.doclets.toolkit.util.SimpleDocletException; |
|
35426
374342e56a56
8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents:
26269
diff
changeset
|
48 |
import jdk.javadoc.internal.doclets.toolkit.util.Utils; |
374342e56a56
8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents:
26269
diff
changeset
|
49 |
|
374342e56a56
8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents:
26269
diff
changeset
|
50 |
import static javax.tools.Diagnostic.Kind.*; |
10 | 51 |
|
52 |
/** |
|
53 |
* An abstract implementation of a Doclet. |
|
54 |
* |
|
14260
727a84636f12
8000665: fix "internal API" comments on javadoc files
jjg
parents:
14258
diff
changeset
|
55 |
* <p><b>This is NOT part of any supported API. |
727a84636f12
8000665: fix "internal API" comments on javadoc files
jjg
parents:
14258
diff
changeset
|
56 |
* 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:
14258
diff
changeset
|
57 |
* This code and its internal interfaces are subject to change or |
727a84636f12
8000665: fix "internal API" comments on javadoc files
jjg
parents:
14258
diff
changeset
|
58 |
* deletion without notice.</b> |
10 | 59 |
* |
60 |
* @author Jamie Ho |
|
61 |
*/ |
|
40315
9e994c77db6a
8160137: HTMLDoclet and AbstractDoclet should implement Doclet
jjg
parents:
40303
diff
changeset
|
62 |
public abstract class AbstractDoclet implements Doclet { |
10 | 63 |
|
64 |
/** |
|
65 |
* The global configuration information for this run. |
|
66 |
*/ |
|
45417 | 67 |
private BaseConfiguration configuration; |
40303 | 68 |
|
69 |
protected Messages messages; |
|
70 |
||
25454
376a52c9540c
8039028: [javadoc] refactor the usage of Util.java
ksrini
parents:
22159
diff
changeset
|
71 |
/* |
376a52c9540c
8039028: [javadoc] refactor the usage of Util.java
ksrini
parents:
22159
diff
changeset
|
72 |
* a handle to our utility methods |
376a52c9540c
8039028: [javadoc] refactor the usage of Util.java
ksrini
parents:
22159
diff
changeset
|
73 |
*/ |
376a52c9540c
8039028: [javadoc] refactor the usage of Util.java
ksrini
parents:
22159
diff
changeset
|
74 |
protected Utils utils; |
10 | 75 |
|
76 |
/** |
|
77 |
* The only doclet that may use this toolkit is {@value} |
|
78 |
*/ |
|
14542 | 79 |
private static final String TOOLKIT_DOCLET_NAME = |
35426
374342e56a56
8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents:
26269
diff
changeset
|
80 |
jdk.javadoc.internal.doclets.formats.html.HtmlDoclet.class.getName(); |
10 | 81 |
|
82 |
/** |
|
83 |
* Verify that the only doclet that is using this toolkit is |
|
84 |
* {@value #TOOLKIT_DOCLET_NAME}. |
|
85 |
*/ |
|
26269
0d8efbb110b4
8054925: [javadoc] refactor the Doclet start method.
ksrini
parents:
25874
diff
changeset
|
86 |
private boolean isValidDoclet() { |
0d8efbb110b4
8054925: [javadoc] refactor the Doclet start method.
ksrini
parents:
25874
diff
changeset
|
87 |
if (!getClass().getName().equals(TOOLKIT_DOCLET_NAME)) { |
40303 | 88 |
messages.error("doclet.Toolkit_Usage_Violation", |
10 | 89 |
TOOLKIT_DOCLET_NAME); |
90 |
return false; |
|
91 |
} |
|
92 |
return true; |
|
93 |
} |
|
94 |
||
95 |
/** |
|
96 |
* The method that starts the execution of the doclet. |
|
97 |
* |
|
40508
74ef30d16fb9
8159305: Enhance the javadoc tool to support module related options
ksrini
parents:
40315
diff
changeset
|
98 |
* @param docEnv the {@link DocletEnvironment}. |
10 | 99 |
* @return true if the doclet executed without error. False otherwise. |
100 |
*/ |
|
40315
9e994c77db6a
8160137: HTMLDoclet and AbstractDoclet should implement Doclet
jjg
parents:
40303
diff
changeset
|
101 |
@Override |
40508
74ef30d16fb9
8159305: Enhance the javadoc tool to support module related options
ksrini
parents:
40315
diff
changeset
|
102 |
public boolean run(DocletEnvironment docEnv) { |
42277 | 103 |
configuration = getConfiguration(); |
104 |
configuration.initConfiguration(docEnv); |
|
35426
374342e56a56
8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents:
26269
diff
changeset
|
105 |
configuration.cmtUtils = new CommentUtils(configuration); |
374342e56a56
8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents:
26269
diff
changeset
|
106 |
configuration.utils = new Utils(configuration); |
25454
376a52c9540c
8039028: [javadoc] refactor the usage of Util.java
ksrini
parents:
22159
diff
changeset
|
107 |
utils = configuration.utils; |
35426
374342e56a56
8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents:
26269
diff
changeset
|
108 |
configuration.workArounds = new WorkArounds(configuration); |
40303 | 109 |
messages = configuration.getMessages(); |
110 |
||
26269
0d8efbb110b4
8054925: [javadoc] refactor the Doclet start method.
ksrini
parents:
25874
diff
changeset
|
111 |
if (!isValidDoclet()) { |
10 | 112 |
return false; |
113 |
} |
|
35426
374342e56a56
8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents:
26269
diff
changeset
|
114 |
|
10 | 115 |
try { |
43261 | 116 |
try { |
117 |
startGeneration(docEnv); |
|
118 |
return true; |
|
119 |
} catch (UncheckedDocletException e) { |
|
120 |
throw (DocletException) e.getCause(); |
|
121 |
} |
|
40587 | 122 |
|
123 |
} catch (DocFileIOException e) { |
|
124 |
switch (e.mode) { |
|
125 |
case READ: |
|
126 |
messages.error("doclet.exception.read.file", |
|
127 |
e.fileName.getPath(), e.getCause()); |
|
128 |
break; |
|
129 |
case WRITE: |
|
130 |
messages.error("doclet.exception.write.file", |
|
131 |
e.fileName.getPath(), e.getCause()); |
|
20600
052970964bc1
6978886: javadoc shows stacktrace after print error resulting from disk full
kizune
parents:
16324
diff
changeset
|
132 |
} |
41451 | 133 |
dumpStack(configuration.dumpOnError, e); |
40587 | 134 |
|
135 |
} catch (ResourceIOException e) { |
|
136 |
messages.error("doclet.exception.read.resource", |
|
137 |
e.resource.getPath(), e.getCause()); |
|
41451 | 138 |
dumpStack(configuration.dumpOnError, e); |
40587 | 139 |
|
140 |
} catch (SimpleDocletException e) { |
|
141 |
configuration.reporter.print(ERROR, e.getMessage()); |
|
41451 | 142 |
dumpStack(configuration.dumpOnError, e); |
40587 | 143 |
|
144 |
} catch (InternalException e) { |
|
145 |
configuration.reporter.print(ERROR, e.getMessage()); |
|
146 |
reportInternalError(e.getCause()); |
|
147 |
||
148 |
} catch (DocletException | RuntimeException | Error e) { |
|
149 |
messages.error("doclet.internal.exception", e); |
|
150 |
reportInternalError(e); |
|
10 | 151 |
} |
40587 | 152 |
|
153 |
return false; |
|
154 |
} |
|
155 |
||
156 |
private void reportInternalError(Throwable t) { |
|
157 |
if (getClass().equals(StandardDoclet.class) || getClass().equals(HtmlDoclet.class)) { |
|
158 |
System.err.println(configuration.getResources().getText("doclet.internal.report.bug")); |
|
159 |
} |
|
160 |
dumpStack(true, t); |
|
161 |
} |
|
162 |
||
163 |
private void dumpStack(boolean enabled, Throwable t) { |
|
164 |
if (enabled && t != null) { |
|
165 |
t.printStackTrace(System.err); |
|
166 |
} |
|
10 | 167 |
} |
168 |
||
169 |
/** |
|
35426
374342e56a56
8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents:
26269
diff
changeset
|
170 |
* Returns the SourceVersion indicating the features supported by this doclet. |
40587 | 171 |
* |
35426
374342e56a56
8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents:
26269
diff
changeset
|
172 |
* @return SourceVersion |
10 | 173 |
*/ |
40315
9e994c77db6a
8160137: HTMLDoclet and AbstractDoclet should implement Doclet
jjg
parents:
40303
diff
changeset
|
174 |
@Override |
9e994c77db6a
8160137: HTMLDoclet and AbstractDoclet should implement Doclet
jjg
parents:
40303
diff
changeset
|
175 |
public SourceVersion getSupportedSourceVersion() { |
39090
10c2cda4358d
8159396: javadoc getSupportedVersion returns 8 instead of 9
ksrini
parents:
36526
diff
changeset
|
176 |
return SourceVersion.RELEASE_9; |
10 | 177 |
} |
178 |
||
179 |
/** |
|
180 |
* Create the configuration instance and returns it. |
|
40587 | 181 |
* |
10 | 182 |
* @return the configuration of the doclet. |
183 |
*/ |
|
45417 | 184 |
public abstract BaseConfiguration getConfiguration(); |
10 | 185 |
|
186 |
/** |
|
187 |
* Start the generation of files. Call generate methods in the individual |
|
14368
6f4c62de6985
8001664: refactor javadoc to use abstraction to handle files
jjg
parents:
14357
diff
changeset
|
188 |
* writers, which will in turn generate the documentation files. Call the |
10 | 189 |
* TreeWriter generation first to ensure the Class Hierarchy is built |
190 |
* first and then can be used in the later generation. |
|
191 |
* |
|
35426
374342e56a56
8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents:
26269
diff
changeset
|
192 |
* @see jdk.doclet.DocletEnvironment |
40587 | 193 |
* @throws DocletException if there is a problem while generating the documentation |
10 | 194 |
*/ |
40587 | 195 |
private void startGeneration(DocletEnvironment docEnv) throws DocletException { |
42277 | 196 |
if (configuration.getIncludedTypeElements().isEmpty()) { |
40303 | 197 |
messages.error("doclet.No_Public_Classes_To_Document"); |
10 | 198 |
return; |
199 |
} |
|
35426
374342e56a56
8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents:
26269
diff
changeset
|
200 |
if (!configuration.setOptions()) { |
374342e56a56
8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents:
26269
diff
changeset
|
201 |
return; |
374342e56a56
8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents:
26269
diff
changeset
|
202 |
} |
40303 | 203 |
messages.notice("doclet.build_version", |
10 | 204 |
configuration.getDocletSpecificBuildDate()); |
205 |
ClassTree classtree = new ClassTree(configuration, configuration.nodeprecated); |
|
206 |
||
40508
74ef30d16fb9
8159305: Enhance the javadoc tool to support module related options
ksrini
parents:
40315
diff
changeset
|
207 |
generateClassFiles(docEnv, classtree); |
10 | 208 |
|
209 |
PackageListWriter.generate(configuration); |
|
210 |
generatePackageFiles(classtree); |
|
36526 | 211 |
generateModuleFiles(); |
10 | 212 |
|
40508
74ef30d16fb9
8159305: Enhance the javadoc tool to support module related options
ksrini
parents:
40315
diff
changeset
|
213 |
generateOtherFiles(docEnv, classtree); |
10 | 214 |
configuration.tagletManager.printReport(); |
215 |
} |
|
216 |
||
217 |
/** |
|
218 |
* Generate additional documentation that is added to the API documentation. |
|
219 |
* |
|
40587 | 220 |
* @param docEnv the DocletEnvironment |
221 |
* @param classtree the data structure representing the class tree |
|
222 |
* @throws DocletException if there is a problem while generating the documentation |
|
10 | 223 |
*/ |
40587 | 224 |
protected void generateOtherFiles(DocletEnvironment docEnv, ClassTree classtree) |
225 |
throws DocletException { |
|
10 | 226 |
BuilderFactory builderFactory = configuration.getBuilderFactory(); |
35426
374342e56a56
8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents:
26269
diff
changeset
|
227 |
AbstractBuilder constantsSummaryBuilder = builderFactory.getConstantsSummaryBuilder(); |
10 | 228 |
constantsSummaryBuilder.build(); |
229 |
AbstractBuilder serializedFormBuilder = builderFactory.getSerializedFormBuilder(); |
|
230 |
serializedFormBuilder.build(); |
|
231 |
} |
|
232 |
||
233 |
/** |
|
36526 | 234 |
* Generate the module documentation. |
235 |
* |
|
40587 | 236 |
* @throws DocletException if there is a problem while generating the documentation |
237 |
* |
|
36526 | 238 |
*/ |
40587 | 239 |
protected abstract void generateModuleFiles() throws DocletException; |
36526 | 240 |
|
241 |
/** |
|
10 | 242 |
* Generate the package documentation. |
243 |
* |
|
40587 | 244 |
* @param classtree the data structure representing the class tree |
245 |
* @throws DocletException if there is a problem while generating the documentation |
|
10 | 246 |
*/ |
40587 | 247 |
protected abstract void generatePackageFiles(ClassTree classtree) throws DocletException; |
10 | 248 |
|
249 |
/** |
|
250 |
* Generate the class documentation. |
|
251 |
* |
|
40587 | 252 |
* @param arr the set of types to be documented |
253 |
* @param classtree the data structure representing the class tree |
|
254 |
* @throws DocletException if there is a problem while generating the documentation |
|
10 | 255 |
*/ |
40587 | 256 |
protected abstract void generateClassFiles(SortedSet<TypeElement> arr, ClassTree classtree) |
257 |
throws DocletException; |
|
10 | 258 |
|
259 |
/** |
|
260 |
* Iterate through all classes and construct documentation for them. |
|
261 |
* |
|
40587 | 262 |
* @param docEnv the DocletEnvironment |
263 |
* @param classtree the data structure representing the class tree |
|
264 |
* @throws DocletException if there is a problem while generating the documentation |
|
10 | 265 |
*/ |
40587 | 266 |
protected void generateClassFiles(DocletEnvironment docEnv, ClassTree classtree) |
267 |
throws DocletException { |
|
10 | 268 |
generateClassFiles(classtree); |
35426
374342e56a56
8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents:
26269
diff
changeset
|
269 |
SortedSet<PackageElement> packages = new TreeSet<>(utils.makePackageComparator()); |
42277 | 270 |
packages.addAll(configuration.getSpecifiedPackageElements()); |
42827
36468b5fa7f4
8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents:
42277
diff
changeset
|
271 |
configuration.modulePackages.values().stream().forEach(packages::addAll); |
40587 | 272 |
for (PackageElement pkg : packages) { |
35426
374342e56a56
8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents:
26269
diff
changeset
|
273 |
generateClassFiles(utils.getAllClasses(pkg), classtree); |
40587 | 274 |
} |
10 | 275 |
} |
276 |
||
277 |
/** |
|
278 |
* Generate the class files for single classes specified on the command line. |
|
279 |
* |
|
40587 | 280 |
* @param classtree the data structure representing the class tree |
281 |
* @throws DocletException if there is a problem while generating the documentation |
|
10 | 282 |
*/ |
40587 | 283 |
private void generateClassFiles(ClassTree classtree) throws DocletException { |
35426
374342e56a56
8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents:
26269
diff
changeset
|
284 |
SortedSet<PackageElement> packages = configuration.typeElementCatalog.packages(); |
40587 | 285 |
for (PackageElement pkg : packages) { |
35426
374342e56a56
8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents:
26269
diff
changeset
|
286 |
generateClassFiles(configuration.typeElementCatalog.allClasses(pkg), classtree); |
40587 | 287 |
} |
10 | 288 |
} |
289 |
} |