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