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