1 /*
   2  * Copyright (c) 2003, 2019, 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.SortedSet;
  29 import java.util.TreeSet;
  30 
  31 import javax.lang.model.SourceVersion;
  32 import javax.lang.model.element.PackageElement;
  33 import javax.lang.model.element.TypeElement;
  34 
  35 import jdk.javadoc.doclet.Doclet;
  36 import jdk.javadoc.doclet.DocletEnvironment;
  37 import jdk.javadoc.doclet.StandardDoclet;
  38 import jdk.javadoc.internal.doclets.formats.html.HtmlDoclet;
  39 import jdk.javadoc.internal.doclets.toolkit.builders.AbstractBuilder;
  40 import jdk.javadoc.internal.doclets.toolkit.builders.BuilderFactory;
  41 import jdk.javadoc.internal.doclets.toolkit.util.ClassTree;
  42 import jdk.javadoc.internal.doclets.toolkit.util.DocFileIOException;
  43 import jdk.javadoc.internal.doclets.toolkit.util.UncheckedDocletException;
  44 import jdk.javadoc.internal.doclets.toolkit.util.InternalException;
  45 import jdk.javadoc.internal.doclets.toolkit.util.ElementListWriter;
  46 import jdk.javadoc.internal.doclets.toolkit.util.ResourceIOException;
  47 import jdk.javadoc.internal.doclets.toolkit.util.SimpleDocletException;
  48 import jdk.javadoc.internal.doclets.toolkit.util.Utils;
  49 
  50 import static javax.tools.Diagnostic.Kind.*;
  51 
  52 /**
  53  * An abstract implementation of a Doclet.
  54  *
  55  *  <p><b>This is NOT part of any supported API.
  56  *  If you write code that depends on this, you do so at your own risk.
  57  *  This code and its internal interfaces are subject to change or
  58  *  deletion without notice.</b>
  59  */
  60 public abstract class AbstractDoclet implements Doclet {
  61 
  62     /**
  63      * The global configuration information for this run.
  64      */
  65     private BaseConfiguration configuration;
  66 
  67     protected Messages messages;
  68 
  69     /*
  70      *  a handle to our utility methods
  71      */
  72     protected Utils utils;
  73 
  74     /**
  75      * The only doclet that may use this toolkit is {@value}
  76      */
  77     private static final String TOOLKIT_DOCLET_NAME =
  78         jdk.javadoc.internal.doclets.formats.html.HtmlDoclet.class.getName();
  79 
  80     /**
  81      * Verify that the only doclet that is using this toolkit is
  82      * #TOOLKIT_DOCLET_NAME.
  83      */
  84     private boolean isValidDoclet() {
  85         if (!getClass().getName().equals(TOOLKIT_DOCLET_NAME)) {
  86             messages.error("doclet.Toolkit_Usage_Violation",
  87                 TOOLKIT_DOCLET_NAME);
  88             return false;
  89         }
  90         return true;
  91     }
  92 
  93     /**
  94      * The method that starts the execution of the doclet.
  95      *
  96      * @param docEnv   the {@link DocletEnvironment}.
  97      * @return true if the doclet executed without error.  False otherwise.
  98      */
  99     @Override
 100     public boolean run(DocletEnvironment docEnv) {
 101         configuration = getConfiguration();
 102         configuration.initConfiguration(docEnv);
 103         utils = configuration.utils;
 104         messages = configuration.getMessages();
 105 
 106         if (!isValidDoclet()) {
 107             return false;
 108         }
 109 
 110         try {
 111             try {
 112                 startGeneration(docEnv);
 113                 return true;
 114             } catch (UncheckedDocletException e) {
 115                 throw (DocletException) e.getCause();
 116             }
 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 BaseConfiguration 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      * @throws DocletException if there is a problem while generating the documentation
 188      */
 189     private void startGeneration(DocletEnvironment docEnv) throws DocletException {
 190 
 191         // Modules with no documented classes may be specified on the
 192         // command line to specify a service provider, allow these.
 193         if (configuration.getSpecifiedModuleElements().isEmpty() &&
 194                 configuration.getIncludedTypeElements().isEmpty()) {
 195             messages.error("doclet.No_Public_Classes_To_Document");
 196             return;
 197         }
 198         if (!configuration.setOptions()) {
 199             return;
 200         }
 201         messages.notice("doclet.build_version",
 202             configuration.getDocletVersion());
 203         ClassTree classtree = new ClassTree(configuration, configuration.nodeprecated);
 204 
 205         generateClassFiles(docEnv, classtree);
 206 
 207         ElementListWriter.generate(configuration);
 208         generatePackageFiles(classtree);
 209         generateModuleFiles();
 210 
 211         generateOtherFiles(docEnv, classtree);
 212         configuration.tagletManager.printReport();
 213     }
 214 
 215     /**
 216      * Generate additional documentation that is added to the API documentation.
 217      *
 218      * @param docEnv     the DocletEnvironment
 219      * @param classtree the data structure representing the class tree
 220      * @throws DocletException if there is a problem while generating the documentation
 221      */
 222     protected void generateOtherFiles(DocletEnvironment docEnv, ClassTree classtree)
 223             throws DocletException {
 224         BuilderFactory builderFactory = configuration.getBuilderFactory();
 225         AbstractBuilder constantsSummaryBuilder = builderFactory.getConstantsSummaryBuilder();
 226         constantsSummaryBuilder.build();
 227         AbstractBuilder serializedFormBuilder = builderFactory.getSerializedFormBuilder();
 228         serializedFormBuilder.build();
 229     }
 230 
 231     /**
 232      * Generate the module documentation.
 233      *
 234      * @throws DocletException if there is a problem while generating the documentation
 235      *
 236      */
 237     protected abstract void generateModuleFiles() throws DocletException;
 238 
 239     /**
 240      * Generate the package documentation.
 241      *
 242      * @param classtree the data structure representing the class tree
 243      * @throws DocletException if there is a problem while generating the documentation
 244      */
 245     protected abstract void generatePackageFiles(ClassTree classtree) throws DocletException;
 246 
 247     /**
 248      * Generate the class documentation.
 249      *
 250      * @param arr the set of types to be documented
 251      * @param classtree the data structure representing the class tree
 252      * @throws DocletException if there is a problem while generating the documentation
 253      */
 254     protected abstract void generateClassFiles(SortedSet<TypeElement> arr, ClassTree classtree)
 255             throws DocletException;
 256 
 257     /**
 258      * Iterate through all classes and construct documentation for them.
 259      *
 260      * @param docEnv      the DocletEnvironment
 261      * @param classtree the data structure representing the class tree
 262      * @throws DocletException if there is a problem while generating the documentation
 263      */
 264     protected void generateClassFiles(DocletEnvironment docEnv, ClassTree classtree)
 265             throws DocletException {
 266         generateClassFiles(classtree);
 267         SortedSet<PackageElement> packages = new TreeSet<>(utils.makePackageComparator());
 268         packages.addAll(configuration.getSpecifiedPackageElements());
 269         configuration.modulePackages.values().stream().forEach(packages::addAll);
 270         for (PackageElement pkg : packages) {
 271             generateClassFiles(utils.getAllClasses(pkg), classtree);
 272         }
 273     }
 274 
 275     /**
 276      * Generate the class files for single classes specified on the command line.
 277      *
 278      * @param classtree the data structure representing the class tree
 279      * @throws DocletException if there is a problem while generating the documentation
 280      */
 281     private void generateClassFiles(ClassTree classtree) throws DocletException {
 282         SortedSet<PackageElement> packages = configuration.typeElementCatalog.packages();
 283         for (PackageElement pkg : packages) {
 284             generateClassFiles(configuration.typeElementCatalog.allClasses(pkg), classtree);
 285         }
 286     }
 287 }