1 /*
   2  * Copyright (c) 2003, 2017, 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.toolkit.util.links;
  27 
  28 import java.util.ArrayList;
  29 import java.util.List;
  30 
  31 import javax.lang.model.element.Element;
  32 import javax.lang.model.element.TypeElement;
  33 import javax.lang.model.element.TypeParameterElement;
  34 import javax.lang.model.type.ArrayType;
  35 import javax.lang.model.type.DeclaredType;
  36 import javax.lang.model.type.TypeMirror;
  37 import javax.lang.model.type.TypeVariable;
  38 import javax.lang.model.type.WildcardType;
  39 import javax.lang.model.util.SimpleTypeVisitor9;
  40 
  41 import jdk.javadoc.internal.doclets.toolkit.Content;
  42 import jdk.javadoc.internal.doclets.toolkit.util.Utils;
  43 
  44 /**
  45  * A factory that constructs links from given link information.
  46  *
  47  *  <p><b>This is NOT part of any supported API.
  48  *  If you write code that depends on this, you do so at your own risk.
  49  *  This code and its internal interfaces are subject to change or
  50  *  deletion without notice.</b>
  51  *
  52  * @author Jamie Ho
  53  */
  54 public abstract class LinkFactory {
  55     protected final Utils utils;
  56 
  57     protected LinkFactory(Utils utils) {
  58         this.utils = utils;
  59     }
  60 
  61     /**
  62      * Return an empty instance of a content object.
  63      *
  64      * @return an empty instance of a content object.
  65      */
  66     protected abstract Content newContent();
  67 
  68     /**
  69      * Constructs a link from the given link information.
  70      *
  71      * @param linkInfo the information about the link.
  72      * @return the output of the link.
  73      */
  74     public Content getLink(LinkInfo linkInfo) {
  75         if (linkInfo.type != null) {
  76             SimpleTypeVisitor9<Content, LinkInfo> linkVisitor =
  77                     new SimpleTypeVisitor9<Content, LinkInfo>() {
  78 
  79                 TypeMirror componentType = utils.getComponentType(linkInfo.type);
  80                 Content link = newContent();
  81 
  82                 // handles primitives, no types and error types
  83                 @Override
  84                 protected Content defaultAction(TypeMirror type, LinkInfo linkInfo) {
  85                     link.addContent(utils.getTypeName(type, false));
  86                     return link;
  87                 }
  88 
  89                 int currentDepth = 0;
  90                 @Override
  91                 public Content visitArray(ArrayType type, LinkInfo linkInfo) {
  92                     // keep track of the dimension depth and replace the last dimension
  93                     // specifier with vararags, when the stack is fully unwound.
  94                     currentDepth++;
  95                     linkInfo.type = type.getComponentType();
  96                     visit(linkInfo.type, linkInfo);
  97                     currentDepth--;
  98                     if (utils.isAnnotated(type)) {
  99                         linkInfo.type = type;
 100                         link.addContent(" ");
 101                         link.addContent(getTypeAnnotationLinks(linkInfo));
 102                     }
 103                     // use vararg if required
 104                     if (linkInfo.isVarArg && currentDepth == 0) {
 105                         link.addContent("...");
 106                     } else {
 107                         link.addContent("[]");
 108                     }
 109                     return link;
 110                 }
 111 
 112                 @Override
 113                 public Content visitWildcard(WildcardType type, LinkInfo linkInfo) {
 114                     linkInfo.isTypeBound = true;
 115                     link.addContent("?");
 116                     TypeMirror extendsBound = type.getExtendsBound();
 117                     if (extendsBound != null) {
 118                         link.addContent(" extends ");
 119                         setBoundsLinkInfo(linkInfo, extendsBound);
 120                         link.addContent(getLink(linkInfo));
 121                     }
 122                     TypeMirror superBound = type.getSuperBound();
 123                     if (superBound != null) {
 124                         link.addContent(" super ");
 125                         setBoundsLinkInfo(linkInfo, superBound);
 126                         link.addContent(getLink(linkInfo));
 127                     }
 128                     return link;
 129                 }
 130 
 131                 @Override
 132                 public Content visitTypeVariable(TypeVariable type, LinkInfo linkInfo) {
 133                     link.addContent(getTypeAnnotationLinks(linkInfo));
 134                     linkInfo.isTypeBound = true;
 135                     TypeVariable typevariable = (utils.isArrayType(type))
 136                             ? (TypeVariable) componentType
 137                             : type;
 138                     Element owner = typevariable.asElement().getEnclosingElement();
 139                     if ((!linkInfo.excludeTypeParameterLinks) && utils.isTypeElement(owner)) {
 140                         linkInfo.typeElement = (TypeElement) owner;
 141                         Content label = newContent();
 142                         label.addContent(utils.getTypeName(type, false));
 143                         linkInfo.label = label;
 144                         link.addContent(getClassLink(linkInfo));
 145                     } else {
 146                         // No need to link method type parameters.
 147                         link.addContent(utils.getTypeName(typevariable, false));
 148                     }
 149 
 150                     if (!linkInfo.excludeTypeBounds) {
 151                         linkInfo.excludeTypeBounds = true;
 152                         TypeParameterElement tpe = ((TypeParameterElement) typevariable.asElement());
 153                         boolean more = false;
 154                         List<? extends TypeMirror> bounds = utils.getBounds(tpe);
 155                         for (TypeMirror bound : bounds) {
 156                             // we get everything as extends java.lang.Object we suppress
 157                             // all of them except those that have multiple extends
 158                             if (bounds.size() == 1 &&
 159                                     bound.equals(utils.getObjectType()) &&
 160                                     !utils.isAnnotated(bound)) {
 161                                 continue;
 162                             }
 163                             link.addContent(more ? " & " : " extends ");
 164                             setBoundsLinkInfo(linkInfo, bound);
 165                             link.addContent(getLink(linkInfo));
 166                             more = true;
 167                         }
 168                     }
 169                     return link;
 170                 }
 171 
 172                 @Override
 173                 public Content visitDeclared(DeclaredType type, LinkInfo linkInfo) {
 174                     if (linkInfo.isTypeBound && linkInfo.excludeTypeBoundsLinks) {
 175                         // Since we are excluding type parameter links, we should not
 176                         // be linking to the type bound.
 177                         link.addContent(utils.getTypeName(type, false));
 178                         link.addContent(getTypeParameterLinks(linkInfo));
 179                         return link;
 180                     } else {
 181                         link = newContent();
 182                         link.addContent(getTypeAnnotationLinks(linkInfo));
 183                         linkInfo.typeElement = utils.asTypeElement(type);
 184                         link.addContent(getClassLink(linkInfo));
 185                         if (linkInfo.includeTypeAsSepLink) {
 186                             link.addContent(getTypeParameterLinks(linkInfo, false));
 187                         }
 188                     }
 189                     return link;
 190                 }
 191             };
 192             return linkVisitor.visit(linkInfo.type, linkInfo);
 193         } else if (linkInfo.typeElement != null) {
 194             Content link = newContent();
 195             link.addContent(getClassLink(linkInfo));
 196             if (linkInfo.includeTypeAsSepLink) {
 197                 link.addContent(getTypeParameterLinks(linkInfo, false));
 198             }
 199             return link;
 200         } else {
 201             return null;
 202         }
 203     }
 204 
 205     private void setBoundsLinkInfo(LinkInfo linkInfo, TypeMirror bound) {
 206         linkInfo.typeElement = null;
 207         linkInfo.label = null;
 208         linkInfo.type = bound;
 209     }
 210 
 211     /**
 212      * Returns a link to the given class.
 213      *
 214      * @param linkInfo the information about the link to construct
 215      *
 216      * @return the link for the given class.
 217      */
 218     protected abstract Content getClassLink(LinkInfo linkInfo);
 219 
 220     /**
 221      * Returns a link to the given type parameter.
 222      *
 223      * @param linkInfo     the information about the link to construct
 224      * @param typeParam the type parameter to link to
 225      * @return the link
 226      */
 227     protected abstract Content getTypeParameterLink(LinkInfo linkInfo, TypeMirror typeParam);
 228 
 229     /**
 230      * Returns links to the type parameters.
 231      *
 232      * @param linkInfo     the information about the link to construct
 233      * @return the links to the type parameters.
 234      */
 235     public Content getTypeParameterLinks(LinkInfo linkInfo) {
 236         return getTypeParameterLinks(linkInfo, true);
 237     }
 238 
 239     /**
 240      * Returns links to the type parameters.
 241      *
 242      * @param linkInfo     the information about the link to construct
 243      * @param isClassLabel true if this is a class label, or false if it is
 244      *                     the type parameters portion of the link
 245      * @return the links to the type parameters
 246      */
 247     public Content getTypeParameterLinks(LinkInfo linkInfo, boolean isClassLabel) {
 248         Content links = newContent();
 249         List<TypeMirror> vars = new ArrayList<>();
 250         TypeMirror ctype = linkInfo.type != null
 251                 ? utils.getComponentType(linkInfo.type)
 252                 : null;
 253         if (linkInfo.executableElement != null) {
 254             linkInfo.executableElement.getTypeParameters().stream().forEach((t) -> {
 255                 vars.add(t.asType());
 256             });
 257         } else if (linkInfo.type != null && utils.isDeclaredType(linkInfo.type)) {
 258             ((DeclaredType)linkInfo.type).getTypeArguments().stream().forEach(vars::add);
 259         } else if (ctype != null && utils.isDeclaredType(ctype)) {
 260             ((DeclaredType)ctype).getTypeArguments().stream().forEach(vars::add);
 261         } else if (linkInfo.typeElement != null) {
 262             linkInfo.typeElement.getTypeParameters().stream().forEach((t) -> {
 263                 vars.add(t.asType());
 264             });
 265         } else {
 266             // Nothing to document.
 267             return links;
 268         }
 269         if (((linkInfo.includeTypeInClassLinkLabel && isClassLabel)
 270                 || (linkInfo.includeTypeAsSepLink && !isClassLabel)) && !vars.isEmpty()) {
 271             links.addContent("<");
 272             boolean many = false;
 273             for (TypeMirror t : vars) {
 274                 if (many) {
 275                     links.addContent(",");
 276                 }
 277                 links.addContent(getTypeParameterLink(linkInfo, t));
 278                 many = true;
 279             }
 280             links.addContent(">");
 281         }
 282         return links;
 283     }
 284 
 285     public abstract Content getTypeAnnotationLinks(LinkInfo linkInfo);
 286 }