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.SortedSet;
  29 import java.util.TreeSet;
  30 
  31 import javax.lang.model.element.Element;
  32 import javax.lang.model.element.ExecutableElement;
  33 import javax.lang.model.element.TypeElement;
  34 import javax.lang.model.type.TypeMirror;
  35 
  36 import jdk.javadoc.internal.doclets.formats.html.markup.ContentBuilder;
  37 import jdk.javadoc.internal.doclets.formats.html.markup.Entity;
  38 import jdk.javadoc.internal.doclets.formats.html.markup.HtmlStyle;
  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.Content;
  44 import jdk.javadoc.internal.doclets.toolkit.MemberSummaryWriter;
  45 import jdk.javadoc.internal.doclets.toolkit.MethodWriter;
  46 import jdk.javadoc.internal.doclets.toolkit.util.Utils;
  47 import jdk.javadoc.internal.doclets.toolkit.util.VisibleMemberTable;
  48 
  49 /**
  50  * Writes method documentation in HTML format.
  51  *
  52  *  <p><b>This is NOT part of any supported API.
  53  *  If you write code that depends on this, you do so at your own risk.
  54  *  This code and its internal interfaces are subject to change or
  55  *  deletion without notice.</b>
  56  *
  57  * @author Robert Field
  58  * @author Atul M Dambalkar
  59  * @author Jamie Ho (rewrite)
  60  * @author Bhavesh Patel (Modified)
  61  */
  62 public class MethodWriterImpl extends AbstractExecutableMemberWriter
  63         implements MethodWriter, MemberSummaryWriter {
  64 
  65     /**
  66      * Construct a new MethodWriterImpl.
  67      *
  68      * @param writer the writer for the class that the methods belong to.
  69      * @param typeElement the class being documented.
  70      */
  71     public MethodWriterImpl(SubWriterHolderWriter writer, TypeElement typeElement) {
  72         super(writer, typeElement);
  73     }
  74 
  75     /**
  76      * Construct a new MethodWriterImpl.
  77      *
  78      * @param writer The writer for the class that the methods belong to.
  79      */
  80     public MethodWriterImpl(SubWriterHolderWriter writer) {
  81         super(writer);
  82     }
  83 
  84     /**
  85      * {@inheritDoc}
  86      */
  87     @Override
  88     public Content getMemberSummaryHeader(TypeElement typeElement, Content memberSummaryTree) {
  89         memberSummaryTree.add(MarkerComments.START_OF_METHOD_SUMMARY);
  90         Content memberTree = new ContentBuilder();
  91         writer.addSummaryHeader(this, typeElement, memberTree);
  92         return memberTree;
  93     }
  94 
  95     /**
  96      * {@inheritDoc}
  97      */
  98     @Override
  99     public void addMemberTree(Content memberSummaryTree, Content memberTree) {
 100         writer.addMemberTree(HtmlStyle.methodSummary, memberSummaryTree, memberTree);
 101     }
 102 
 103     /**
 104      * {@inheritDoc}
 105      */
 106     @Override
 107     public Content getMethodDetailsTreeHeader(TypeElement typeElement, Content memberDetailsTree) {
 108         memberDetailsTree.add(MarkerComments.START_OF_METHOD_DETAILS);
 109         Content methodDetailsTree = new ContentBuilder();
 110         Content heading = HtmlTree.HEADING(Headings.TypeDeclaration.DETAILS_HEADING,
 111                 contents.methodDetailLabel);
 112         methodDetailsTree.add(links.createAnchor(SectionName.METHOD_DETAIL));
 113         methodDetailsTree.add(heading);
 114         return methodDetailsTree;
 115     }
 116 
 117     /**
 118      * {@inheritDoc}
 119      */
 120     @Override
 121     public Content getMethodDocTreeHeader(ExecutableElement method, Content methodDetailsTree) {
 122         String erasureAnchor;
 123         Content methodDocTree = new ContentBuilder();
 124         Content heading = new HtmlTree(Headings.TypeDeclaration.MEMBER_HEADING);
 125         if ((erasureAnchor = getErasureAnchor(method)) != null) {
 126             heading.add(links.createAnchor((erasureAnchor)));
 127         }
 128         heading.add(links.createAnchor(writer.getAnchor(method), new StringContent(name(method))));
 129         methodDocTree.add(heading);
 130         return HtmlTree.SECTION(HtmlStyle.detail, methodDocTree);
 131     }
 132 
 133     /**
 134      * Get the signature for the given method.
 135      *
 136      * @param method the method being documented.
 137      * @return a content object for the signature
 138      */
 139     @Override
 140     public Content getSignature(ExecutableElement method) {
 141         return new MemberSignature(method)
 142                 .addTypeParameters(getTypeParameters(method))
 143                 .addReturnType(getReturnType(method))
 144                 .addParameters(getParameters(method, true))
 145                 .addExceptions(getExceptions(method))
 146                 .toContent();
 147     }
 148 
 149     /**
 150      * {@inheritDoc}
 151      */
 152     @Override
 153     public void addDeprecated(ExecutableElement method, Content methodDocTree) {
 154         addDeprecatedInfo(method, methodDocTree);
 155     }
 156 
 157     /**
 158      * {@inheritDoc}
 159      */
 160     @Override
 161     public void addComments(TypeMirror holderType, ExecutableElement method, Content methodDocTree) {
 162         TypeElement holder = utils.asTypeElement(holderType);
 163         if (!utils.getFullBody(method).isEmpty()) {
 164             if (holder.equals(typeElement) ||
 165                     !(utils.isPublic(holder) ||
 166                     utils.isLinkable(holder))) {
 167                 writer.addInlineComment(method, methodDocTree);
 168             } else {
 169                 Content link =
 170                         writer.getDocLink(LinkInfoImpl.Kind.EXECUTABLE_ELEMENT_COPY,
 171                         holder, method,
 172                         utils.isIncluded(holder)
 173                                 ? utils.getSimpleName(holder)
 174                                 : utils.getFullyQualifiedName(holder),
 175                             false);
 176                 Content codelLink = HtmlTree.CODE(link);
 177                 Content descfrmLabel = HtmlTree.SPAN(HtmlStyle.descfrmTypeLabel,
 178                         utils.isClass(holder)
 179                                 ? contents.descfrmClassLabel
 180                                 : contents.descfrmInterfaceLabel);
 181                 descfrmLabel.add(Entity.NO_BREAK_SPACE);
 182                 descfrmLabel.add(codelLink);
 183                 methodDocTree.add(HtmlTree.DIV(HtmlStyle.block, descfrmLabel));
 184                 writer.addInlineComment(method, methodDocTree);
 185             }
 186         }
 187     }
 188 
 189     /**
 190      * {@inheritDoc}
 191      */
 192     @Override
 193     public void addTags(ExecutableElement method, Content methodDocTree) {
 194         writer.addTagsInfo(method, methodDocTree);
 195     }
 196 
 197     /**
 198      * {@inheritDoc}
 199      */
 200     @Override
 201     public Content getMethodDetails(Content methodDetailsTreeHeader, Content methodDetailsTree) {
 202         Content methodDetails = new ContentBuilder(methodDetailsTreeHeader, methodDetailsTree);
 203         return getMemberTree(HtmlTree.SECTION(HtmlStyle.methodDetails, methodDetails));
 204     }
 205 
 206     /**
 207      * {@inheritDoc}
 208      */
 209     @Override
 210     public Content getMethodDoc(Content methodDocTree) {
 211         return getMemberTree(methodDocTree);
 212     }
 213 
 214     /**
 215      * {@inheritDoc}
 216      */
 217     @Override
 218     public void addSummaryLabel(Content memberTree) {
 219         Content label = HtmlTree.HEADING(Headings.TypeDeclaration.SUMMARY_HEADING,
 220                 contents.methodSummary);
 221         memberTree.add(label);
 222     }
 223 
 224     /**
 225      * {@inheritDoc}
 226      */
 227     @Override
 228     public TableHeader getSummaryTableHeader(Element member) {
 229         return new TableHeader(contents.modifierAndTypeLabel, contents.methodLabel,
 230                 contents.descriptionLabel);
 231     }
 232 
 233     @Override
 234     protected Table createSummaryTable() {
 235         return new Table(HtmlStyle.memberSummary)
 236                 .setHeader(getSummaryTableHeader(typeElement))
 237                 .setRowScopeColumn(1)
 238                 .setColumnStyles(HtmlStyle.colFirst, HtmlStyle.colSecond, HtmlStyle.colLast)
 239                 .setDefaultTab(resources.getText("doclet.All_Methods"))
 240                 .addTab(resources.getText("doclet.Static_Methods"), utils::isStatic)
 241                 .addTab(resources.getText("doclet.Instance_Methods"), e -> !utils.isStatic(e))
 242                 .addTab(resources.getText("doclet.Abstract_Methods"), utils::isAbstract)
 243                 .addTab(resources.getText("doclet.Concrete_Methods"),
 244                         e -> !utils.isAbstract(e) && !utils.isInterface(e.getEnclosingElement()))
 245                 .addTab(resources.getText("doclet.Default_Methods"), utils::isDefault)
 246                 .addTab(resources.getText("doclet.Deprecated_Methods"),
 247                         e -> utils.isDeprecated(e) || utils.isDeprecated(typeElement))
 248                 .setTabScript(i -> "show(" + i + ");");
 249     }
 250 
 251     /**
 252      * {@inheritDoc}
 253      */
 254     @Override
 255     public void addSummaryAnchor(TypeElement typeElement, Content memberTree) {
 256         memberTree.add(links.createAnchor(SectionName.METHOD_SUMMARY));
 257     }
 258 
 259     /**
 260      * {@inheritDoc}
 261      */
 262     @Override
 263     public void addInheritedSummaryAnchor(TypeElement typeElement, Content inheritedTree) {
 264         inheritedTree.add(links.createAnchor(
 265                 SectionName.METHODS_INHERITANCE, configuration.getClassName(typeElement)));
 266     }
 267 
 268     /**
 269      * {@inheritDoc}
 270      */
 271     @Override
 272     public void addInheritedSummaryLabel(TypeElement typeElement, Content inheritedTree) {
 273         Content classLink = writer.getPreQualifiedClassLink(
 274                 LinkInfoImpl.Kind.MEMBER, typeElement, false);
 275         Content label;
 276         if (configuration.summarizeOverriddenMethods) {
 277             label = new StringContent(utils.isClass(typeElement)
 278                     ? resources.getText("doclet.Methods_Declared_In_Class")
 279                     : resources.getText("doclet.Methods_Declared_In_Interface"));
 280         } else {
 281             label = new StringContent(utils.isClass(typeElement)
 282                     ? resources.getText("doclet.Methods_Inherited_From_Class")
 283                     : resources.getText("doclet.Methods_Inherited_From_Interface"));
 284         }
 285         Content labelHeading = HtmlTree.HEADING(Headings.TypeDeclaration.INHERITED_SUMMARY_HEADING,
 286                 label);
 287         labelHeading.add(Entity.NO_BREAK_SPACE);
 288         labelHeading.add(classLink);
 289         inheritedTree.add(labelHeading);
 290     }
 291 
 292     /**
 293      * {@inheritDoc}
 294      */
 295     @Override
 296     protected void addSummaryType(Element member, Content tdSummaryType) {
 297         ExecutableElement meth = (ExecutableElement)member;
 298         addModifierAndType(meth, utils.getReturnType(meth), tdSummaryType);
 299     }
 300 
 301     /**
 302      * {@inheritDoc}
 303      */
 304     protected static void addOverridden(HtmlDocletWriter writer,
 305             TypeMirror overriddenType, ExecutableElement method, Content dl) {
 306         if (writer.configuration.nocomment) {
 307             return;
 308         }
 309         Utils utils = writer.utils;
 310         Contents contents = writer.contents;
 311         TypeElement holder = utils.getEnclosingTypeElement(method);
 312         if (!(utils.isPublic(holder) ||
 313             utils.isLinkable(holder))) {
 314             //This is an implementation detail that should not be documented.
 315             return;
 316         }
 317         if (utils.isIncluded(holder) && ! utils.isIncluded(method)) {
 318             //The class is included but the method is not.  That means that it
 319             //is not visible so don't document this.
 320             return;
 321         }
 322         Content label = contents.overridesLabel;
 323         LinkInfoImpl.Kind context = LinkInfoImpl.Kind.METHOD_OVERRIDES;
 324 
 325         if (method != null) {
 326             if (utils.isAbstract(holder) && utils.isAbstract(method)){
 327                 //Abstract method is implemented from abstract class,
 328                 //not overridden
 329                 label = contents.specifiedByLabel;
 330                 context = LinkInfoImpl.Kind.METHOD_SPECIFIED_BY;
 331             }
 332             Content dt = HtmlTree.DT(HtmlTree.SPAN(HtmlStyle.overrideSpecifyLabel, label));
 333             dl.add(dt);
 334             Content overriddenTypeLink =
 335                     writer.getLink(new LinkInfoImpl(writer.configuration, context, overriddenType));
 336             Content codeOverridenTypeLink = HtmlTree.CODE(overriddenTypeLink);
 337             Content methlink = writer.getLink(
 338                     new LinkInfoImpl(writer.configuration, LinkInfoImpl.Kind.MEMBER,
 339                     holder)
 340                     .where(writer.links.getName(writer.getAnchor(method))).label(method.getSimpleName()));
 341             Content codeMethLink = HtmlTree.CODE(methlink);
 342             Content dd = HtmlTree.DD(codeMethLink);
 343             dd.add(Entity.NO_BREAK_SPACE);
 344             dd.add(writer.contents.inClass);
 345             dd.add(Entity.NO_BREAK_SPACE);
 346             dd.add(codeOverridenTypeLink);
 347             dl.add(dd);
 348         }
 349     }
 350 
 351     /**
 352      * {@inheritDoc}
 353      */
 354     protected static void addImplementsInfo(HtmlDocletWriter writer,
 355             ExecutableElement method, Content dl) {
 356         Utils utils = writer.utils;
 357         if (utils.isStatic(method) || writer.configuration.nocomment) {
 358             return;
 359         }
 360         Contents contents = writer.contents;
 361         VisibleMemberTable vmt = writer.configuration
 362                 .getVisibleMemberTable(utils.getEnclosingTypeElement(method));
 363         SortedSet<ExecutableElement> implementedMethods =
 364                 new TreeSet<>(utils.makeOverrideUseComparator());
 365         implementedMethods.addAll(vmt.getImplementedMethods(method));
 366         for (ExecutableElement implementedMeth : implementedMethods) {
 367             TypeMirror intfac = vmt.getImplementedMethodHolder(method, implementedMeth);
 368             intfac = utils.getDeclaredType(utils.getEnclosingTypeElement(method), intfac);
 369             Content intfaclink = writer.getLink(new LinkInfoImpl(
 370                     writer.configuration, LinkInfoImpl.Kind.METHOD_SPECIFIED_BY, intfac));
 371             Content codeIntfacLink = HtmlTree.CODE(intfaclink);
 372             Content dt = HtmlTree.DT(HtmlTree.SPAN(HtmlStyle.overrideSpecifyLabel, contents.specifiedByLabel));
 373             dl.add(dt);
 374             Content methlink = writer.getDocLink(
 375                     LinkInfoImpl.Kind.MEMBER, implementedMeth,
 376                     implementedMeth.getSimpleName(), false);
 377             Content codeMethLink = HtmlTree.CODE(methlink);
 378             Content dd = HtmlTree.DD(codeMethLink);
 379             dd.add(Entity.NO_BREAK_SPACE);
 380             dd.add(contents.inInterface);
 381             dd.add(Entity.NO_BREAK_SPACE);
 382             dd.add(codeIntfacLink);
 383             dl.add(dd);
 384         }
 385     }
 386 
 387     /**
 388      * Get the return type for the given method.
 389      *
 390      * @param method the method being documented.
 391      * @return content containing the return type
 392      */
 393     protected Content getReturnType(ExecutableElement method) {
 394         TypeMirror type = utils.getReturnType(method);
 395         if (type != null) {
 396             return writer.getLink(new LinkInfoImpl(configuration, LinkInfoImpl.Kind.RETURN_TYPE, type));
 397         }
 398         return new ContentBuilder();
 399     }
 400 
 401     @Override
 402     public Content getMemberTreeHeader(){
 403         return writer.getMemberTreeHeader();
 404     }
 405 }