1/*
2 * Copyright (c) 1997, 2017, 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.formats.html;
27
28import java.util.*;
29
30import javax.lang.model.element.Element;
31import javax.lang.model.element.ExecutableElement;
32import javax.lang.model.element.TypeElement;
33
34import jdk.javadoc.internal.doclets.formats.html.markup.HtmlAttr;
35import jdk.javadoc.internal.doclets.formats.html.markup.HtmlConstants;
36import jdk.javadoc.internal.doclets.formats.html.markup.HtmlStyle;
37import jdk.javadoc.internal.doclets.formats.html.markup.HtmlTag;
38import jdk.javadoc.internal.doclets.formats.html.markup.HtmlTree;
39import jdk.javadoc.internal.doclets.formats.html.markup.StringContent;
40import jdk.javadoc.internal.doclets.toolkit.ConstructorWriter;
41import jdk.javadoc.internal.doclets.toolkit.Content;
42import jdk.javadoc.internal.doclets.toolkit.MemberSummaryWriter;
43import jdk.javadoc.internal.doclets.toolkit.util.VisibleMemberMap;
44
45
46/**
47 * Writes constructor documentation.
48 *
49 *  <p><b>This is NOT part of any supported API.
50 *  If you write code that depends on this, you do so at your own risk.
51 *  This code and its internal interfaces are subject to change or
52 *  deletion without notice.</b>
53 *
54 * @author Robert Field
55 * @author Atul M Dambalkar
56 * @author Bhavesh Patel (Modified)
57 */
58public class ConstructorWriterImpl extends AbstractExecutableMemberWriter
59    implements ConstructorWriter, MemberSummaryWriter {
60
61    private boolean foundNonPubConstructor = false;
62
63    /**
64     * Construct a new ConstructorWriterImpl.
65     *
66     * @param writer The writer for the class that the constructors belong to.
67     * @param typeElement the class being documented.
68     */
69    public ConstructorWriterImpl(SubWriterHolderWriter writer, TypeElement typeElement) {
70        super(writer, typeElement);
71
72        VisibleMemberMap visibleMemberMap = configuration.getVisibleMemberMap(
73                typeElement,
74                VisibleMemberMap.Kind.CONSTRUCTORS);
75        List<Element> constructors = visibleMemberMap.getMembers(typeElement);
76        for (Element constructor : constructors) {
77            if (utils.isProtected(constructor) || utils.isPrivate(constructor)) {
78                setFoundNonPubConstructor(true);
79            }
80        }
81    }
82
83    /**
84     * Construct a new ConstructorWriterImpl.
85     *
86     * @param writer The writer for the class that the constructors belong to.
87     */
88    public ConstructorWriterImpl(SubWriterHolderWriter writer) {
89        super(writer);
90    }
91
92    /**
93     * {@inheritDoc}
94     */
95    @Override
96    public Content getMemberSummaryHeader(TypeElement typeElement,
97            Content memberSummaryTree) {
98        memberSummaryTree.addContent(HtmlConstants.START_OF_CONSTRUCTOR_SUMMARY);
99        Content memberTree = writer.getMemberTreeHeader();
100        writer.addSummaryHeader(this, typeElement, memberTree);
101        return memberTree;
102    }
103
104    /**
105     * {@inheritDoc}
106     */
107    public void addMemberTree(Content memberSummaryTree, Content memberTree) {
108        writer.addMemberTree(memberSummaryTree, memberTree);
109    }
110
111    /**
112     * {@inheritDoc}
113     */
114    @Override
115    public Content getConstructorDetailsTreeHeader(TypeElement typeElement,
116            Content memberDetailsTree) {
117        memberDetailsTree.addContent(HtmlConstants.START_OF_CONSTRUCTOR_DETAILS);
118        Content constructorDetailsTree = writer.getMemberTreeHeader();
119        constructorDetailsTree.addContent(writer.getMarkerAnchor(
120                SectionName.CONSTRUCTOR_DETAIL));
121        Content heading = HtmlTree.HEADING(HtmlConstants.DETAILS_HEADING,
122                contents.constructorDetailsLabel);
123        constructorDetailsTree.addContent(heading);
124        return constructorDetailsTree;
125    }
126
127    /**
128     * {@inheritDoc}
129     */
130    @Override
131    public Content getConstructorDocTreeHeader(ExecutableElement constructor,
132            Content constructorDetailsTree) {
133        String erasureAnchor;
134        if ((erasureAnchor = getErasureAnchor(constructor)) != null) {
135            constructorDetailsTree.addContent(writer.getMarkerAnchor((erasureAnchor)));
136        }
137        constructorDetailsTree.addContent(
138                writer.getMarkerAnchor(writer.getAnchor(constructor)));
139        Content constructorDocTree = writer.getMemberTreeHeader();
140        Content heading = new HtmlTree(HtmlConstants.MEMBER_HEADING);
141        heading.addContent(name(constructor));
142        constructorDocTree.addContent(heading);
143        return constructorDocTree;
144    }
145
146    /**
147     * {@inheritDoc}
148     */
149    @Override
150    public Content getSignature(ExecutableElement constructor) {
151        Content pre = new HtmlTree(HtmlTag.PRE);
152        writer.addAnnotationInfo(constructor, pre);
153        int annotationLength = pre.charCount();
154        addModifiers(constructor, pre);
155        if (configuration.linksource) {
156            Content constructorName = new StringContent(name(constructor));
157            writer.addSrcLink(constructor, constructorName, pre);
158        } else {
159            addName(name(constructor), pre);
160        }
161        int indent = pre.charCount() - annotationLength;
162        addParameters(constructor, pre, indent);
163        addExceptions(constructor, pre, indent);
164        return pre;
165    }
166
167    /**
168     * {@inheritDoc}
169     */
170    @Override
171    public void setSummaryColumnStyleAndScope(HtmlTree thTree) {
172        thTree.addStyle(HtmlStyle.colConstructorName);
173        thTree.addAttr(HtmlAttr.SCOPE, "row");
174    }
175
176    /**
177     * {@inheritDoc}
178     */
179    @Override
180    public void addDeprecated(ExecutableElement constructor, Content constructorDocTree) {
181        addDeprecatedInfo(constructor, constructorDocTree);
182    }
183
184    /**
185     * {@inheritDoc}
186     */
187    @Override
188    public void addComments(ExecutableElement constructor, Content constructorDocTree) {
189        addComment(constructor, constructorDocTree);
190    }
191
192    /**
193     * {@inheritDoc}
194     */
195    @Override
196    public void addTags(ExecutableElement constructor, Content constructorDocTree) {
197        writer.addTagsInfo(constructor, constructorDocTree);
198    }
199
200    /**
201     * {@inheritDoc}
202     */
203    @Override
204    public Content getConstructorDetails(Content constructorDetailsTree) {
205        if (configuration.allowTag(HtmlTag.SECTION)) {
206            HtmlTree htmlTree = HtmlTree.SECTION(getMemberTree(constructorDetailsTree));
207            return htmlTree;
208        }
209        return getMemberTree(constructorDetailsTree);
210    }
211
212    /**
213     * {@inheritDoc}
214     */
215    @Override
216    public Content getConstructorDoc(Content constructorDocTree,
217            boolean isLastContent) {
218        return getMemberTree(constructorDocTree, isLastContent);
219    }
220
221    /**
222     * Let the writer know whether a non public constructor was found.
223     *
224     * @param foundNonPubConstructor true if we found a non public constructor.
225     */
226    @Override
227    public void setFoundNonPubConstructor(boolean foundNonPubConstructor) {
228        this.foundNonPubConstructor = foundNonPubConstructor;
229    }
230
231    /**
232     * {@inheritDoc}
233     */
234    @Override
235    public void addSummaryLabel(Content memberTree) {
236        Content label = HtmlTree.HEADING(HtmlConstants.SUMMARY_HEADING,
237                contents.constructorSummaryLabel);
238        memberTree.addContent(label);
239    }
240
241    /**
242     * {@inheritDoc}
243     */
244    @Override
245    public String getTableSummary() {
246        return resources.getText("doclet.Member_Table_Summary",
247                resources.getText("doclet.Constructor_Summary"),
248                resources.getText("doclet.constructors"));
249    }
250
251    /**
252     * {@inheritDoc}
253     */
254    @Override
255    public Content getCaption() {
256        return contents.constructors;
257    }
258
259    /**
260     * {@inheritDoc}
261     */
262    @Override
263    public List<String> getSummaryTableHeader(Element member) {
264        List<String> header = new ArrayList<>();
265        if (foundNonPubConstructor) {
266            header.add(resources.getText("doclet.Modifier"));
267        }
268        header.add(resources.getText("doclet.Constructor"));
269        header.add(resources.getText("doclet.Description"));
270        return header;
271    }
272
273    /**
274     * {@inheritDoc}
275     */
276    @Override
277    public void addSummaryAnchor(TypeElement typeElement, Content memberTree) {
278        memberTree.addContent(writer.getMarkerAnchor(
279                SectionName.CONSTRUCTOR_SUMMARY));
280    }
281
282    /**
283     * {@inheritDoc}
284     */
285    @Override
286    public void addInheritedSummaryAnchor(TypeElement typeElement, Content inheritedTree) {
287    }
288
289    /**
290     * {@inheritDoc}
291     */
292    @Override
293    public void addInheritedSummaryLabel(TypeElement typeElement, Content inheritedTree) {
294    }
295
296    /**
297     * {@inheritDoc}
298     */
299    @Override
300    protected Content getNavSummaryLink(TypeElement typeElement, boolean link) {
301        if (link) {
302            return writer.getHyperLink(SectionName.CONSTRUCTOR_SUMMARY,
303                    contents.navConstructor);
304        } else {
305            return contents.navConstructor;
306        }
307    }
308
309    /**
310     * {@inheritDoc}
311     */
312    @Override
313    protected void addNavDetailLink(boolean link, Content liNav) {
314        if (link) {
315            liNav.addContent(writer.getHyperLink(
316                    SectionName.CONSTRUCTOR_DETAIL,
317                    contents.navConstructor));
318        } else {
319            liNav.addContent(contents.navConstructor);
320        }
321    }
322
323    /**
324     * {@inheritDoc}
325     */
326    @Override
327    protected void addSummaryType(Element member, Content tdSummaryType) {
328        if (foundNonPubConstructor) {
329            Content code = new HtmlTree(HtmlTag.CODE);
330            if (utils.isProtected(member)) {
331                code.addContent("protected ");
332            } else if (utils.isPrivate(member)) {
333                code.addContent("private ");
334            } else if (utils.isPublic(member)) {
335                code.addContent(Contents.SPACE);
336            } else {
337                code.addContent(
338                        configuration.getText("doclet.Package_private"));
339            }
340            tdSummaryType.addContent(code);
341        }
342    }
343}
344