src/share/classes/com/sun/tools/doclets/formats/html/AbstractExecutableMemberWriter.java

Thu, 15 Nov 2012 19:54:20 -0800

author
jjg
date
Thu, 15 Nov 2012 19:54:20 -0800
changeset 1412
400a4e8accd3
parent 1410
bfec2a1cc869
child 1521
71f35e4b93a5
permissions
-rw-r--r--

8002079: update DocFile to use a JavaFileManager
Reviewed-by: darcy

     1 /*
     2  * Copyright (c) 1997, 2012, 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  */
    26 package com.sun.tools.doclets.formats.html;
    28 import com.sun.javadoc.*;
    29 import com.sun.tools.doclets.formats.html.markup.*;
    30 import com.sun.tools.doclets.internal.toolkit.*;
    31 import com.sun.tools.doclets.internal.toolkit.util.*;
    33 /**
    34  * Print method and constructor info.
    35  *
    36  *  <p><b>This is NOT part of any supported API.
    37  *  If you write code that depends on this, you do so at your own risk.
    38  *  This code and its internal interfaces are subject to change or
    39  *  deletion without notice.</b>
    40  *
    41  * @author Robert Field
    42  * @author Atul M Dambalkar
    43  * @author Bhavesh Patel (Modified)
    44  */
    45 public abstract class AbstractExecutableMemberWriter extends AbstractMemberWriter {
    47     public AbstractExecutableMemberWriter(SubWriterHolderWriter writer,
    48             ClassDoc classdoc) {
    49         super(writer, classdoc);
    50     }
    52     public AbstractExecutableMemberWriter(SubWriterHolderWriter writer) {
    53         super(writer);
    54     }
    56     /**
    57      * Add the type parameters for the executable member.
    58      *
    59      * @param member the member to write type parameters for.
    60      * @param htmltree the content tree to which the parameters will be added.
    61      * @return the display length required to write this information.
    62      */
    63     protected int addTypeParameters(ExecutableMemberDoc member, Content htmltree) {
    64         LinkInfoImpl linkInfo = new LinkInfoImpl(configuration,
    65             LinkInfoImpl.CONTEXT_MEMBER_TYPE_PARAMS, member, false);
    66         String typeParameters = writer.getTypeParameterLinks(linkInfo);
    67         if (linkInfo.displayLength > 0) {
    68             Content linkContent = new RawHtml(typeParameters);
    69             htmltree.addContent(linkContent);
    70             htmltree.addContent(writer.getSpace());
    71             writer.displayLength += linkInfo.displayLength + 1;
    72         }
    73         return linkInfo.displayLength;
    74     }
    76     /**
    77      * {@inheritDoc}
    78      */
    79     protected Content getDeprecatedLink(ProgramElementDoc member) {
    80         ExecutableMemberDoc emd = (ExecutableMemberDoc)member;
    81         return writer.getDocLink(LinkInfoImpl.CONTEXT_MEMBER, (MemberDoc) emd,
    82                 emd.qualifiedName() + emd.flatSignature());
    83     }
    85     /**
    86      * Add the summary link for the member.
    87      *
    88      * @param context the id of the context where the link will be printed
    89      * @param cd the classDoc that we should link to
    90      * @param member the member being linked to
    91      * @param tdSummary the content tree to which the link will be added
    92      */
    93     protected void addSummaryLink(int context, ClassDoc cd, ProgramElementDoc member,
    94             Content tdSummary) {
    95         ExecutableMemberDoc emd = (ExecutableMemberDoc)member;
    96         String name = emd.name();
    97         Content strong = HtmlTree.STRONG(new RawHtml(
    98                 writer.getDocLink(context, cd, (MemberDoc) emd,
    99                 name, false)));
   100         Content code = HtmlTree.CODE(strong);
   101         writer.displayLength = name.length();
   102         addParameters(emd, false, code);
   103         tdSummary.addContent(code);
   104     }
   106     /**
   107      * Add the inherited summary link for the member.
   108      *
   109      * @param cd the classDoc that we should link to
   110      * @param member the member being linked to
   111      * @param linksTree the content tree to which the link will be added
   112      */
   113     protected void addInheritedSummaryLink(ClassDoc cd,
   114             ProgramElementDoc member, Content linksTree) {
   115         linksTree.addContent(new RawHtml(
   116                 writer.getDocLink(LinkInfoImpl.CONTEXT_MEMBER, cd, (MemberDoc) member,
   117                 member.name(), false)));
   118     }
   120     /**
   121      * Add the parameter for the executable member.
   122      *
   123      * @param member the member to write parameter for.
   124      * @param param the parameter that needs to be written.
   125      * @param isVarArg true if this is a link to var arg.
   126      * @param tree the content tree to which the parameter information will be added.
   127      */
   128     protected void addParam(ExecutableMemberDoc member, Parameter param,
   129         boolean isVarArg, Content tree) {
   130         if (param.type() != null) {
   131             Content link = new RawHtml(writer.getLink(new LinkInfoImpl(
   132                     configuration, LinkInfoImpl.CONTEXT_EXECUTABLE_MEMBER_PARAM,
   133                     param.type(), isVarArg)));
   134             tree.addContent(link);
   135         }
   136         if(param.name().length() > 0) {
   137             tree.addContent(writer.getSpace());
   138             tree.addContent(param.name());
   139         }
   140     }
   142     /**
   143      * Add all the parameters for the executable member.
   144      *
   145      * @param member the member to write parameters for.
   146      * @param htmltree the content tree to which the parameters information will be added.
   147      */
   148     protected void addParameters(ExecutableMemberDoc member, Content htmltree) {
   149         addParameters(member, true, htmltree);
   150     }
   152     /**
   153      * Add all the parameters for the executable member.
   154      *
   155      * @param member the member to write parameters for.
   156      * @param includeAnnotations true if annotation information needs to be added.
   157      * @param htmltree the content tree to which the parameters information will be added.
   158      */
   159     protected void addParameters(ExecutableMemberDoc member,
   160             boolean includeAnnotations, Content htmltree) {
   161         htmltree.addContent("(");
   162         Parameter[] params = member.parameters();
   163         String indent = makeSpace(writer.displayLength);
   164         if (configuration.linksource) {
   165             //add spaces to offset indentation changes caused by link.
   166             indent+= makeSpace(member.name().length());
   167         }
   168         int paramstart;
   169         for (paramstart = 0; paramstart < params.length; paramstart++) {
   170             Parameter param = params[paramstart];
   171             if (!param.name().startsWith("this$")) {
   172                 if (includeAnnotations) {
   173                     boolean foundAnnotations =
   174                             writer.addAnnotationInfo(indent.length(),
   175                             member, param, htmltree);
   176                     if (foundAnnotations) {
   177                         htmltree.addContent(DocletConstants.NL);
   178                         htmltree.addContent(indent);
   179                     }
   180                 }
   181                 addParam(member, param,
   182                     (paramstart == params.length - 1) && member.isVarArgs(), htmltree);
   183                 break;
   184             }
   185         }
   187         for (int i = paramstart + 1; i < params.length; i++) {
   188             htmltree.addContent(",");
   189             htmltree.addContent(DocletConstants.NL);
   190             htmltree.addContent(indent);
   191             if (includeAnnotations) {
   192                 boolean foundAnnotations =
   193                         writer.addAnnotationInfo(indent.length(), member, params[i],
   194                         htmltree);
   195                 if (foundAnnotations) {
   196                     htmltree.addContent(DocletConstants.NL);
   197                     htmltree.addContent(indent);
   198                 }
   199             }
   200             addParam(member, params[i], (i == params.length - 1) && member.isVarArgs(),
   201                     htmltree);
   202         }
   203         htmltree.addContent(")");
   204     }
   206     /**
   207      * Add exceptions for the executable member.
   208      *
   209      * @param member the member to write exceptions for.
   210      * @param htmltree the content tree to which the exceptions information will be added.
   211      */
   212     protected void addExceptions(ExecutableMemberDoc member, Content htmltree) {
   213         Type[] exceptions = member.thrownExceptionTypes();
   214         if(exceptions.length > 0) {
   215             LinkInfoImpl memberTypeParam = new LinkInfoImpl(configuration,
   216                     LinkInfoImpl.CONTEXT_MEMBER, member, false);
   217             int retlen = getReturnTypeLength(member);
   218             writer.getTypeParameterLinks(memberTypeParam);
   219             retlen += memberTypeParam.displayLength == 0 ?
   220                 0 : memberTypeParam.displayLength + 1;
   221             String indent = makeSpace(modifierString(member).length() +
   222                     member.name().length() + retlen - 4);
   223             htmltree.addContent(DocletConstants.NL);
   224             htmltree.addContent(indent);
   225             htmltree.addContent("throws ");
   226             indent += "       ";
   227             Content link = new RawHtml(writer.getLink(new LinkInfoImpl(configuration,
   228                     LinkInfoImpl.CONTEXT_MEMBER, exceptions[0])));
   229             htmltree.addContent(link);
   230             for(int i = 1; i < exceptions.length; i++) {
   231                 htmltree.addContent(",");
   232                 htmltree.addContent(DocletConstants.NL);
   233                 htmltree.addContent(indent);
   234                 Content exceptionLink = new RawHtml(writer.getLink(new LinkInfoImpl(
   235                         configuration, LinkInfoImpl.CONTEXT_MEMBER, exceptions[i])));
   236                 htmltree.addContent(exceptionLink);
   237             }
   238         }
   239     }
   241     protected int getReturnTypeLength(ExecutableMemberDoc member) {
   242         if (member instanceof MethodDoc) {
   243             MethodDoc method = (MethodDoc)member;
   244             Type rettype = method.returnType();
   245             if (rettype.isPrimitive()) {
   246                 return rettype.typeName().length() +
   247                        rettype.dimension().length();
   248             } else {
   249                 LinkInfoImpl linkInfo = new LinkInfoImpl(configuration,
   250                     LinkInfoImpl.CONTEXT_MEMBER, rettype);
   251                 writer.getLink(linkInfo);
   252                 return linkInfo.displayLength;
   253             }
   254         } else {   // it's a constructordoc
   255             return -1;
   256         }
   257     }
   259     protected ClassDoc implementsMethodInIntfac(MethodDoc method,
   260                                                 ClassDoc[] intfacs) {
   261         for (int i = 0; i < intfacs.length; i++) {
   262             MethodDoc[] methods = intfacs[i].methods();
   263             if (methods.length > 0) {
   264                 for (int j = 0; j < methods.length; j++) {
   265                     if (methods[j].name().equals(method.name()) &&
   266                           methods[j].signature().equals(method.signature())) {
   267                         return intfacs[i];
   268                     }
   269                 }
   270             }
   271         }
   272         return null;
   273     }
   275     /**
   276      * For backward compatibility, include an anchor using the erasures of the
   277      * parameters.  NOTE:  We won't need this method anymore after we fix
   278      * see tags so that they use the type instead of the erasure.
   279      *
   280      * @param emd the ExecutableMemberDoc to anchor to.
   281      * @return the 1.4.x style anchor for the ExecutableMemberDoc.
   282      */
   283     protected String getErasureAnchor(ExecutableMemberDoc emd) {
   284         StringBuilder buf = new StringBuilder(emd.name() + "(");
   285         Parameter[] params = emd.parameters();
   286         boolean foundTypeVariable = false;
   287         for (int i = 0; i < params.length; i++) {
   288             if (i > 0) {
   289                 buf.append(",");
   290             }
   291             Type t = params[i].type();
   292             foundTypeVariable = foundTypeVariable || t.asTypeVariable() != null;
   293             buf.append(t.isPrimitive() ?
   294                 t.typeName() : t.asClassDoc().qualifiedName());
   295             buf.append(t.dimension());
   296         }
   297         buf.append(")");
   298         return foundTypeVariable ? buf.toString() : null;
   299     }
   300 }

mercurial