src/share/classes/com/sun/tools/doclets/internal/toolkit/util/links/LinkFactory.java

Wed, 23 Jan 2013 13:27:24 -0800

author
jjg
date
Wed, 23 Jan 2013 13:27:24 -0800
changeset 1521
71f35e4b93a5
parent 1359
25e14ad23cef
child 1691
f10cffab99b4
permissions
-rw-r--r--

8006775: JSR 308: Compiler changes in JDK8
Reviewed-by: jjg
Contributed-by: mernst@cs.washington.edu, wmdietl@cs.washington.edu, mpapi@csail.mit.edu, mahmood@notnoop.com

     1 /*
     2  * Copyright (c) 2003, 2013, 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.internal.toolkit.util.links;
    28 import com.sun.javadoc.*;
    30 /**
    31  * A factory that constructs links from given link information.
    32  *
    33  *  <p><b>This is NOT part of any supported API.
    34  *  If you write code that depends on this, you do so at your own risk.
    35  *  This code and its internal interfaces are subject to change or
    36  *  deletion without notice.</b>
    37  *
    38  * @author Jamie Ho
    39  * @since 1.5
    40  */
    41 public abstract class LinkFactory {
    43     /**
    44      * Return an empty instance of the link output object.
    45      *
    46      * @return an empty instance of the link output object.
    47      */
    48     protected abstract LinkOutput getOutputInstance();
    50     /**
    51      * Constructs a link from the given link information.
    52      *
    53      * @param linkInfo the information about the link.
    54      * @return the output of the link.
    55      */
    56     public LinkOutput getLinkOutput(LinkInfo linkInfo) {
    57         if (linkInfo.type != null) {
    58             Type type = linkInfo.type;
    59             LinkOutput linkOutput = getOutputInstance();
    60             if (type.isPrimitive()) {
    61                 //Just a primitive.
    62                 linkInfo.displayLength += type.typeName().length();
    63                 linkOutput.append(type.typeName());
    64             } else if (type.asAnnotatedType() != null) {
    65                 linkOutput.append(getTypeAnnotationLinks(linkInfo));
    66                 linkInfo.type = type.asAnnotatedType().underlyingType();
    67                 linkOutput.append(getLinkOutput(linkInfo));
    68                 return linkOutput;
    69             } else if (type.asWildcardType() != null) {
    70                 //Wildcard type.
    71                 linkInfo.isTypeBound = true;
    72                 linkInfo.displayLength += 1;
    73                 linkOutput.append("?");
    74                 WildcardType wildcardType = type.asWildcardType();
    75                 Type[] extendsBounds = wildcardType.extendsBounds();
    76                 for (int i = 0; i < extendsBounds.length; i++) {
    77                     linkInfo.displayLength += i > 0 ? 2 : 9;
    78                     linkOutput.append(i > 0 ? ", " : " extends ");
    79                     setBoundsLinkInfo(linkInfo, extendsBounds[i]);
    80                     linkOutput.append(getLinkOutput(linkInfo));
    81                 }
    82                 Type[] superBounds = wildcardType.superBounds();
    83                 for (int i = 0; i < superBounds.length; i++) {
    84                     linkInfo.displayLength += i > 0 ? 2 : 7;
    85                     linkOutput.append(i > 0 ? ", " : " super ");
    86                     setBoundsLinkInfo(linkInfo, superBounds[i]);
    87                     linkOutput.append(getLinkOutput(linkInfo));
    88                 }
    89             } else if (type.asTypeVariable()!= null) {
    90                 linkOutput.append(getTypeAnnotationLinks(linkInfo));
    91                 linkInfo.isTypeBound = true;
    92                 //A type variable.
    93                 Doc owner = type.asTypeVariable().owner();
    94                 if ((! linkInfo.excludeTypeParameterLinks) &&
    95                         owner instanceof ClassDoc) {
    96                     linkInfo.classDoc = (ClassDoc) owner;
    97                     linkInfo.label = type.typeName();
    98                     linkOutput.append(getClassLink(linkInfo));
    99                 } else {
   100                     //No need to link method type parameters.
   101                     linkInfo.displayLength += type.typeName().length();
   102                     linkOutput.append(type.typeName());
   103                 }
   105                 Type[] bounds = type.asTypeVariable().bounds();
   106                 if (! linkInfo.excludeTypeBounds) {
   107                     linkInfo.excludeTypeBounds = true;
   108                     for (int i = 0; i < bounds.length; i++) {
   109                         linkInfo.displayLength += i > 0 ? 2 : 9;
   110                         linkOutput.append(i > 0 ? " & " : " extends ");
   111                         setBoundsLinkInfo(linkInfo, bounds[i]);
   112                         linkOutput.append(getLinkOutput(linkInfo));
   113                     }
   114                 }
   115             } else if (type.asClassDoc() != null) {
   116                 //A class type.
   117                 if (linkInfo.isTypeBound &&
   118                         linkInfo.excludeTypeBoundsLinks) {
   119                     //Since we are excluding type parameter links, we should not
   120                     //be linking to the type bound.
   121                     linkInfo.displayLength += type.typeName().length();
   122                     linkOutput.append(type.typeName());
   123                     linkOutput.append(getTypeParameterLinks(linkInfo));
   124                     return linkOutput;
   125                 } else {
   126                     linkInfo.classDoc = type.asClassDoc();
   127                     linkOutput = getClassLink(linkInfo);
   128                     if (linkInfo.includeTypeAsSepLink) {
   129                         linkOutput.append(getTypeParameterLinks(linkInfo, false));
   130                     }
   131                 }
   132             }
   134             if (linkInfo.isVarArg) {
   135                 if (type.dimension().length() > 2) {
   136                     //Javadoc returns var args as array.
   137                     //Strip out the first [] from the var arg.
   138                     linkInfo.displayLength += type.dimension().length()-2;
   139                     linkOutput.append(type.dimension().substring(2));
   140                 }
   141                 linkInfo.displayLength += 3;
   142                 linkOutput.append("...");
   143             } else {
   144                 linkInfo.displayLength += type.dimension().length();
   145                 linkOutput.append(type.dimension());
   146             }
   147             return linkOutput;
   148         } else if (linkInfo.classDoc != null) {
   149             //Just a class link
   150             LinkOutput linkOutput = getClassLink(linkInfo);
   151             if (linkInfo.includeTypeAsSepLink) {
   152                 linkOutput.append(getTypeParameterLinks(linkInfo, false));
   153             }
   154             return linkOutput;
   155         } else {
   156             return null;
   157         }
   158     }
   160     private void setBoundsLinkInfo(LinkInfo linkInfo, Type bound) {
   161         linkInfo.classDoc = null;
   162         linkInfo.label = null;
   163         linkInfo.type = bound;
   164     }
   166     /**
   167      * Return the link to the given class.
   168      *
   169      * @param linkInfo the information about the link to construct.
   170      *
   171      * @return the link for the given class.
   172      */
   173     protected abstract LinkOutput getClassLink(LinkInfo linkInfo);
   175     /**
   176      * Return the link to the given type parameter.
   177      *
   178      * @param linkInfo     the information about the link to construct.
   179      * @param typeParam the type parameter to link to.
   180      */
   181     protected abstract LinkOutput getTypeParameterLink(LinkInfo linkInfo,
   182         Type typeParam);
   184     protected abstract LinkOutput getTypeAnnotationLink(LinkInfo linkInfo,
   185             AnnotationDesc annotation);
   187     /**
   188      * Return the links to the type parameters.
   189      *
   190      * @param linkInfo     the information about the link to construct.
   191      * @return the links to the type parameters.
   192      */
   193     public LinkOutput getTypeParameterLinks(LinkInfo linkInfo) {
   194         return getTypeParameterLinks(linkInfo, true);
   195     }
   197     /**
   198      * Return the links to the type parameters.
   199      *
   200      * @param linkInfo     the information about the link to construct.
   201      * @param isClassLabel true if this is a class label.  False if it is
   202      *                     the type parameters portion of the link.
   203      * @return the links to the type parameters.
   204      */
   205     public LinkOutput getTypeParameterLinks(LinkInfo linkInfo, boolean isClassLabel) {
   206         LinkOutput output = getOutputInstance();
   207         Type[] vars;
   208         if (linkInfo.executableMemberDoc != null) {
   209             vars = linkInfo.executableMemberDoc.typeParameters();
   210         } else if (linkInfo.type != null &&
   211                 linkInfo.type.asParameterizedType() != null){
   212             vars =  linkInfo.type.asParameterizedType().typeArguments();
   213         } else if (linkInfo.classDoc != null){
   214             vars = linkInfo.classDoc.typeParameters();
   215         } else {
   216             //Nothing to document.
   217             return output;
   218         }
   219         if (((linkInfo.includeTypeInClassLinkLabel && isClassLabel) ||
   220              (linkInfo.includeTypeAsSepLink && ! isClassLabel)
   221               )
   222             && vars.length > 0) {
   223             linkInfo.displayLength += 1;
   224             output.append(getLessThanString());
   225             for (int i = 0; i < vars.length; i++) {
   226                 if (i > 0) {
   227                     linkInfo.displayLength += 1;
   228                     output.append(",");
   229                 }
   230                 output.append(getTypeParameterLink(linkInfo, vars[i]));
   231             }
   232             linkInfo.displayLength += 1;
   233             output.append(getGreaterThanString());
   234         }
   235         return output;
   236     }
   238     public LinkOutput getTypeAnnotationLinks(LinkInfo linkInfo) {
   239         LinkOutput output = getOutputInstance();
   240         if (linkInfo.type.asAnnotatedType() == null)
   241             return output;
   242         AnnotationDesc[] annotations = linkInfo.type.asAnnotatedType().annotations();
   243         for (int i = 0; i < annotations.length; i++) {
   244             if (i > 0) {
   245                 linkInfo.displayLength += 1;
   246                 output.append(" ");
   247             }
   248             output.append(getTypeAnnotationLink(linkInfo, annotations[i]));
   249         }
   251         linkInfo.displayLength += 1;
   252         output.append(" ");
   253         return output;
   254     }
   256     /**
   257      * Return &amp;lt;, which is used in type parameters.  Override this
   258      * if your doclet uses something different.
   259      *
   260      * @return return &amp;lt;, which is used in type parameters.
   261      */
   262     protected String getLessThanString() {
   263         return "&lt;";
   264     }
   266     /**
   267      * Return &amp;gt;, which is used in type parameters.  Override this
   268      * if your doclet uses something different.
   269      *
   270      * @return return &amp;gt;, which is used in type parameters.
   271      */
   272     protected String getGreaterThanString() {
   273         return "&gt;";
   274     }
   275 }

mercurial