/
AbstractDoclet.java
288 lines (249 loc) · 10.6 KB
/
AbstractDoclet.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
/*
* Copyright (c) 2003, 2020, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package jdk.javadoc.internal.doclets.toolkit;
import java.util.Map;
import java.util.SortedSet;
import java.util.TreeSet;
import java.util.function.Function;
import javax.lang.model.SourceVersion;
import javax.lang.model.element.PackageElement;
import javax.lang.model.element.TypeElement;
import jdk.javadoc.doclet.Doclet;
import jdk.javadoc.doclet.DocletEnvironment;
import jdk.javadoc.doclet.StandardDoclet;
import jdk.javadoc.internal.doclets.formats.html.HtmlDoclet;
import jdk.javadoc.internal.doclets.toolkit.builders.AbstractBuilder;
import jdk.javadoc.internal.doclets.toolkit.builders.BuilderFactory;
import jdk.javadoc.internal.doclets.toolkit.util.ClassTree;
import jdk.javadoc.internal.doclets.toolkit.util.DocFileIOException;
import jdk.javadoc.internal.doclets.toolkit.util.UncheckedDocletException;
import jdk.javadoc.internal.doclets.toolkit.util.InternalException;
import jdk.javadoc.internal.doclets.toolkit.util.ElementListWriter;
import jdk.javadoc.internal.doclets.toolkit.util.ResourceIOException;
import jdk.javadoc.internal.doclets.toolkit.util.SimpleDocletException;
import jdk.javadoc.internal.doclets.toolkit.util.Utils;
import static javax.tools.Diagnostic.Kind.*;
/**
* An abstract implementation of a Doclet.
*
* <p><b>This is NOT part of any supported API.
* If you write code that depends on this, you do so at your own risk.
* This code and its internal interfaces are subject to change or
* deletion without notice.</b>
*/
public abstract class AbstractDoclet implements Doclet {
/**
* The global configuration information for this run.
*/
private BaseConfiguration configuration;
protected Messages messages;
/*
* a handle to our utility methods
*/
protected Utils utils;
/**
* The only doclet that may use this toolkit is {@value}
*/
private static final String TOOLKIT_DOCLET_NAME =
jdk.javadoc.internal.doclets.formats.html.HtmlDoclet.class.getName();
/**
* Verify that the only doclet that is using this toolkit is
* #TOOLKIT_DOCLET_NAME.
*/
private boolean isValidDoclet() {
if (!getClass().getName().equals(TOOLKIT_DOCLET_NAME)) {
messages.error("doclet.Toolkit_Usage_Violation",
TOOLKIT_DOCLET_NAME);
return false;
}
return true;
}
/**
* The method that starts the execution of the doclet.
*
* @param docEnv the {@link DocletEnvironment}.
* @return true if the doclet executed without error. False otherwise.
*/
@Override
public boolean run(DocletEnvironment docEnv) {
configuration = getConfiguration();
configuration.initConfiguration(docEnv, getResourceKeyMapper(docEnv));
utils = configuration.utils;
messages = configuration.getMessages();
BaseOptions options = configuration.getOptions();
if (!isValidDoclet()) {
return false;
}
try {
try {
startGeneration();
return true;
} catch (UncheckedDocletException e) {
throw (DocletException) e.getCause();
}
} catch (DocFileIOException e) {
switch (e.mode) {
case READ:
messages.error("doclet.exception.read.file",
e.fileName.getPath(), e.getCause());
break;
case WRITE:
messages.error("doclet.exception.write.file",
e.fileName.getPath(), e.getCause());
}
dumpStack(options.dumpOnError(), e);
} catch (ResourceIOException e) {
messages.error("doclet.exception.read.resource",
e.resource.getPath(), e.getCause());
dumpStack(options.dumpOnError(), e);
} catch (SimpleDocletException e) {
configuration.reporter.print(ERROR, e.getMessage());
dumpStack(options.dumpOnError(), e);
} catch (InternalException e) {
configuration.reporter.print(ERROR, e.getMessage());
reportInternalError(e.getCause());
} catch (DocletException | RuntimeException | Error e) {
messages.error("doclet.internal.exception", e);
reportInternalError(e);
}
return false;
}
protected Function<String, String> getResourceKeyMapper(DocletEnvironment docEnv) {
return null;
}
private void reportInternalError(Throwable t) {
if (getClass().equals(StandardDoclet.class) || getClass().equals(HtmlDoclet.class)) {
System.err.println(configuration.getDocResources().getText("doclet.internal.report.bug"));
}
dumpStack(true, t);
}
private void dumpStack(boolean enabled, Throwable t) {
if (enabled && t != null) {
t.printStackTrace(System.err);
}
}
/**
* Returns the SourceVersion indicating the features supported by this doclet.
*
* @return SourceVersion
*/
@Override
public SourceVersion getSupportedSourceVersion() {
return SourceVersion.RELEASE_9;
}
/**
* Create the configuration instance and returns it.
*
* @return the configuration of the doclet.
*/
public abstract BaseConfiguration getConfiguration();
/**
* Start the generation of files. Call generate methods in the individual
* writers, which will in turn generate the documentation files. Call the
* TreeWriter generation first to ensure the Class Hierarchy is built
* first and then can be used in the later generation.
*
* @throws DocletException if there is a problem while generating the documentation
*/
private void startGeneration() throws DocletException {
// Modules with no documented classes may be specified on the
// command line to specify a service provider, allow these.
if (configuration.getSpecifiedModuleElements().isEmpty() &&
configuration.getIncludedTypeElements().isEmpty()) {
messages.error("doclet.No_Public_Classes_To_Document");
return;
}
if (!configuration.setOptions()) {
return;
}
messages.notice("doclet.build_version",
configuration.getDocletVersion());
ClassTree classtree = new ClassTree(configuration, configuration.getOptions().noDeprecated());
generateClassFiles(classtree);
ElementListWriter.generate(configuration);
generatePackageFiles(classtree);
generateModuleFiles();
generateOtherFiles(classtree);
configuration.tagletManager.printReport();
}
/**
* Generate additional documentation that is added to the API documentation.
*
* @param classtree the data structure representing the class tree
* @throws DocletException if there is a problem while generating the documentation
*/
protected void generateOtherFiles(ClassTree classtree) throws DocletException {
BuilderFactory builderFactory = configuration.getBuilderFactory();
AbstractBuilder constantsSummaryBuilder = builderFactory.getConstantsSummaryBuilder();
constantsSummaryBuilder.build();
AbstractBuilder serializedFormBuilder = builderFactory.getSerializedFormBuilder();
serializedFormBuilder.build();
}
/**
* Generate the module documentation.
*
* @throws DocletException if there is a problem while generating the documentation
*
*/
protected abstract void generateModuleFiles() throws DocletException;
/**
* Generate the package documentation.
*
* @param classtree the data structure representing the class tree
* @throws DocletException if there is a problem while generating the documentation
*/
protected abstract void generatePackageFiles(ClassTree classtree) throws DocletException;
/**
* Generate the class documentation.
*
* @param arr the set of types to be documented
* @param classtree the data structure representing the class tree
* @throws DocletException if there is a problem while generating the documentation
*/
protected abstract void generateClassFiles(SortedSet<TypeElement> arr, ClassTree classtree)
throws DocletException;
/**
* Iterate through all classes and construct documentation for them.
*
* @param classtree the data structure representing the class tree
* @throws DocletException if there is a problem while generating the documentation
*/
protected void generateClassFiles(ClassTree classtree)
throws DocletException {
SortedSet<TypeElement> classes = new TreeSet<>(utils.comparators.makeGeneralPurposeComparator());
// handle classes specified as files on the command line
for (PackageElement pkg : configuration.typeElementCatalog.packages()) {
classes.addAll(configuration.typeElementCatalog.allClasses(pkg));
}
// handle classes specified in modules and packages on the command line
SortedSet<PackageElement> packages = new TreeSet<>(utils.comparators.makePackageComparator());
packages.addAll(configuration.getSpecifiedPackageElements());
configuration.modulePackages.values().stream().forEach(packages::addAll);
for (PackageElement pkg : packages) {
classes.addAll(utils.getAllClasses(pkg));
}
generateClassFiles(classes, classtree);
}
}