1 /*
2  * Copyright (c) 2003, 2021, 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 
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.PropertyWriter;
38 
39 import static jdk.javadoc.internal.doclets.toolkit.util.VisibleMemberTable.Kind.*;
40 
41 /**
42  * Builds documentation for a property.
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 PropertyBuilder extends AbstractMemberBuilder {
50 
51     /**
52      * The writer to output the property documentation.
53      */
54     private final PropertyWriter writer;
55 
56     /**
57      * The list of properties being documented.
58      */
59     private final List<? extends Element> properties;
60 
61     /**
62      * The index of the current property that is being documented at this point
63      * in time.
64      */
65     private ExecutableElement currentProperty;
66 
67     /**
68      * Construct a new PropertyBuilder.
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      */
PropertyBuilder(Context context, TypeElement typeElement, PropertyWriter writer)74     private PropertyBuilder(Context context,
75             TypeElement typeElement,
76             PropertyWriter writer) {
77         super(context, typeElement);
78         this.writer = Objects.requireNonNull(writer);
79         properties = getVisibleMembers(PROPERTIES);
80     }
81 
82     /**
83      * Construct a new PropertyBuilder.
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 PropertyBuilder
89      */
getInstance(Context context, TypeElement typeElement, PropertyWriter writer)90     public static PropertyBuilder getInstance(Context context,
91             TypeElement typeElement,
92             PropertyWriter writer) {
93         return new PropertyBuilder(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
hasMembersToDocument()102     public boolean hasMembersToDocument() {
103         return !properties.isEmpty();
104     }
105 
106     @Override
build(Content contentTree)107     public void build(Content contentTree) throws DocletException {
108         buildPropertyDoc(contentTree);
109     }
110 
111     /**
112      * Build the property documentation.
113      *
114      * @param detailsList the content tree to which the documentation will be added
115      * @throws DocletException if there is a problem while building the documentation
116      */
buildPropertyDoc(Content detailsList)117     protected void buildPropertyDoc(Content detailsList) throws DocletException {
118         if (hasMembersToDocument()) {
119             Content propertyDetailsTreeHeader = writer.getPropertyDetailsTreeHeader(detailsList);
120             Content memberList = writer.getMemberList();
121 
122             for (Element property : properties) {
123                 currentProperty = (ExecutableElement)property;
124                 Content propertyDocTree = writer.getPropertyDocTreeHeader(currentProperty);
125 
126                 buildSignature(propertyDocTree);
127                 buildPropertyComments(propertyDocTree);
128                 buildTagInfo(propertyDocTree);
129 
130                 memberList.add(writer.getMemberListItem(propertyDocTree));
131             }
132             Content propertyDetails = writer.getPropertyDetails(propertyDetailsTreeHeader, memberList);
133             detailsList.add(propertyDetails);
134         }
135     }
136 
137     /**
138      * Build the signature.
139      *
140      * @param propertyDocTree the content tree to which the documentation will be added
141      */
buildSignature(Content propertyDocTree)142     protected void buildSignature(Content propertyDocTree) {
143         propertyDocTree.add(writer.getSignature(currentProperty));
144     }
145 
146     /**
147      * Build the deprecation information.
148      *
149      * @param propertyDocTree the content tree to which the documentation will be added
150      */
buildDeprecationInfo(Content propertyDocTree)151     protected void buildDeprecationInfo(Content propertyDocTree) {
152         writer.addDeprecated(currentProperty, propertyDocTree);
153     }
154 
155     /**
156      * Build the preview information.
157      *
158      * @param propertyDocTree the content tree to which the documentation will be added
159      */
buildPreviewInfo(Content propertyDocTree)160     protected void buildPreviewInfo(Content propertyDocTree) {
161         writer.addPreview(currentProperty, propertyDocTree);
162     }
163 
164     /**
165      * Build the comments for the property.  Do nothing if
166      * {@link BaseOptions#noComment()} is set to true.
167      *
168      * @param propertyDocTree the content tree to which the documentation will be added
169      */
buildPropertyComments(Content propertyDocTree)170     protected void buildPropertyComments(Content propertyDocTree) {
171         if (!options.noComment()) {
172             writer.addComments(currentProperty, propertyDocTree);
173         }
174     }
175 
176     /**
177      * Build the tag information.
178      *
179      * @param propertyDocTree the content tree to which the documentation will be added
180      */
buildTagInfo(Content propertyDocTree)181     protected void buildTagInfo(Content propertyDocTree) {
182         writer.addTags(currentProperty, propertyDocTree);
183     }
184 
185     /**
186      * Return the property writer for this builder.
187      *
188      * @return the property writer for this builder.
189      */
getWriter()190     public PropertyWriter getWriter() {
191         return writer;
192     }
193 }
194