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  * @author Jamie Ho
  50  * @author Bhavesh Patel (Modified)
  51  */
  52 public class ConstructorBuilder extends AbstractMemberBuilder {
  53 
  54     /**
  55      * The current constructor that is being documented at this point in time.
  56      */
  57     private ExecutableElement currentConstructor;
  58 
  59     /**
  60      * The writer to output the constructor documentation.
  61      */
  62     private final ConstructorWriter writer;
  63 
  64     /**
  65      * The constructors being documented.
  66      */
  67     private final List<? extends Element> constructors;
  68 
  69     /**
  70      * Construct a new ConstructorBuilder.
  71      *
  72      * @param context  the build context.
  73      * @param typeElement the class whose members are being documented.
  74      * @param writer the doclet specific writer.
  75      */
  76     private ConstructorBuilder(Context context,
  77             TypeElement typeElement,
  78             ConstructorWriter writer) {
  79         super(context, typeElement);
  80         this.writer = writer;
  81         constructors = getVisibleMembers(CONSTRUCTORS);
  82         for (Element ctor : constructors) {
  83             if (utils.isProtected(ctor) || utils.isPrivate(ctor)) {
  84                 writer.setFoundNonPubConstructor(true);
  85             }
  86         }
  87     }
  88 
  89     /**
  90      * Construct a new ConstructorBuilder.
  91      *
  92      * @param context  the build context.
  93      * @param typeElement the class whose members are being documented.
  94      * @param writer the doclet specific writer.
  95      * @return the new ConstructorBuilder
  96      */
  97     public static ConstructorBuilder getInstance(Context context,
  98             TypeElement typeElement, ConstructorWriter writer) {
  99         return new ConstructorBuilder(context, typeElement, writer);
 100     }
 101 
 102     /**
 103      * {@inheritDoc}
 104      */
 105     @Override
 106     public boolean hasMembersToDocument() {
 107         return !constructors.isEmpty();
 108     }
 109 
 110     /**
 111      * Return the constructor writer for this builder.
 112      *
 113      * @return the constructor writer for this builder.
 114      */
 115     public ConstructorWriter getWriter() {
 116         return writer;
 117     }
 118 
 119     /**
 120      * {@inheritDoc}
 121      */
 122     @Override
 123     public void build(Content contentTree) throws DocletException {
 124         buildConstructorDoc(contentTree);
 125     }
 126 
 127     /**
 128      * Build the constructor documentation.
 129      *
 130      * @param memberDetailsTree the content tree to which the documentation will be added
 131      * @throws DocletException is there is a problem while building the documentation
 132      */
 133     protected void buildConstructorDoc(Content memberDetailsTree) throws DocletException {
 134         if (writer == null) {
 135             return;
 136         }
 137         if (hasMembersToDocument()) {
 138             Content constructorDetailsTreeHeader = writer.getConstructorDetailsTreeHeader(typeElement,
 139                     memberDetailsTree);
 140             Content constructorDetailsTree = writer.getMemberTreeHeader();
 141 
 142             for (Element contructor : constructors) {
 143                 currentConstructor = (ExecutableElement)contructor;
 144                 Content constructorDocTree = writer.getConstructorDocTreeHeader(currentConstructor, constructorDetailsTree);
 145 
 146                 buildSignature(constructorDocTree);
 147                 buildDeprecationInfo(constructorDocTree);
 148                 buildConstructorComments(constructorDocTree);
 149                 buildTagInfo(constructorDocTree);
 150 
 151                 constructorDetailsTree.add(writer.getConstructorDoc(constructorDocTree));
 152             }
 153             memberDetailsTree.add(
 154                     writer.getConstructorDetails(constructorDetailsTreeHeader, constructorDetailsTree));
 155         }
 156     }
 157 
 158     /**
 159      * Build the signature.
 160      *
 161      * @param constructorDocTree the content tree to which the documentation will be added
 162      */
 163     protected void buildSignature(Content constructorDocTree) {
 164         constructorDocTree.add(writer.getSignature(currentConstructor));
 165     }
 166 
 167     /**
 168      * Build the deprecation information.
 169      *
 170      * @param constructorDocTree the content tree to which the documentation will be added
 171      */
 172     protected void buildDeprecationInfo(Content constructorDocTree) {
 173         writer.addDeprecated(currentConstructor, constructorDocTree);
 174     }
 175 
 176     /**
 177      * Build the comments for the constructor.  Do nothing if
 178      * {@link BaseConfiguration#nocomment} is set to true.
 179      *
 180      * @param constructorDocTree the content tree to which the documentation will be added
 181      */
 182     protected void buildConstructorComments(Content constructorDocTree) {
 183         if (!configuration.nocomment) {
 184             writer.addComments(currentConstructor, constructorDocTree);
 185         }
 186     }
 187 
 188     /**
 189      * Build the tag information.
 190      *
 191      * @param constructorDocTree the content tree to which the documentation will be added
 192      */
 193     protected void buildTagInfo(Content constructorDocTree) {
 194         writer.addTags(currentConstructor, constructorDocTree);
 195     }
 196 }