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.TypeElement;
  32 import javax.lang.model.element.VariableElement;
  33 
  34 import jdk.javadoc.internal.doclets.toolkit.BaseConfiguration;
  35 import jdk.javadoc.internal.doclets.toolkit.Content;
  36 import jdk.javadoc.internal.doclets.toolkit.DocletException;
  37 import jdk.javadoc.internal.doclets.toolkit.FieldWriter;
  38 
  39 import static jdk.javadoc.internal.doclets.toolkit.util.VisibleMemberTable.Kind.*;
  40 
  41 /**
  42  * Builds documentation for a field.
  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 FieldBuilder extends AbstractMemberBuilder {
  53 
  54     /**
  55      * The writer to output the field documentation.
  56      */
  57     private final FieldWriter writer;
  58 
  59     /**
  60      * The list of fields being documented.
  61      */
  62     private final List<? extends Element> fields;
  63 
  64     /**
  65      * The index of the current field that is being documented at this point
  66      * in time.
  67      */
  68     private VariableElement currentElement;
  69 
  70     /**
  71      * Construct a new FieldBuilder.
  72      *
  73      * @param context  the build context.
  74      * @param typeElement the class whoses members are being documented.
  75      * @param writer the doclet specific writer.
  76      */
  77     private FieldBuilder(Context context,
  78                          TypeElement typeElement,
  79                          FieldWriter writer) {
  80         super(context, typeElement);
  81         this.writer = writer;
  82         fields = getVisibleMembers(FIELDS);
  83     }
  84 
  85     /**
  86      * Construct a new FieldBuilder.
  87      *
  88      * @param context  the build context.
  89      * @param typeElement the class whoses members are being documented.
  90      * @param writer the doclet specific writer.
  91      * @return the new FieldBuilder
  92      */
  93     public static FieldBuilder getInstance(Context context,
  94             TypeElement typeElement,
  95             FieldWriter writer) {
  96         return new FieldBuilder(context, typeElement, writer);
  97     }
  98 
  99     /**
 100      * Returns whether or not there are members to document.
 101      *
 102      * @return whether or not there are members to document
 103      */
 104     @Override
 105     public boolean hasMembersToDocument() {
 106         return !fields.isEmpty();
 107     }
 108 
 109     /**
 110      * {@inheritDoc}
 111      */
 112     @Override
 113     public void build(Content contentTree) throws DocletException {
 114         buildFieldDoc(contentTree);
 115     }
 116 
 117     /**
 118      * Build the field documentation.
 119      *
 120      * @param memberDetailsTree the content tree to which the documentation will be added
 121      * @throws DocletException if there is a problem while building the documentation
 122      */
 123     protected void buildFieldDoc(Content memberDetailsTree) throws DocletException {
 124         if (writer == null) {
 125             return;
 126         }
 127         if (!fields.isEmpty()) {
 128             Content fieldDetailsTreeHeader = writer.getFieldDetailsTreeHeader(typeElement, memberDetailsTree);
 129             Content fieldDetailsTree = writer.getMemberTreeHeader();
 130 
 131             Element lastElement = fields.get(fields.size() - 1);
 132             for (Element element : fields) {
 133                 currentElement = (VariableElement)element;
 134                 Content fieldDocTree = writer.getFieldDocTreeHeader(currentElement, fieldDetailsTree);
 135 
 136                 buildSignature(fieldDocTree);
 137                 buildDeprecationInfo(fieldDocTree);
 138                 buildFieldComments(fieldDocTree);
 139                 buildTagInfo(fieldDocTree);
 140 
 141                 fieldDetailsTree.add(writer.getFieldDoc(
 142                         fieldDocTree, currentElement == lastElement));
 143             }
 144             memberDetailsTree.add(
 145                     writer.getFieldDetails(fieldDetailsTreeHeader, fieldDetailsTree));
 146         }
 147     }
 148 
 149     /**
 150      * Build the signature.
 151      *
 152      * @param fieldDocTree the content tree to which the documentation will be added
 153      */
 154     protected void buildSignature(Content fieldDocTree) {
 155         fieldDocTree.add(writer.getSignature(currentElement));
 156     }
 157 
 158     /**
 159      * Build the deprecation information.
 160      *
 161      * @param fieldDocTree the content tree to which the documentation will be added
 162      */
 163     protected void buildDeprecationInfo(Content fieldDocTree) {
 164         writer.addDeprecated(currentElement, fieldDocTree);
 165     }
 166 
 167     /**
 168      * Build the comments for the field.  Do nothing if
 169      * {@link BaseConfiguration#nocomment} is set to true.
 170      *
 171      * @param fieldDocTree the content tree to which the documentation will be added
 172      */
 173     protected void buildFieldComments(Content fieldDocTree) {
 174         if (!configuration.nocomment) {
 175             writer.addComments(currentElement, fieldDocTree);
 176         }
 177     }
 178 
 179     /**
 180      * Build the tag information.
 181      *
 182      * @param fieldDocTree the content tree to which the documentation will be added
 183      */
 184     protected void buildTagInfo(Content fieldDocTree) {
 185         writer.addTags(currentElement, fieldDocTree);
 186     }
 187 
 188     /**
 189      * Return the field writer for this builder.
 190      *
 191      * @return the field writer for this builder.
 192      */
 193     public FieldWriter getWriter() {
 194         return writer;
 195     }
 196 }