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