1 /*
   2  * Copyright (c) 1997, 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 
  26 package jdk.javadoc.internal.doclets.formats.html;
  27 
  28 import java.io.*;
  29 import java.util.*;
  30 
  31 import javax.lang.model.element.Element;
  32 import javax.lang.model.element.ExecutableElement;
  33 import javax.lang.model.element.TypeElement;
  34 
  35 import jdk.javadoc.internal.doclets.formats.html.markup.HtmlConstants;
  36 import jdk.javadoc.internal.doclets.formats.html.markup.HtmlStyle;
  37 import jdk.javadoc.internal.doclets.formats.html.markup.HtmlTag;
  38 import jdk.javadoc.internal.doclets.formats.html.markup.HtmlTree;
  39 import jdk.javadoc.internal.doclets.formats.html.markup.StringContent;
  40 import jdk.javadoc.internal.doclets.toolkit.ConstructorWriter;
  41 import jdk.javadoc.internal.doclets.toolkit.Content;
  42 import jdk.javadoc.internal.doclets.toolkit.MemberSummaryWriter;
  43 import 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  */
  58 public 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 = new VisibleMemberMap(
  73                 typeElement,
  74                 VisibleMemberMap.Kind.CONSTRUCTORS, configuration);
  75         SortedSet<Element> constructors = visibleMemberMap.getMembersFor(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                 writer.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 setSummaryColumnStyle(HtmlTree tdTree) {
 172         if (foundNonPubConstructor)
 173             tdTree.addStyle(HtmlStyle.colLast);
 174         else
 175             tdTree.addStyle(HtmlStyle.colOne);
 176     }
 177 
 178     /**
 179      * {@inheritDoc}
 180      */
 181     @Override
 182     public void addDeprecated(ExecutableElement constructor, Content constructorDocTree) {
 183         addDeprecatedInfo(constructor, constructorDocTree);
 184     }
 185 
 186     /**
 187      * {@inheritDoc}
 188      */
 189     @Override
 190     public void addComments(ExecutableElement constructor, Content constructorDocTree) {
 191         addComment(constructor, constructorDocTree);
 192     }
 193 
 194     /**
 195      * {@inheritDoc}
 196      */
 197     @Override
 198     public void addTags(ExecutableElement constructor, Content constructorDocTree) {
 199         writer.addTagsInfo(constructor, constructorDocTree);
 200     }
 201 
 202     /**
 203      * {@inheritDoc}
 204      */
 205     @Override
 206     public Content getConstructorDetails(Content constructorDetailsTree) {
 207         if (configuration.allowTag(HtmlTag.SECTION)) {
 208             HtmlTree htmlTree = HtmlTree.SECTION(getMemberTree(constructorDetailsTree));
 209             return htmlTree;
 210         }
 211         return getMemberTree(constructorDetailsTree);
 212     }
 213 
 214     /**
 215      * {@inheritDoc}
 216      */
 217     @Override
 218     public Content getConstructorDoc(Content constructorDocTree,
 219             boolean isLastContent) {
 220         return getMemberTree(constructorDocTree, isLastContent);
 221     }
 222 
 223     /**
 224      * Close the writer.
 225      */
 226     @Override
 227     public void close() throws IOException {
 228         writer.close();
 229     }
 230 
 231     /**
 232      * Let the writer know whether a non public constructor was found.
 233      *
 234      * @param foundNonPubConstructor true if we found a non public constructor.
 235      */
 236     @Override
 237     public void setFoundNonPubConstructor(boolean foundNonPubConstructor) {
 238         this.foundNonPubConstructor = foundNonPubConstructor;
 239     }
 240 
 241     /**
 242      * {@inheritDoc}
 243      */
 244     @Override
 245     public void addSummaryLabel(Content memberTree) {
 246         Content label = HtmlTree.HEADING(HtmlConstants.SUMMARY_HEADING,
 247                 writer.getResource("doclet.Constructor_Summary"));
 248         memberTree.addContent(label);
 249     }
 250 
 251     /**
 252      * {@inheritDoc}
 253      */
 254     @Override
 255     public String getTableSummary() {
 256         return configuration.getText("doclet.Member_Table_Summary",
 257                 configuration.getText("doclet.Constructor_Summary"),
 258                 configuration.getText("doclet.constructors"));
 259     }
 260 
 261     /**
 262      * {@inheritDoc}
 263      */
 264     @Override
 265     public Content getCaption() {
 266         return configuration.getResource("doclet.Constructors");
 267     }
 268 
 269     /**
 270      * {@inheritDoc}
 271      */
 272     @Override
 273     public List<String> getSummaryTableHeader(Element member) {
 274         List<String> header = new ArrayList<>();
 275         if (foundNonPubConstructor) {
 276             header.add(configuration.getText("doclet.Modifier"));
 277         }
 278         header.add(configuration.getText("doclet.0_and_1",
 279                 configuration.getText("doclet.Constructor"),
 280                 configuration.getText("doclet.Description")));
 281         return header;
 282     }
 283 
 284     /**
 285      * {@inheritDoc}
 286      */
 287     @Override
 288     public void addSummaryAnchor(TypeElement typeElement, Content memberTree) {
 289         memberTree.addContent(writer.getMarkerAnchor(
 290                 SectionName.CONSTRUCTOR_SUMMARY));
 291     }
 292 
 293     /**
 294      * {@inheritDoc}
 295      */
 296     @Override
 297     public void addInheritedSummaryAnchor(TypeElement typeElement, Content inheritedTree) {
 298     }
 299 
 300     /**
 301      * {@inheritDoc}
 302      */
 303     @Override
 304     public void addInheritedSummaryLabel(TypeElement typeElement, Content inheritedTree) {
 305     }
 306 
 307     /**
 308      * {@inheritDoc}
 309      */
 310     @Override
 311     protected Content getNavSummaryLink(TypeElement typeElement, boolean link) {
 312         if (link) {
 313             return writer.getHyperLink(SectionName.CONSTRUCTOR_SUMMARY,
 314                     writer.getResource("doclet.navConstructor"));
 315         } else {
 316             return writer.getResource("doclet.navConstructor");
 317         }
 318     }
 319 
 320     /**
 321      * {@inheritDoc}
 322      */
 323     @Override
 324     protected void addNavDetailLink(boolean link, Content liNav) {
 325         if (link) {
 326             liNav.addContent(writer.getHyperLink(
 327                     SectionName.CONSTRUCTOR_DETAIL,
 328                     writer.getResource("doclet.navConstructor")));
 329         } else {
 330             liNav.addContent(writer.getResource("doclet.navConstructor"));
 331         }
 332     }
 333 
 334     /**
 335      * {@inheritDoc}
 336      */
 337     @Override
 338     protected void addSummaryType(Element member, Content tdSummaryType) {
 339         if (foundNonPubConstructor) {
 340             Content code = new HtmlTree(HtmlTag.CODE);
 341             if (utils.isProtected(member)) {
 342                 code.addContent("protected ");
 343             } else if (utils.isPrivate(member)) {
 344                 code.addContent("private ");
 345             } else if (utils.isPublic(member)) {
 346                 code.addContent(writer.getSpace());
 347             } else {
 348                 code.addContent(
 349                         configuration.getText("doclet.Package_private"));
 350             }
 351             tdSummaryType.addContent(code);
 352         }
 353     }
 354 }