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