Wed, 27 Apr 2016 01:34:52 +0800
Initial load
http://hg.openjdk.java.net/jdk8u/jdk8u/langtools/
changeset: 2573:53ca196be1ae
tag: jdk8u25-b17
aoqi@0 | 1 | /* |
aoqi@0 | 2 | * Copyright (c) 2003, 2013, Oracle and/or its affiliates. All rights reserved. |
aoqi@0 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
aoqi@0 | 4 | * |
aoqi@0 | 5 | * This code is free software; you can redistribute it and/or modify it |
aoqi@0 | 6 | * under the terms of the GNU General Public License version 2 only, as |
aoqi@0 | 7 | * published by the Free Software Foundation. Oracle designates this |
aoqi@0 | 8 | * particular file as subject to the "Classpath" exception as provided |
aoqi@0 | 9 | * by Oracle in the LICENSE file that accompanied this code. |
aoqi@0 | 10 | * |
aoqi@0 | 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
aoqi@0 | 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
aoqi@0 | 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
aoqi@0 | 14 | * version 2 for more details (a copy is included in the LICENSE file that |
aoqi@0 | 15 | * accompanied this code). |
aoqi@0 | 16 | * |
aoqi@0 | 17 | * You should have received a copy of the GNU General Public License version |
aoqi@0 | 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
aoqi@0 | 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
aoqi@0 | 20 | * |
aoqi@0 | 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
aoqi@0 | 22 | * or visit www.oracle.com if you need additional information or have any |
aoqi@0 | 23 | * questions. |
aoqi@0 | 24 | */ |
aoqi@0 | 25 | |
aoqi@0 | 26 | package com.sun.tools.doclets.internal.toolkit.util.links; |
aoqi@0 | 27 | |
aoqi@0 | 28 | import com.sun.javadoc.*; |
aoqi@0 | 29 | import com.sun.tools.doclets.internal.toolkit.Content; |
aoqi@0 | 30 | |
aoqi@0 | 31 | /** |
aoqi@0 | 32 | * A factory that constructs links from given link information. |
aoqi@0 | 33 | * |
aoqi@0 | 34 | * <p><b>This is NOT part of any supported API. |
aoqi@0 | 35 | * If you write code that depends on this, you do so at your own risk. |
aoqi@0 | 36 | * This code and its internal interfaces are subject to change or |
aoqi@0 | 37 | * deletion without notice.</b> |
aoqi@0 | 38 | * |
aoqi@0 | 39 | * @author Jamie Ho |
aoqi@0 | 40 | * @since 1.5 |
aoqi@0 | 41 | */ |
aoqi@0 | 42 | public abstract class LinkFactory { |
aoqi@0 | 43 | |
aoqi@0 | 44 | /** |
aoqi@0 | 45 | * Return an empty instance of a content object. |
aoqi@0 | 46 | * |
aoqi@0 | 47 | * @return an empty instance of a content object. |
aoqi@0 | 48 | */ |
aoqi@0 | 49 | protected abstract Content newContent(); |
aoqi@0 | 50 | |
aoqi@0 | 51 | /** |
aoqi@0 | 52 | * Constructs a link from the given link information. |
aoqi@0 | 53 | * |
aoqi@0 | 54 | * @param linkInfo the information about the link. |
aoqi@0 | 55 | * @return the output of the link. |
aoqi@0 | 56 | */ |
aoqi@0 | 57 | public Content getLink(LinkInfo linkInfo) { |
aoqi@0 | 58 | if (linkInfo.type != null) { |
aoqi@0 | 59 | Type type = linkInfo.type; |
aoqi@0 | 60 | Content link = newContent(); |
aoqi@0 | 61 | if (type.isPrimitive()) { |
aoqi@0 | 62 | //Just a primitive. |
aoqi@0 | 63 | link.addContent(type.typeName()); |
aoqi@0 | 64 | } else if (type.asAnnotatedType() != null && type.dimension().length() == 0) { |
aoqi@0 | 65 | link.addContent(getTypeAnnotationLinks(linkInfo)); |
aoqi@0 | 66 | linkInfo.type = type.asAnnotatedType().underlyingType(); |
aoqi@0 | 67 | link.addContent(getLink(linkInfo)); |
aoqi@0 | 68 | return link; |
aoqi@0 | 69 | } else if (type.asWildcardType() != null) { |
aoqi@0 | 70 | //Wildcard type. |
aoqi@0 | 71 | linkInfo.isTypeBound = true; |
aoqi@0 | 72 | link.addContent("?"); |
aoqi@0 | 73 | WildcardType wildcardType = type.asWildcardType(); |
aoqi@0 | 74 | Type[] extendsBounds = wildcardType.extendsBounds(); |
aoqi@0 | 75 | for (int i = 0; i < extendsBounds.length; i++) { |
aoqi@0 | 76 | link.addContent(i > 0 ? ", " : " extends "); |
aoqi@0 | 77 | setBoundsLinkInfo(linkInfo, extendsBounds[i]); |
aoqi@0 | 78 | link.addContent(getLink(linkInfo)); |
aoqi@0 | 79 | } |
aoqi@0 | 80 | Type[] superBounds = wildcardType.superBounds(); |
aoqi@0 | 81 | for (int i = 0; i < superBounds.length; i++) { |
aoqi@0 | 82 | link.addContent(i > 0 ? ", " : " super "); |
aoqi@0 | 83 | setBoundsLinkInfo(linkInfo, superBounds[i]); |
aoqi@0 | 84 | link.addContent(getLink(linkInfo)); |
aoqi@0 | 85 | } |
aoqi@0 | 86 | } else if (type.asTypeVariable()!= null) { |
aoqi@0 | 87 | link.addContent(getTypeAnnotationLinks(linkInfo)); |
aoqi@0 | 88 | linkInfo.isTypeBound = true; |
aoqi@0 | 89 | //A type variable. |
aoqi@0 | 90 | Doc owner = type.asTypeVariable().owner(); |
aoqi@0 | 91 | if ((! linkInfo.excludeTypeParameterLinks) && |
aoqi@0 | 92 | owner instanceof ClassDoc) { |
aoqi@0 | 93 | linkInfo.classDoc = (ClassDoc) owner; |
aoqi@0 | 94 | Content label = newContent(); |
aoqi@0 | 95 | label.addContent(type.typeName()); |
aoqi@0 | 96 | linkInfo.label = label; |
aoqi@0 | 97 | link.addContent(getClassLink(linkInfo)); |
aoqi@0 | 98 | } else { |
aoqi@0 | 99 | //No need to link method type parameters. |
aoqi@0 | 100 | link.addContent(type.typeName()); |
aoqi@0 | 101 | } |
aoqi@0 | 102 | |
aoqi@0 | 103 | Type[] bounds = type.asTypeVariable().bounds(); |
aoqi@0 | 104 | if (! linkInfo.excludeTypeBounds) { |
aoqi@0 | 105 | linkInfo.excludeTypeBounds = true; |
aoqi@0 | 106 | for (int i = 0; i < bounds.length; i++) { |
aoqi@0 | 107 | link.addContent(i > 0 ? " & " : " extends "); |
aoqi@0 | 108 | setBoundsLinkInfo(linkInfo, bounds[i]); |
aoqi@0 | 109 | link.addContent(getLink(linkInfo)); |
aoqi@0 | 110 | } |
aoqi@0 | 111 | } |
aoqi@0 | 112 | } else if (type.asClassDoc() != null) { |
aoqi@0 | 113 | //A class type. |
aoqi@0 | 114 | if (linkInfo.isTypeBound && |
aoqi@0 | 115 | linkInfo.excludeTypeBoundsLinks) { |
aoqi@0 | 116 | //Since we are excluding type parameter links, we should not |
aoqi@0 | 117 | //be linking to the type bound. |
aoqi@0 | 118 | link.addContent(type.typeName()); |
aoqi@0 | 119 | link.addContent(getTypeParameterLinks(linkInfo)); |
aoqi@0 | 120 | return link; |
aoqi@0 | 121 | } else { |
aoqi@0 | 122 | linkInfo.classDoc = type.asClassDoc(); |
aoqi@0 | 123 | link = newContent(); |
aoqi@0 | 124 | link.addContent(getClassLink(linkInfo)); |
aoqi@0 | 125 | if (linkInfo.includeTypeAsSepLink) { |
aoqi@0 | 126 | link.addContent(getTypeParameterLinks(linkInfo, false)); |
aoqi@0 | 127 | } |
aoqi@0 | 128 | } |
aoqi@0 | 129 | } |
aoqi@0 | 130 | |
aoqi@0 | 131 | if (linkInfo.isVarArg) { |
aoqi@0 | 132 | if (type.dimension().length() > 2) { |
aoqi@0 | 133 | //Javadoc returns var args as array. |
aoqi@0 | 134 | //Strip out the first [] from the var arg. |
aoqi@0 | 135 | link.addContent(type.dimension().substring(2)); |
aoqi@0 | 136 | } |
aoqi@0 | 137 | link.addContent("..."); |
aoqi@0 | 138 | } else { |
aoqi@0 | 139 | while (type != null && type.dimension().length() > 0) { |
aoqi@0 | 140 | if (type.asAnnotatedType() != null) { |
aoqi@0 | 141 | linkInfo.type = type; |
aoqi@0 | 142 | link.addContent(" "); |
aoqi@0 | 143 | link.addContent(getTypeAnnotationLinks(linkInfo)); |
aoqi@0 | 144 | link.addContent("[]"); |
aoqi@0 | 145 | type = type.asAnnotatedType().underlyingType().getElementType(); |
aoqi@0 | 146 | } else { |
aoqi@0 | 147 | link.addContent("[]"); |
aoqi@0 | 148 | type = type.getElementType(); |
aoqi@0 | 149 | } |
aoqi@0 | 150 | } |
aoqi@0 | 151 | linkInfo.type = type; |
aoqi@0 | 152 | Content newLink = newContent(); |
aoqi@0 | 153 | newLink.addContent(getTypeAnnotationLinks(linkInfo)); |
aoqi@0 | 154 | newLink.addContent(link); |
aoqi@0 | 155 | link = newLink; |
aoqi@0 | 156 | } |
aoqi@0 | 157 | return link; |
aoqi@0 | 158 | } else if (linkInfo.classDoc != null) { |
aoqi@0 | 159 | //Just a class link |
aoqi@0 | 160 | Content link = newContent(); |
aoqi@0 | 161 | link.addContent(getClassLink(linkInfo)); |
aoqi@0 | 162 | if (linkInfo.includeTypeAsSepLink) { |
aoqi@0 | 163 | link.addContent(getTypeParameterLinks(linkInfo, false)); |
aoqi@0 | 164 | } |
aoqi@0 | 165 | return link; |
aoqi@0 | 166 | } else { |
aoqi@0 | 167 | return null; |
aoqi@0 | 168 | } |
aoqi@0 | 169 | } |
aoqi@0 | 170 | |
aoqi@0 | 171 | private void setBoundsLinkInfo(LinkInfo linkInfo, Type bound) { |
aoqi@0 | 172 | linkInfo.classDoc = null; |
aoqi@0 | 173 | linkInfo.label = null; |
aoqi@0 | 174 | linkInfo.type = bound; |
aoqi@0 | 175 | } |
aoqi@0 | 176 | |
aoqi@0 | 177 | /** |
aoqi@0 | 178 | * Return the link to the given class. |
aoqi@0 | 179 | * |
aoqi@0 | 180 | * @param linkInfo the information about the link to construct. |
aoqi@0 | 181 | * |
aoqi@0 | 182 | * @return the link for the given class. |
aoqi@0 | 183 | */ |
aoqi@0 | 184 | protected abstract Content getClassLink(LinkInfo linkInfo); |
aoqi@0 | 185 | |
aoqi@0 | 186 | /** |
aoqi@0 | 187 | * Return the link to the given type parameter. |
aoqi@0 | 188 | * |
aoqi@0 | 189 | * @param linkInfo the information about the link to construct. |
aoqi@0 | 190 | * @param typeParam the type parameter to link to. |
aoqi@0 | 191 | */ |
aoqi@0 | 192 | protected abstract Content getTypeParameterLink(LinkInfo linkInfo, |
aoqi@0 | 193 | Type typeParam); |
aoqi@0 | 194 | |
aoqi@0 | 195 | protected abstract Content getTypeAnnotationLink(LinkInfo linkInfo, |
aoqi@0 | 196 | AnnotationDesc annotation); |
aoqi@0 | 197 | |
aoqi@0 | 198 | /** |
aoqi@0 | 199 | * Return the links to the type parameters. |
aoqi@0 | 200 | * |
aoqi@0 | 201 | * @param linkInfo the information about the link to construct. |
aoqi@0 | 202 | * @return the links to the type parameters. |
aoqi@0 | 203 | */ |
aoqi@0 | 204 | public Content getTypeParameterLinks(LinkInfo linkInfo) { |
aoqi@0 | 205 | return getTypeParameterLinks(linkInfo, true); |
aoqi@0 | 206 | } |
aoqi@0 | 207 | |
aoqi@0 | 208 | /** |
aoqi@0 | 209 | * Return the links to the type parameters. |
aoqi@0 | 210 | * |
aoqi@0 | 211 | * @param linkInfo the information about the link to construct. |
aoqi@0 | 212 | * @param isClassLabel true if this is a class label. False if it is |
aoqi@0 | 213 | * the type parameters portion of the link. |
aoqi@0 | 214 | * @return the links to the type parameters. |
aoqi@0 | 215 | */ |
aoqi@0 | 216 | public Content getTypeParameterLinks(LinkInfo linkInfo, boolean isClassLabel) { |
aoqi@0 | 217 | Content links = newContent(); |
aoqi@0 | 218 | Type[] vars; |
aoqi@0 | 219 | if (linkInfo.executableMemberDoc != null) { |
aoqi@0 | 220 | vars = linkInfo.executableMemberDoc.typeParameters(); |
aoqi@0 | 221 | } else if (linkInfo.type != null && |
aoqi@0 | 222 | linkInfo.type.asParameterizedType() != null){ |
aoqi@0 | 223 | vars = linkInfo.type.asParameterizedType().typeArguments(); |
aoqi@0 | 224 | } else if (linkInfo.classDoc != null){ |
aoqi@0 | 225 | vars = linkInfo.classDoc.typeParameters(); |
aoqi@0 | 226 | } else { |
aoqi@0 | 227 | //Nothing to document. |
aoqi@0 | 228 | return links; |
aoqi@0 | 229 | } |
aoqi@0 | 230 | if (((linkInfo.includeTypeInClassLinkLabel && isClassLabel) || |
aoqi@0 | 231 | (linkInfo.includeTypeAsSepLink && ! isClassLabel) |
aoqi@0 | 232 | ) |
aoqi@0 | 233 | && vars.length > 0) { |
aoqi@0 | 234 | links.addContent("<"); |
aoqi@0 | 235 | for (int i = 0; i < vars.length; i++) { |
aoqi@0 | 236 | if (i > 0) { |
aoqi@0 | 237 | links.addContent(","); |
aoqi@0 | 238 | } |
aoqi@0 | 239 | links.addContent(getTypeParameterLink(linkInfo, vars[i])); |
aoqi@0 | 240 | } |
aoqi@0 | 241 | links.addContent(">"); |
aoqi@0 | 242 | } |
aoqi@0 | 243 | return links; |
aoqi@0 | 244 | } |
aoqi@0 | 245 | |
aoqi@0 | 246 | public Content getTypeAnnotationLinks(LinkInfo linkInfo) { |
aoqi@0 | 247 | Content links = newContent(); |
aoqi@0 | 248 | if (linkInfo.type.asAnnotatedType() == null) |
aoqi@0 | 249 | return links; |
aoqi@0 | 250 | AnnotationDesc[] annotations = linkInfo.type.asAnnotatedType().annotations(); |
aoqi@0 | 251 | for (int i = 0; i < annotations.length; i++) { |
aoqi@0 | 252 | if (i > 0) { |
aoqi@0 | 253 | links.addContent(" "); |
aoqi@0 | 254 | } |
aoqi@0 | 255 | links.addContent(getTypeAnnotationLink(linkInfo, annotations[i])); |
aoqi@0 | 256 | } |
aoqi@0 | 257 | |
aoqi@0 | 258 | links.addContent(" "); |
aoqi@0 | 259 | return links; |
aoqi@0 | 260 | } |
aoqi@0 | 261 | } |