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(memberDetailsTree);
 127             Content methodDetailsTree = writer.getMemberTreeHeader();
 128 
 129             for (Element method : methods) {
 130                 currentMethod = (ExecutableElement)method;
 131                 Content methodDocTree = writer.getMethodDocTreeHeader(currentMethod);
 132 
 133                 buildSignature(methodDocTree);
 134                 buildDeprecationInfo(methodDocTree);
 135                 buildMethodComments(methodDocTree);
 136                 buildTagInfo(methodDocTree);
 137 
 138                 methodDetailsTree.add(writer.getMethodDoc(methodDocTree));
 139             }
 140             memberDetailsTree.add(writer.getMethodDetails(methodDetailsTreeHeader, methodDetailsTree));
 141         }
 142     }
 143 
 144     /**
 145      * Build the signature.
 146      *
 147      * @param methodDocTree the content tree to which the documentation will be added
 148      */
 149     protected void buildSignature(Content methodDocTree) {
 150         methodDocTree.add(writer.getSignature(currentMethod));
 151     }
 152 
 153     /**
 154      * Build the deprecation information.
 155      *
 156      * @param methodDocTree the content tree to which the documentation will be added
 157      */
 158     protected void buildDeprecationInfo(Content methodDocTree) {
 159         writer.addDeprecated(currentMethod, methodDocTree);
 160     }
 161 
 162     /**
 163      * Build the comments for the method.  Do nothing if
 164      * {@link BaseConfiguration#nocomment} is set to true.
 165      *
 166      * @param methodDocTree the content tree to which the documentation will be added
 167      */
 168     protected void buildMethodComments(Content methodDocTree) {
 169         if (!configuration.nocomment) {
 170             ExecutableElement method = currentMethod;
 171             if (utils.getFullBody(currentMethod).isEmpty()) {
 172                 DocFinder.Output docs = DocFinder.search(configuration,
 173                         new DocFinder.Input(utils, currentMethod));
 174                 if (docs.inlineTags != null && !docs.inlineTags.isEmpty())
 175                         method = (ExecutableElement)docs.holder;
 176             }
 177             TypeMirror containingType = method.getEnclosingElement().asType();
 178             writer.addComments(containingType, method, methodDocTree);
 179         }
 180     }
 181 
 182     /**
 183      * Build the tag information.
 184      *
 185      * @param methodDocTree the content tree to which the documentation will be added
 186      */
 187     protected void buildTagInfo(Content methodDocTree) {
 188         writer.addTags(currentMethod, methodDocTree);
 189     }
 190 
 191     /**
 192      * Return the method writer for this builder.
 193      *
 194      * @return the method writer for this builder.
 195      */
 196     public MethodWriter getWriter() {
 197         return writer;
 198     }
 199 }