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