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