FieldBuilder.java revision 3233:b5d08bc0d224
1/*
2 * Copyright (c) 2003, 2016, 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
26package jdk.javadoc.internal.doclets.toolkit.builders;
27
28import java.util.*;
29
30import javax.lang.model.element.Element;
31import javax.lang.model.element.TypeElement;
32import javax.lang.model.element.VariableElement;
33
34import jdk.javadoc.internal.doclets.toolkit.Configuration;
35import jdk.javadoc.internal.doclets.toolkit.Content;
36import jdk.javadoc.internal.doclets.toolkit.FieldWriter;
37import jdk.javadoc.internal.doclets.toolkit.util.VisibleMemberMap;
38
39
40/**
41 * Builds documentation for a field.
42 *
43 *  <p><b>This is NOT part of any supported API.
44 *  If you write code that depends on this, you do so at your own risk.
45 *  This code and its internal interfaces are subject to change or
46 *  deletion without notice.</b>
47 *
48 * @author Jamie Ho
49 * @author Bhavesh Patel (Modified)
50 */
51public class FieldBuilder extends AbstractMemberBuilder {
52
53    /**
54     * The class whose fields are being documented.
55     */
56    private final TypeElement typeElement;
57
58    /**
59     * The visible fields for the given class.
60     */
61    private final VisibleMemberMap visibleMemberMap;
62
63    /**
64     * The writer to output the field documentation.
65     */
66    private final FieldWriter writer;
67
68    /**
69     * The list of fields being documented.
70     */
71    private final SortedSet<Element> fields;
72
73    /**
74     * The index of the current field that is being documented at this point
75     * in time.
76     */
77    private VariableElement currentElement;
78
79    /**
80     * Construct a new FieldBuilder.
81     *
82     * @param context  the build context.
83     * @param typeElement the class whoses members are being documented.
84     * @param writer the doclet specific writer.
85     */
86    private FieldBuilder(Context context,
87            TypeElement typeElement,
88            FieldWriter writer) {
89        super(context);
90        this.typeElement = typeElement;
91        this.writer = writer;
92        visibleMemberMap =
93                new VisibleMemberMap(
94                typeElement,
95                VisibleMemberMap.Kind.FIELDS,
96                configuration);
97        fields = visibleMemberMap.getLeafClassMembers();
98    }
99
100    /**
101     * Construct a new FieldBuilder.
102     *
103     * @param context  the build context.
104     * @param typeElement the class whoses members are being documented.
105     * @param writer the doclet specific writer.
106     */
107    public static FieldBuilder getInstance(Context context,
108            TypeElement typeElement,
109            FieldWriter writer) {
110        return new FieldBuilder(context, typeElement, writer);
111    }
112
113    /**
114     * {@inheritDoc}
115     */
116    @Override
117    public String getName() {
118        return "FieldDetails";
119    }
120
121    /**
122     * Returns a list of fields that will be documented for the given class.
123     * This information can be used for doclet specific documentation
124     * generation.
125     *
126     * @param typeElement the {@link TypeElement} we want to check.
127     * @return a list of fields that will be documented.
128     */
129    public SortedSet<Element> members(TypeElement typeElement) {
130        return visibleMemberMap.getMembersFor(typeElement);
131    }
132
133    /**
134     * Returns the visible member map for the fields of this class.
135     *
136     * @return the visible member map for the fields of this class.
137     */
138    public VisibleMemberMap getVisibleMemberMap() {
139        return visibleMemberMap;
140    }
141
142    /**
143     * summaryOrder.size()
144     */
145    @Override
146    public boolean hasMembersToDocument() {
147        return !fields.isEmpty();
148    }
149
150    /**
151     * Build the field documentation.
152     *
153     * @param node the XML element that specifies which components to document
154     * @param memberDetailsTree the content tree to which the documentation will be added
155     */
156    public void buildFieldDoc(XMLNode node, Content memberDetailsTree) {
157        if (writer == null) {
158            return;
159        }
160        if (!fields.isEmpty()) {
161            Content fieldDetailsTree = writer.getFieldDetailsTreeHeader(typeElement, memberDetailsTree);
162            for (Element element : fields) {
163                currentElement = (VariableElement)element;
164                Content fieldDocTree = writer.getFieldDocTreeHeader(currentElement, fieldDetailsTree);
165                buildChildren(node, fieldDocTree);
166                fieldDetailsTree.addContent(writer.getFieldDoc(
167                        fieldDocTree, currentElement.equals(fields.last())));
168            }
169            memberDetailsTree.addContent(
170                    writer.getFieldDetails(fieldDetailsTree));
171        }
172    }
173
174    /**
175     * Build the signature.
176     *
177     * @param node the XML element that specifies which components to document
178     * @param fieldDocTree the content tree to which the documentation will be added
179     */
180    public void buildSignature(XMLNode node, Content fieldDocTree) {
181        fieldDocTree.addContent(writer.getSignature(currentElement));
182    }
183
184    /**
185     * Build the deprecation information.
186     *
187     * @param node the XML element that specifies which components to document
188     * @param fieldDocTree the content tree to which the documentation will be added
189     */
190    public void buildDeprecationInfo(XMLNode node, Content fieldDocTree) {
191        writer.addDeprecated(currentElement, fieldDocTree);
192    }
193
194    /**
195     * Build the comments for the field.  Do nothing if
196     * {@link Configuration#nocomment} is set to true.
197     *
198     * @param node the XML element that specifies which components to document
199     * @param fieldDocTree the content tree to which the documentation will be added
200     */
201    public void buildFieldComments(XMLNode node, Content fieldDocTree) {
202        if (!configuration.nocomment) {
203            writer.addComments(currentElement, fieldDocTree);
204        }
205    }
206
207    /**
208     * Build the tag information.
209     *
210     * @param node the XML element that specifies which components to document
211     * @param fieldDocTree the content tree to which the documentation will be added
212     */
213    public void buildTagInfo(XMLNode node, Content fieldDocTree) {
214        writer.addTags(currentElement, fieldDocTree);
215    }
216
217    /**
218     * Return the field writer for this builder.
219     *
220     * @return the field writer for this builder.
221     */
222    public FieldWriter getWriter() {
223        return writer;
224    }
225}
226