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      */
FieldBuilder(Context context, TypeElement typeElement, FieldWriter writer)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      */
getInstance(Context context, TypeElement typeElement, FieldWriter writer)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
hasMembersToDocument()105     public boolean hasMembersToDocument() {
106         return !fields.isEmpty();
107     }
108 
109     /**
110      * {@inheritDoc}
111      */
112     @Override
build(Content contentTree)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      */
buildFieldDoc(Content memberDetailsTree)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             for (Element element : fields) {
132                 currentElement = (VariableElement)element;
133                 Content fieldDocTree = writer.getFieldDocTreeHeader(currentElement, fieldDetailsTree);
134 
135                 buildSignature(fieldDocTree);
136                 buildDeprecationInfo(fieldDocTree);
137                 buildFieldComments(fieldDocTree);
138                 buildTagInfo(fieldDocTree);
139 
140                 fieldDetailsTree.add(writer.getFieldDoc(fieldDocTree));
141             }
142             memberDetailsTree.add(
143                     writer.getFieldDetails(fieldDetailsTreeHeader, fieldDetailsTree));
144         }
145     }
146 
147     /**
148      * Build the signature.
149      *
150      * @param fieldDocTree the content tree to which the documentation will be added
151      */
buildSignature(Content fieldDocTree)152     protected void buildSignature(Content fieldDocTree) {
153         fieldDocTree.add(writer.getSignature(currentElement));
154     }
155 
156     /**
157      * Build the deprecation information.
158      *
159      * @param fieldDocTree the content tree to which the documentation will be added
160      */
buildDeprecationInfo(Content fieldDocTree)161     protected void buildDeprecationInfo(Content fieldDocTree) {
162         writer.addDeprecated(currentElement, fieldDocTree);
163     }
164 
165     /**
166      * Build the comments for the field.  Do nothing if
167      * {@link BaseConfiguration#nocomment} is set to true.
168      *
169      * @param fieldDocTree the content tree to which the documentation will be added
170      */
buildFieldComments(Content fieldDocTree)171     protected void buildFieldComments(Content fieldDocTree) {
172         if (!configuration.nocomment) {
173             writer.addComments(currentElement, fieldDocTree);
174         }
175     }
176 
177     /**
178      * Build the tag information.
179      *
180      * @param fieldDocTree the content tree to which the documentation will be added
181      */
buildTagInfo(Content fieldDocTree)182     protected void buildTagInfo(Content fieldDocTree) {
183         writer.addTags(currentElement, fieldDocTree);
184     }
185 
186     /**
187      * Return the field writer for this builder.
188      *
189      * @return the field writer for this builder.
190      */
getWriter()191     public FieldWriter getWriter() {
192         return writer;
193     }
194 }
195