Sat, 13 Apr 2013 18:48:29 -0700
8009686: Generated javadoc documentation should be able to display type annotation on an array
Reviewed-by: jjg
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 && type.dimension().length() == 0) {
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 while (type != null && type.dimension().length() > 0) {
145 linkInfo.displayLength += type.dimension().length();
146 if (type.asAnnotatedType() != null) {
147 linkInfo.type = type;
148 linkOutput.append(" ");
149 linkOutput.append(getTypeAnnotationLinks(linkInfo));
150 linkOutput.append("[]");
151 type = type.asAnnotatedType().underlyingType().getElementType();
152 } else {
153 linkOutput.append("[]");
154 type = type.getElementType();
155 }
156 }
157 linkInfo.type = type;
158 linkOutput.insert(0, getTypeAnnotationLinks(linkInfo));
159 }
160 return linkOutput;
161 } else if (linkInfo.classDoc != null) {
162 //Just a class link
163 LinkOutput linkOutput = getClassLink(linkInfo);
164 if (linkInfo.includeTypeAsSepLink) {
165 linkOutput.append(getTypeParameterLinks(linkInfo, false));
166 }
167 return linkOutput;
168 } else {
169 return null;
170 }
171 }
173 private void setBoundsLinkInfo(LinkInfo linkInfo, Type bound) {
174 linkInfo.classDoc = null;
175 linkInfo.label = null;
176 linkInfo.type = bound;
177 }
179 /**
180 * Return the link to the given class.
181 *
182 * @param linkInfo the information about the link to construct.
183 *
184 * @return the link for the given class.
185 */
186 protected abstract LinkOutput getClassLink(LinkInfo linkInfo);
188 /**
189 * Return the link to the given type parameter.
190 *
191 * @param linkInfo the information about the link to construct.
192 * @param typeParam the type parameter to link to.
193 */
194 protected abstract LinkOutput getTypeParameterLink(LinkInfo linkInfo,
195 Type typeParam);
197 protected abstract LinkOutput getTypeAnnotationLink(LinkInfo linkInfo,
198 AnnotationDesc annotation);
200 /**
201 * Return the links to the type parameters.
202 *
203 * @param linkInfo the information about the link to construct.
204 * @return the links to the type parameters.
205 */
206 public LinkOutput getTypeParameterLinks(LinkInfo linkInfo) {
207 return getTypeParameterLinks(linkInfo, true);
208 }
210 /**
211 * Return the links to the type parameters.
212 *
213 * @param linkInfo the information about the link to construct.
214 * @param isClassLabel true if this is a class label. False if it is
215 * the type parameters portion of the link.
216 * @return the links to the type parameters.
217 */
218 public LinkOutput getTypeParameterLinks(LinkInfo linkInfo, boolean isClassLabel) {
219 LinkOutput output = getOutputInstance();
220 Type[] vars;
221 if (linkInfo.executableMemberDoc != null) {
222 vars = linkInfo.executableMemberDoc.typeParameters();
223 } else if (linkInfo.type != null &&
224 linkInfo.type.asParameterizedType() != null){
225 vars = linkInfo.type.asParameterizedType().typeArguments();
226 } else if (linkInfo.classDoc != null){
227 vars = linkInfo.classDoc.typeParameters();
228 } else {
229 //Nothing to document.
230 return output;
231 }
232 if (((linkInfo.includeTypeInClassLinkLabel && isClassLabel) ||
233 (linkInfo.includeTypeAsSepLink && ! isClassLabel)
234 )
235 && vars.length > 0) {
236 linkInfo.displayLength += 1;
237 output.append(getLessThanString());
238 for (int i = 0; i < vars.length; i++) {
239 if (i > 0) {
240 linkInfo.displayLength += 1;
241 output.append(",");
242 }
243 output.append(getTypeParameterLink(linkInfo, vars[i]));
244 }
245 linkInfo.displayLength += 1;
246 output.append(getGreaterThanString());
247 }
248 return output;
249 }
251 public LinkOutput getTypeAnnotationLinks(LinkInfo linkInfo) {
252 LinkOutput output = getOutputInstance();
253 if (linkInfo.type.asAnnotatedType() == null)
254 return output;
255 AnnotationDesc[] annotations = linkInfo.type.asAnnotatedType().annotations();
256 for (int i = 0; i < annotations.length; i++) {
257 if (i > 0) {
258 linkInfo.displayLength += 1;
259 output.append(" ");
260 }
261 output.append(getTypeAnnotationLink(linkInfo, annotations[i]));
262 }
264 linkInfo.displayLength += 1;
265 output.append(" ");
266 return output;
267 }
269 /**
270 * Return &lt;, which is used in type parameters. Override this
271 * if your doclet uses something different.
272 *
273 * @return return &lt;, which is used in type parameters.
274 */
275 protected String getLessThanString() {
276 return "<";
277 }
279 /**
280 * Return &gt;, which is used in type parameters. Override this
281 * if your doclet uses something different.
282 *
283 * @return return &gt;, which is used in type parameters.
284 */
285 protected String getGreaterThanString() {
286 return ">";
287 }
288 }