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 
  26 package jdk.javadoc.internal.doclets.formats.html;
  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.formats.html.TableHeader;
  35 import jdk.javadoc.internal.doclets.formats.html.markup.HtmlAttr;
  36 import jdk.javadoc.internal.doclets.formats.html.markup.HtmlConstants;
  37 import jdk.javadoc.internal.doclets.formats.html.markup.HtmlStyle;
  38 import jdk.javadoc.internal.doclets.formats.html.markup.HtmlTag;
  39 import jdk.javadoc.internal.doclets.formats.html.markup.HtmlTree;
  40 import jdk.javadoc.internal.doclets.formats.html.markup.StringContent;
  41 import jdk.javadoc.internal.doclets.toolkit.ConstructorWriter;
  42 import jdk.javadoc.internal.doclets.toolkit.Content;
  43 import jdk.javadoc.internal.doclets.toolkit.MemberSummaryWriter;
  44 import jdk.javadoc.internal.doclets.toolkit.util.VisibleMemberMap;
  45 
  46 
  47 /**
  48  * Writes constructor documentation.
  49  *
  50  *  <p><b>This is NOT part of any supported API.
  51  *  If you write code that depends on this, you do so at your own risk.
  52  *  This code and its internal interfaces are subject to change or
  53  *  deletion without notice.</b>
  54  *
  55  * @author Robert Field
  56  * @author Atul M Dambalkar
  57  * @author Bhavesh Patel (Modified)
  58  */
  59 public class ConstructorWriterImpl extends AbstractExecutableMemberWriter
  60     implements ConstructorWriter, MemberSummaryWriter {
  61 
  62     private boolean foundNonPubConstructor = false;
  63 
  64     /**
  65      * Construct a new ConstructorWriterImpl.
  66      *
  67      * @param writer The writer for the class that the constructors belong to.
  68      * @param typeElement the class being documented.
  69      */
  70     public ConstructorWriterImpl(SubWriterHolderWriter writer, TypeElement typeElement) {
  71         super(writer, typeElement);
  72 
  73         VisibleMemberMap visibleMemberMap = configuration.getVisibleMemberMap(
  74                 typeElement,
  75                 VisibleMemberMap.Kind.CONSTRUCTORS);
  76         List<Element> constructors = visibleMemberMap.getMembers(typeElement);
  77         for (Element constructor : constructors) {
  78             if (utils.isProtected(constructor) || utils.isPrivate(constructor)) {
  79                 setFoundNonPubConstructor(true);
  80             }
  81         }
  82     }
  83 
  84     /**
  85      * Construct a new ConstructorWriterImpl.
  86      *
  87      * @param writer The writer for the class that the constructors belong to.
  88      */
  89     public ConstructorWriterImpl(SubWriterHolderWriter writer) {
  90         super(writer);
  91     }
  92 
  93     /**
  94      * {@inheritDoc}
  95      */
  96     @Override
  97     public Content getMemberSummaryHeader(TypeElement typeElement,
  98             Content memberSummaryTree) {
  99         memberSummaryTree.addContent(HtmlConstants.START_OF_CONSTRUCTOR_SUMMARY);
 100         Content memberTree = writer.getMemberTreeHeader();
 101         writer.addSummaryHeader(this, typeElement, memberTree);
 102         return memberTree;
 103     }
 104 
 105     /**
 106      * {@inheritDoc}
 107      */
 108     public void addMemberTree(Content memberSummaryTree, Content memberTree) {
 109         writer.addMemberTree(memberSummaryTree, memberTree);
 110     }
 111 
 112     /**
 113      * {@inheritDoc}
 114      */
 115     @Override
 116     public Content getConstructorDetailsTreeHeader(TypeElement typeElement,
 117             Content memberDetailsTree) {
 118         memberDetailsTree.addContent(HtmlConstants.START_OF_CONSTRUCTOR_DETAILS);
 119         Content constructorDetailsTree = writer.getMemberTreeHeader();
 120         constructorDetailsTree.addContent(writer.getMarkerAnchor(
 121                 SectionName.CONSTRUCTOR_DETAIL));
 122         Content heading = HtmlTree.HEADING(HtmlConstants.DETAILS_HEADING,
 123                 contents.constructorDetailsLabel);
 124         constructorDetailsTree.addContent(heading);
 125         return constructorDetailsTree;
 126     }
 127 
 128     /**
 129      * {@inheritDoc}
 130      */
 131     @Override
 132     public Content getConstructorDocTreeHeader(ExecutableElement constructor,
 133             Content constructorDetailsTree) {
 134         String erasureAnchor;
 135         if ((erasureAnchor = getErasureAnchor(constructor)) != null) {
 136             constructorDetailsTree.addContent(writer.getMarkerAnchor((erasureAnchor)));
 137         }
 138         constructorDetailsTree.addContent(
 139                 writer.getMarkerAnchor(writer.getAnchor(constructor)));
 140         Content constructorDocTree = writer.getMemberTreeHeader();
 141         Content heading = new HtmlTree(HtmlConstants.MEMBER_HEADING);
 142         heading.addContent(name(constructor));
 143         constructorDocTree.addContent(heading);
 144         return constructorDocTree;
 145     }
 146 
 147     /**
 148      * {@inheritDoc}
 149      */
 150     @Override
 151     public Content getSignature(ExecutableElement constructor) {
 152         Content pre = new HtmlTree(HtmlTag.PRE);
 153         writer.addAnnotationInfo(constructor, pre);
 154         int annotationLength = pre.charCount();
 155         addModifiers(constructor, pre);
 156         if (configuration.linksource) {
 157             Content constructorName = new StringContent(name(constructor));
 158             writer.addSrcLink(constructor, constructorName, pre);
 159         } else {
 160             addName(name(constructor), pre);
 161         }
 162         int indent = pre.charCount() - annotationLength;
 163         addParameters(constructor, pre, indent);
 164         addExceptions(constructor, pre, indent);
 165         return pre;
 166     }
 167 
 168     /**
 169      * {@inheritDoc}
 170      */
 171     @Override
 172     public void setSummaryColumnStyleAndScope(HtmlTree thTree) {
 173         thTree.addStyle(HtmlStyle.colConstructorName);
 174         thTree.addAttr(HtmlAttr.SCOPE, "row");
 175     }
 176 
 177     /**
 178      * {@inheritDoc}
 179      */
 180     @Override
 181     public void addDeprecated(ExecutableElement constructor, Content constructorDocTree) {
 182         addDeprecatedInfo(constructor, constructorDocTree);
 183     }
 184 
 185     /**
 186      * {@inheritDoc}
 187      */
 188     @Override
 189     public void addComments(ExecutableElement constructor, Content constructorDocTree) {
 190         addComment(constructor, constructorDocTree);
 191     }
 192 
 193     /**
 194      * {@inheritDoc}
 195      */
 196     @Override
 197     public void addTags(ExecutableElement constructor, Content constructorDocTree) {
 198         writer.addTagsInfo(constructor, constructorDocTree);
 199     }
 200 
 201     /**
 202      * {@inheritDoc}
 203      */
 204     @Override
 205     public Content getConstructorDetails(Content constructorDetailsTree) {
 206         if (configuration.allowTag(HtmlTag.SECTION)) {
 207             HtmlTree htmlTree = HtmlTree.SECTION(getMemberTree(constructorDetailsTree));
 208             return htmlTree;
 209         }
 210         return getMemberTree(constructorDetailsTree);
 211     }
 212 
 213     /**
 214      * {@inheritDoc}
 215      */
 216     @Override
 217     public Content getConstructorDoc(Content constructorDocTree,
 218             boolean isLastContent) {
 219         return getMemberTree(constructorDocTree, isLastContent);
 220     }
 221 
 222     /**
 223      * Let the writer know whether a non public constructor was found.
 224      *
 225      * @param foundNonPubConstructor true if we found a non public constructor.
 226      */
 227     @Override
 228     public void setFoundNonPubConstructor(boolean foundNonPubConstructor) {
 229         this.foundNonPubConstructor = foundNonPubConstructor;
 230     }
 231 
 232     /**
 233      * {@inheritDoc}
 234      */
 235     @Override
 236     public void addSummaryLabel(Content memberTree) {
 237         Content label = HtmlTree.HEADING(HtmlConstants.SUMMARY_HEADING,
 238                 contents.constructorSummaryLabel);
 239         memberTree.addContent(label);
 240     }
 241 
 242     /**
 243      * {@inheritDoc}
 244      */
 245     @Override
 246     public String getTableSummary() {
 247         return resources.getText("doclet.Member_Table_Summary",
 248                 resources.getText("doclet.Constructor_Summary"),
 249                 resources.getText("doclet.constructors"));
 250     }
 251 
 252     /**
 253      * {@inheritDoc}
 254      */
 255     @Override
 256     public Content getCaption() {
 257         return contents.constructors;
 258     }
 259 
 260     /**
 261      * {@inheritDoc}
 262      */
 263     @Override
 264     public TableHeader getSummaryTableHeader(Element member) {
 265         if (foundNonPubConstructor) {
 266             return new TableHeader(contents.modifierLabel, contents.constructorLabel,
 267                     contents.descriptionLabel);
 268         } else {
 269             return new TableHeader(contents.constructorLabel, contents.descriptionLabel);
 270         }
 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 }