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