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  * @author Jamie Ho
  52  * @author Bhavesh Patel (Modified)
  53  */
  54 public class MethodBuilder extends AbstractMemberBuilder {
  55 
  56     /**
  57      * The index of the current field that is being documented at this point
  58      * in time.
  59      */
  60     private ExecutableElement currentMethod;
  61 
  62     /**
  63      * The writer to output the method documentation.
  64      */
  65     private final MethodWriter writer;
  66 
  67     /**
  68      * The methods being documented.
  69      */
  70     private final List<? extends Element> methods;
  71 
  72 
  73     /**
  74      * Construct a new MethodBuilder.
  75      *
  76      * @param context       the build context.
  77      * @param typeElement the class whoses members are being documented.
  78      * @param writer the doclet specific writer.
  79      */
  80     private MethodBuilder(Context context,
  81             TypeElement typeElement,
  82             MethodWriter writer) {
  83         super(context, typeElement);
  84         this.writer = writer;
  85         methods = getVisibleMembers(METHODS);
  86     }
  87 
  88     /**
  89      * Construct a new MethodBuilder.
  90      *
  91      * @param context       the build context.
  92      * @param typeElement the class whoses members are being documented.
  93      * @param writer the doclet specific writer.
  94      *
  95      * @return an instance of a MethodBuilder.
  96      */
  97     public static MethodBuilder getInstance(Context context,
  98             TypeElement typeElement, MethodWriter writer) {
  99         return new MethodBuilder(context, typeElement, writer);
 100     }
 101 
 102     /**
 103      * {@inheritDoc}
 104      */
 105     @Override
 106     public boolean hasMembersToDocument() {
 107         return !methods.isEmpty();
 108     }
 109 
 110     /**
 111      * {@inheritDoc}
 112      */
 113     @Override
 114     public void build(Content contentTree) throws DocletException {
 115         buildMethodDoc(contentTree);
 116     }
 117 
 118     /**
 119      * Build the method documentation.
 120      *
 121      * @param memberDetailsTree the content tree to which the documentation will be added
 122      * @throws DocletException if there is a problem while building the documentation
 123      */
 124     protected void buildMethodDoc(Content memberDetailsTree) throws DocletException {
 125         if (writer == null) {
 126             return;
 127         }
 128         if (hasMembersToDocument()) {
 129             Content methodDetailsTreeHeader = writer.getMethodDetailsTreeHeader(typeElement,
 130                     memberDetailsTree);
 131             Content methodDetailsTree = writer.getMemberTreeHeader();
 132 
 133             Element lastElement = methods.get(methods.size() - 1);
 134             for (Element method : methods) {
 135                 currentMethod = (ExecutableElement)method;
 136                 Content methodDocTree = writer.getMethodDocTreeHeader(currentMethod, methodDetailsTree);
 137 
 138                 buildSignature(methodDocTree);
 139                 buildDeprecationInfo(methodDocTree);
 140                 buildMethodComments(methodDocTree);
 141                 buildTagInfo(methodDocTree);
 142 
 143                 methodDetailsTree.add(writer.getMethodDoc(
 144                         methodDocTree, currentMethod == lastElement));
 145             }
 146             memberDetailsTree.add(writer.getMethodDetails(methodDetailsTreeHeader, methodDetailsTree));
 147         }
 148     }
 149 
 150     /**
 151      * Build the signature.
 152      *
 153      * @param methodDocTree the content tree to which the documentation will be added
 154      */
 155     protected void buildSignature(Content methodDocTree) {
 156         methodDocTree.add(writer.getSignature(currentMethod));
 157     }
 158 
 159     /**
 160      * Build the deprecation information.
 161      *
 162      * @param methodDocTree the content tree to which the documentation will be added
 163      */
 164     protected void buildDeprecationInfo(Content methodDocTree) {
 165         writer.addDeprecated(currentMethod, methodDocTree);
 166     }
 167 
 168     /**
 169      * Build the comments for the method.  Do nothing if
 170      * {@link BaseConfiguration#nocomment} is set to true.
 171      *
 172      * @param methodDocTree the content tree to which the documentation will be added
 173      */
 174     protected void buildMethodComments(Content methodDocTree) {
 175         if (!configuration.nocomment) {
 176             ExecutableElement method = currentMethod;
 177             if (utils.getFullBody(currentMethod).isEmpty()) {
 178                 DocFinder.Output docs = DocFinder.search(configuration,
 179                         new DocFinder.Input(utils, currentMethod));
 180                 if (docs.inlineTags != null && !docs.inlineTags.isEmpty())
 181                         method = (ExecutableElement)docs.holder;
 182             }
 183             TypeMirror containingType = method.getEnclosingElement().asType();
 184             writer.addComments(containingType, method, methodDocTree);
 185         }
 186     }
 187 
 188     /**
 189      * Build the tag information.
 190      *
 191      * @param methodDocTree the content tree to which the documentation will be added
 192      */
 193     protected void buildTagInfo(Content methodDocTree) {
 194         writer.addTags(currentMethod, methodDocTree);
 195     }
 196 
 197     /**
 198      * Return the method writer for this builder.
 199      *
 200      * @return the method writer for this builder.
 201      */
 202     public MethodWriter getWriter() {
 203         return writer;
 204     }
 205 }