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