1 /*
   2  * Copyright (c) 1997, 2019, 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.Arrays;
  29 import java.util.List;
  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.ContentBuilder;
  36 import jdk.javadoc.internal.doclets.formats.html.markup.Entity;
  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.formats.html.markup.Table;
  42 import jdk.javadoc.internal.doclets.formats.html.markup.TableHeader;
  43 import jdk.javadoc.internal.doclets.toolkit.ConstructorWriter;
  44 import jdk.javadoc.internal.doclets.toolkit.Content;
  45 import jdk.javadoc.internal.doclets.toolkit.MemberSummaryWriter;
  46 import jdk.javadoc.internal.doclets.toolkit.util.VisibleMemberTable;
  47 
  48 import static jdk.javadoc.internal.doclets.toolkit.util.VisibleMemberTable.Kind.CONSTRUCTORS;
  49 
  50 
  51 /**
  52  * Writes constructor documentation.
  53  *
  54  *  <p><b>This is NOT part of any supported API.
  55  *  If you write code that depends on this, you do so at your own risk.
  56  *  This code and its internal interfaces are subject to change or
  57  *  deletion without notice.</b>
  58  *
  59  * @author Robert Field
  60  * @author Atul M Dambalkar
  61  * @author Bhavesh Patel (Modified)
  62  */
  63 public class ConstructorWriterImpl extends AbstractExecutableMemberWriter
  64     implements ConstructorWriter, MemberSummaryWriter {
  65 
  66     private boolean foundNonPubConstructor = false;
  67 
  68     /**
  69      * Construct a new ConstructorWriterImpl.
  70      *
  71      * @param writer The writer for the class that the constructors belong to.
  72      * @param typeElement the class being documented.
  73      */
  74     public ConstructorWriterImpl(SubWriterHolderWriter writer, TypeElement typeElement) {
  75         super(writer, typeElement);
  76 
  77         VisibleMemberTable vmt = configuration.getVisibleMemberTable(typeElement);
  78         List<? extends Element> constructors = vmt.getVisibleMembers(CONSTRUCTORS);
  79 
  80         for (Element constructor : constructors) {
  81             if (utils.isProtected(constructor) || utils.isPrivate(constructor)) {
  82                 setFoundNonPubConstructor(true);
  83             }
  84         }
  85     }
  86 
  87     /**
  88      * Construct a new ConstructorWriterImpl.
  89      *
  90      * @param writer The writer for the class that the constructors belong to.
  91      */
  92     public ConstructorWriterImpl(SubWriterHolderWriter writer) {
  93         super(writer);
  94     }
  95 
  96     /**
  97      * {@inheritDoc}
  98      */
  99     @Override
 100     public Content getMemberSummaryHeader(TypeElement typeElement,
 101             Content memberSummaryTree) {
 102         memberSummaryTree.add(MarkerComments.START_OF_CONSTRUCTOR_SUMMARY);
 103         Content memberTree = new ContentBuilder();
 104         writer.addSummaryHeader(this, typeElement, memberTree);
 105         return memberTree;
 106     }
 107 
 108     /**
 109      * {@inheritDoc}
 110      */
 111     @Override
 112     public void addMemberTree(Content memberSummaryTree, Content memberTree) {
 113         writer.addMemberTree(HtmlStyle.constructorSummary, memberSummaryTree, memberTree);
 114     }
 115 
 116     /**
 117      * {@inheritDoc}
 118      */
 119     @Override
 120     public Content getConstructorDetailsTreeHeader(TypeElement typeElement,
 121             Content memberDetailsTree) {
 122         memberDetailsTree.add(MarkerComments.START_OF_CONSTRUCTOR_DETAILS);
 123         Content constructorDetailsTree = new ContentBuilder();
 124         Content heading = HtmlTree.HEADING(Headings.TypeDeclaration.DETAILS_HEADING,
 125                 contents.constructorDetailsLabel);
 126         constructorDetailsTree.add(links.createAnchor(
 127                 SectionName.CONSTRUCTOR_DETAIL));
 128         constructorDetailsTree.add(heading);
 129         return constructorDetailsTree;
 130     }
 131 
 132     /**
 133      * {@inheritDoc}
 134      */
 135     @Override
 136     public Content getConstructorDocTreeHeader(ExecutableElement constructor,
 137             Content constructorDetailsTree) {
 138         String erasureAnchor;
 139         Content constructorDocTree = new ContentBuilder();
 140         Content heading = new HtmlTree(Headings.TypeDeclaration.MEMBER_HEADING);
 141         if ((erasureAnchor = getErasureAnchor(constructor)) != null) {
 142             heading.add(links.createAnchor((erasureAnchor)));
 143         }
 144         heading.add(links.createAnchor(writer.getAnchor(constructor), new StringContent(name(constructor))));
 145         constructorDocTree.add(heading);
 146         return HtmlTree.SECTION(HtmlStyle.detail, constructorDocTree);
 147     }
 148 
 149     /**
 150      * {@inheritDoc}
 151      */
 152     @Override
 153     public Content getSignature(ExecutableElement constructor) {
 154         return new MemberSignature(constructor)
 155                 .addParameters(getParameters(constructor, true))
 156                 .addExceptions(getExceptions(constructor))
 157                 .toContent();
 158     }
 159 
 160     /**
 161      * {@inheritDoc}
 162      */
 163     @Override
 164     public void addDeprecated(ExecutableElement constructor, Content constructorDocTree) {
 165         addDeprecatedInfo(constructor, constructorDocTree);
 166     }
 167 
 168     /**
 169      * {@inheritDoc}
 170      */
 171     @Override
 172     public void addComments(ExecutableElement constructor, Content constructorDocTree) {
 173         addComment(constructor, constructorDocTree);
 174     }
 175 
 176     /**
 177      * {@inheritDoc}
 178      */
 179     @Override
 180     public void addTags(ExecutableElement constructor, Content constructorDocTree) {
 181         writer.addTagsInfo(constructor, constructorDocTree);
 182     }
 183 
 184     /**
 185      * {@inheritDoc}
 186      */
 187     @Override
 188     public Content getConstructorDetails(Content constructorDetailsTreeHeader, Content constructorDetailsTree) {
 189         Content constructorDetails = new ContentBuilder(constructorDetailsTreeHeader, constructorDetailsTree);
 190         return getMemberTree(HtmlTree.SECTION(HtmlStyle.constructorDetails, constructorDetails));
 191     }
 192 
 193     /**
 194      * {@inheritDoc}
 195      */
 196     @Override
 197     public Content getConstructorDoc(Content constructorDocTree) {
 198         return getMemberTree(constructorDocTree);
 199     }
 200 
 201     /**
 202      * Let the writer know whether a non public constructor was found.
 203      *
 204      * @param foundNonPubConstructor true if we found a non public constructor.
 205      */
 206     @Override
 207     public void setFoundNonPubConstructor(boolean foundNonPubConstructor) {
 208         this.foundNonPubConstructor = foundNonPubConstructor;
 209     }
 210 
 211     /**
 212      * {@inheritDoc}
 213      */
 214     @Override
 215     public void addSummaryLabel(Content memberTree) {
 216         Content label = HtmlTree.HEADING(Headings.TypeDeclaration.SUMMARY_HEADING,
 217                 contents.constructorSummaryLabel);
 218         memberTree.add(label);
 219     }
 220 
 221     /**
 222      * {@inheritDoc}
 223      */
 224     @Override
 225     public TableHeader getSummaryTableHeader(Element member) {
 226         if (foundNonPubConstructor) {
 227             return new TableHeader(contents.modifierLabel, contents.constructorLabel,
 228                     contents.descriptionLabel);
 229         } else {
 230             return new TableHeader(contents.constructorLabel, contents.descriptionLabel);
 231         }
 232     }
 233 
 234     @Override
 235     protected Table createSummaryTable() {
 236         List<HtmlStyle> bodyRowStyles;
 237         int rowScopeColumn;
 238 
 239         if (foundNonPubConstructor) {
 240             bodyRowStyles = Arrays.asList(HtmlStyle.colFirst, HtmlStyle.colConstructorName,
 241                     HtmlStyle.colLast);
 242             rowScopeColumn = 1;
 243         } else {
 244             bodyRowStyles = Arrays.asList(HtmlStyle.colConstructorName, HtmlStyle.colLast);
 245             rowScopeColumn = 0;
 246         }
 247 
 248         return new Table(HtmlStyle.memberSummary)
 249                 .setCaption(contents.constructors)
 250                 .setHeader(getSummaryTableHeader(typeElement))
 251                 .setRowScopeColumn(rowScopeColumn)
 252                 .setColumnStyles(bodyRowStyles);
 253     }
 254 
 255     /**
 256      * {@inheritDoc}
 257      */
 258     @Override
 259     public void addSummaryAnchor(TypeElement typeElement, Content memberTree) {
 260         memberTree.add(links.createAnchor(SectionName.CONSTRUCTOR_SUMMARY));
 261     }
 262 
 263     /**
 264      * {@inheritDoc}
 265      */
 266     @Override
 267     public void addInheritedSummaryAnchor(TypeElement typeElement, Content inheritedTree) {
 268     }
 269 
 270     /**
 271      * {@inheritDoc}
 272      */
 273     @Override
 274     public void addInheritedSummaryLabel(TypeElement typeElement, Content inheritedTree) {
 275     }
 276 
 277     /**
 278      * {@inheritDoc}
 279      */
 280     @Override
 281     protected void addSummaryType(Element member, Content tdSummaryType) {
 282         if (foundNonPubConstructor) {
 283             Content code = new HtmlTree(HtmlTag.CODE);
 284             if (utils.isProtected(member)) {
 285                 code.add("protected ");
 286             } else if (utils.isPrivate(member)) {
 287                 code.add("private ");
 288             } else if (utils.isPublic(member)) {
 289                 code.add(Entity.NO_BREAK_SPACE);
 290             } else {
 291                 code.add(
 292                         resources.getText("doclet.Package_private"));
 293             }
 294             tdSummaryType.add(code);
 295         }
 296     }
 297 
 298     @Override
 299     public Content getMemberTreeHeader(){
 300         return writer.getMemberTreeHeader();
 301     }
 302 }