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 BaseOptions options = configuration.getOptions(); 106 107 if (!isValidDoclet()) { 108 return false; 109 } 110 111 try { 112 try { 113 startGeneration(docEnv); 114 return true; 115 } catch (UncheckedDocletException e) { 116 throw (DocletException) e.getCause(); 117 } 118 119 } catch (DocFileIOException e) { 120 switch (e.mode) { 121 case READ: 122 messages.error("doclet.exception.read.file", 123 e.fileName.getPath(), e.getCause()); 124 break; 125 case WRITE: 126 messages.error("doclet.exception.write.file", 127 e.fileName.getPath(), e.getCause()); 128 } 129 dumpStack(options.dumpOnError, e); 130 131 } catch (ResourceIOException e) { 132 messages.error("doclet.exception.read.resource", 133 e.resource.getPath(), e.getCause()); 134 dumpStack(options.dumpOnError, e); 135 136 } catch (SimpleDocletException e) { 137 configuration.reporter.print(ERROR, e.getMessage()); 138 dumpStack(options.dumpOnError, e); 139 140 } catch (InternalException e) { 141 configuration.reporter.print(ERROR, e.getMessage()); 142 reportInternalError(e.getCause()); 143 144 } catch (DocletException | RuntimeException | Error e) { 145 messages.error("doclet.internal.exception", e); 146 reportInternalError(e); 147 } 148 149 return false; 150 } 151 152 private void reportInternalError(Throwable t) { 153 if (getClass().equals(StandardDoclet.class) || getClass().equals(HtmlDoclet.class)) { 154 System.err.println(configuration.getResources().getText("doclet.internal.report.bug")); 155 } 156 dumpStack(true, t); 157 } 158 159 private void dumpStack(boolean enabled, Throwable t) { 160 if (enabled && t != null) { 161 t.printStackTrace(System.err); 162 } 163 } 164 165 /** 166 * Returns the SourceVersion indicating the features supported by this doclet. 167 * 168 * @return SourceVersion 169 */ 170 @Override 171 public SourceVersion getSupportedSourceVersion() { 172 return SourceVersion.RELEASE_9; 173 } 174 175 /** 176 * Create the configuration instance and returns it. 177 * 178 * @return the configuration of the doclet. 179 */ 180 public abstract BaseConfiguration getConfiguration(); 181 182 /** 183 * Start the generation of files. Call generate methods in the individual 184 * writers, which will in turn generate the documentation files. Call the 185 * TreeWriter generation first to ensure the Class Hierarchy is built 186 * first and then can be used in the later generation. 187 * 188 * @throws DocletException if there is a problem while generating the documentation 189 */ 190 private void startGeneration(DocletEnvironment docEnv) throws DocletException { 191 192 // Modules with no documented classes may be specified on the 193 // command line to specify a service provider, allow these. 194 if (configuration.getSpecifiedModuleElements().isEmpty() && 195 configuration.getIncludedTypeElements().isEmpty()) { 196 messages.error("doclet.No_Public_Classes_To_Document"); 197 return; 198 } 199 if (!configuration.setOptions()) { 200 return; 201 } 202 messages.notice("doclet.build_version", 203 configuration.getDocletVersion()); 204 ClassTree classtree = new ClassTree(configuration, configuration.getOptions().noDeprecated); 205 206 generateClassFiles(docEnv, classtree); 207 208 ElementListWriter.generate(configuration); 209 generatePackageFiles(classtree); 210 generateModuleFiles(); 211 212 generateOtherFiles(docEnv, classtree); 213 configuration.tagletManager.printReport(); 214 } 215 216 /** 217 * Generate additional documentation that is added to the API documentation. 218 * 219 * @param docEnv the DocletEnvironment 220 * @param classtree the data structure representing the class tree 221 * @throws DocletException if there is a problem while generating the documentation 222 */ 223 protected void generateOtherFiles(DocletEnvironment docEnv, ClassTree classtree) 224 throws DocletException { 225 BuilderFactory builderFactory = configuration.getBuilderFactory(); 226 AbstractBuilder constantsSummaryBuilder = builderFactory.getConstantsSummaryBuilder(); 227 constantsSummaryBuilder.build(); 228 AbstractBuilder serializedFormBuilder = builderFactory.getSerializedFormBuilder(); 229 serializedFormBuilder.build(); 230 } 231 232 /** 233 * Generate the module documentation. 234 * 235 * @throws DocletException if there is a problem while generating the documentation 236 * 237 */ 238 protected abstract void generateModuleFiles() throws DocletException; 239 240 /** 241 * Generate the package documentation. 242 * 243 * @param classtree the data structure representing the class tree 244 * @throws DocletException if there is a problem while generating the documentation 245 */ 246 protected abstract void generatePackageFiles(ClassTree classtree) throws DocletException; 247 248 /** 249 * Generate the class documentation. 250 * 251 * @param arr the set of types to be documented 252 * @param classtree the data structure representing the class tree 253 * @throws DocletException if there is a problem while generating the documentation 254 */ 255 protected abstract void generateClassFiles(SortedSet<TypeElement> arr, ClassTree classtree) 256 throws DocletException; 257 258 /** 259 * Iterate through all classes and construct documentation for them. 260 * 261 * @param docEnv the DocletEnvironment 262 * @param classtree the data structure representing the class tree 263 * @throws DocletException if there is a problem while generating the documentation 264 */ 265 protected void generateClassFiles(DocletEnvironment docEnv, ClassTree classtree) 266 throws DocletException { 267 generateClassFiles(classtree); 268 SortedSet<PackageElement> packages = new TreeSet<>(utils.makePackageComparator()); 269 packages.addAll(configuration.getSpecifiedPackageElements()); 270 configuration.modulePackages.values().stream().forEach(packages::addAll); 271 for (PackageElement pkg : packages) { 272 generateClassFiles(utils.getAllClasses(pkg), classtree); 273 } 274 } 275 276 /** 277 * Generate the class files for single classes specified on the command line. 278 * 279 * @param classtree the data structure representing the class tree 280 * @throws DocletException if there is a problem while generating the documentation 281 */ 282 private void generateClassFiles(ClassTree classtree) throws DocletException { 283 SortedSet<PackageElement> packages = configuration.typeElementCatalog.packages(); 284 for (PackageElement pkg : packages) { 285 generateClassFiles(configuration.typeElementCatalog.allClasses(pkg), classtree); 286 } 287 } 288 }