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.builders;
  27 
  28 import java.util.*;
  29 
  30 import javax.lang.model.element.Element;
  31 import javax.lang.model.element.ExecutableElement;
  32 import javax.lang.model.element.TypeElement;
  33 
  34 import jdk.javadoc.internal.doclets.toolkit.BaseConfiguration;
  35 import jdk.javadoc.internal.doclets.toolkit.ConstructorWriter;
  36 import jdk.javadoc.internal.doclets.toolkit.Content;
  37 import jdk.javadoc.internal.doclets.toolkit.DocletException;
  38 
  39 import static jdk.javadoc.internal.doclets.toolkit.util.VisibleMemberTable.Kind.*;
  40 
  41 /**
  42  * Builds documentation for a constructor.
  43  *
  44  *  <p><b>This is NOT part of any supported API.
  45  *  If you write code that depends on this, you do so at your own risk.
  46  *  This code and its internal interfaces are subject to change or
  47  *  deletion without notice.</b>
  48  */
  49 public class ConstructorBuilder extends AbstractMemberBuilder {
  50 
  51     /**
  52      * The current constructor that is being documented at this point in time.
  53      */
  54     private ExecutableElement currentConstructor;
  55 
  56     /**
  57      * The writer to output the constructor documentation.
  58      */
  59     private final ConstructorWriter writer;
  60 
  61     /**
  62      * The constructors being documented.
  63      */
  64     private final List<? extends Element> constructors;
  65 
  66     /**
  67      * Construct a new ConstructorBuilder.
  68      *
  69      * @param context  the build context.
  70      * @param typeElement the class whose members are being documented.
  71      * @param writer the doclet specific writer.
  72      */
  73     private ConstructorBuilder(Context context,
  74             TypeElement typeElement,
  75             ConstructorWriter writer) {
  76         super(context, typeElement);
  77         this.writer = writer;
  78         constructors = getVisibleMembers(CONSTRUCTORS);
  79         for (Element ctor : constructors) {
  80             if (utils.isProtected(ctor) || utils.isPrivate(ctor)) {
  81                 writer.setFoundNonPubConstructor(true);
  82             }
  83         }
  84     }
  85 
  86     /**
  87      * Construct a new ConstructorBuilder.
  88      *
  89      * @param context  the build context.
  90      * @param typeElement the class whose members are being documented.
  91      * @param writer the doclet specific writer.
  92      * @return the new ConstructorBuilder
  93      */
  94     public static ConstructorBuilder getInstance(Context context,
  95             TypeElement typeElement, ConstructorWriter writer) {
  96         return new ConstructorBuilder(context, typeElement, writer);
  97     }
  98 
  99     /**
 100      * {@inheritDoc}
 101      */
 102     @Override
 103     public boolean hasMembersToDocument() {
 104         return !constructors.isEmpty();
 105     }
 106 
 107     /**
 108      * Return the constructor writer for this builder.
 109      *
 110      * @return the constructor writer for this builder.
 111      */
 112     public ConstructorWriter getWriter() {
 113         return writer;
 114     }
 115 
 116     /**
 117      * {@inheritDoc}
 118      */
 119     @Override
 120     public void build(Content contentTree) throws DocletException {
 121         buildConstructorDoc(contentTree);
 122     }
 123 
 124     /**
 125      * Build the constructor documentation.
 126      *
 127      * @param memberDetailsTree the content tree to which the documentation will be added
 128      * @throws DocletException is there is a problem while building the documentation
 129      */
 130     protected void buildConstructorDoc(Content memberDetailsTree) throws DocletException {
 131         if (writer == null) {
 132             return;
 133         }
 134         if (hasMembersToDocument()) {
 135             Content constructorDetailsTreeHeader = writer.getConstructorDetailsTreeHeader(typeElement,
 136                     memberDetailsTree);
 137             Content constructorDetailsTree = writer.getMemberTreeHeader();
 138 
 139             for (Element constructor : constructors) {
 140                 currentConstructor = (ExecutableElement)constructor;
 141                 Content constructorDocTree = writer.getConstructorDocTreeHeader(currentConstructor, constructorDetailsTree);
 142 
 143                 buildSignature(constructorDocTree);
 144                 buildDeprecationInfo(constructorDocTree);
 145                 buildConstructorComments(constructorDocTree);
 146                 buildTagInfo(constructorDocTree);
 147 
 148                 constructorDetailsTree.add(writer.getConstructorDoc(constructorDocTree));
 149             }
 150             memberDetailsTree.add(
 151                     writer.getConstructorDetails(constructorDetailsTreeHeader, constructorDetailsTree));
 152         }
 153     }
 154 
 155     /**
 156      * Build the signature.
 157      *
 158      * @param constructorDocTree the content tree to which the documentation will be added
 159      */
 160     protected void buildSignature(Content constructorDocTree) {
 161         constructorDocTree.add(writer.getSignature(currentConstructor));
 162     }
 163 
 164     /**
 165      * Build the deprecation information.
 166      *
 167      * @param constructorDocTree the content tree to which the documentation will be added
 168      */
 169     protected void buildDeprecationInfo(Content constructorDocTree) {
 170         writer.addDeprecated(currentConstructor, constructorDocTree);
 171     }
 172 
 173     /**
 174      * Build the comments for the constructor.  Do nothing if
 175      * {@link BaseConfiguration#nocomment} is set to true.
 176      *
 177      * @param constructorDocTree the content tree to which the documentation will be added
 178      */
 179     protected void buildConstructorComments(Content constructorDocTree) {
 180         if (!configuration.nocomment) {
 181             writer.addComments(currentConstructor, constructorDocTree);
 182         }
 183     }
 184 
 185     /**
 186      * Build the tag information.
 187      *
 188      * @param constructorDocTree the content tree to which the documentation will be added
 189      */
 190     protected void buildTagInfo(Content constructorDocTree) {
 191         writer.addTags(currentConstructor, constructorDocTree);
 192     }
 193 }