Mon, 10 Dec 2012 16:21:26 +0000
8003967: detect and remove all mutable implicit static enum fields in langtools
Reviewed-by: jjg
1 /*
2 * Copyright (c) 2003, 2012, 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.asWildcardType() != null) {
65 //Wildcard type.
66 linkInfo.isTypeBound = true;
67 linkInfo.displayLength += 1;
68 linkOutput.append("?");
69 WildcardType wildcardType = type.asWildcardType();
70 Type[] extendsBounds = wildcardType.extendsBounds();
71 for (int i = 0; i < extendsBounds.length; i++) {
72 linkInfo.displayLength += i > 0 ? 2 : 9;
73 linkOutput.append(i > 0 ? ", " : " extends ");
74 setBoundsLinkInfo(linkInfo, extendsBounds[i]);
75 linkOutput.append(getLinkOutput(linkInfo));
76 }
77 Type[] superBounds = wildcardType.superBounds();
78 for (int i = 0; i < superBounds.length; i++) {
79 linkInfo.displayLength += i > 0 ? 2 : 7;
80 linkOutput.append(i > 0 ? ", " : " super ");
81 setBoundsLinkInfo(linkInfo, superBounds[i]);
82 linkOutput.append(getLinkOutput(linkInfo));
83 }
84 } else if (type.asTypeVariable()!= null) {
85 linkInfo.isTypeBound = true;
86 //A type variable.
87 Doc owner = type.asTypeVariable().owner();
88 if ((! linkInfo.excludeTypeParameterLinks) &&
89 owner instanceof ClassDoc) {
90 linkInfo.classDoc = (ClassDoc) owner;
91 linkInfo.label = type.typeName();
92 linkOutput.append(getClassLink(linkInfo));
93 } else {
94 //No need to link method type parameters.
95 linkInfo.displayLength += type.typeName().length();
96 linkOutput.append(type.typeName());
97 }
99 Type[] bounds = type.asTypeVariable().bounds();
100 if (! linkInfo.excludeTypeBounds) {
101 linkInfo.excludeTypeBounds = true;
102 for (int i = 0; i < bounds.length; i++) {
103 linkInfo.displayLength += i > 0 ? 2 : 9;
104 linkOutput.append(i > 0 ? " & " : " extends ");
105 setBoundsLinkInfo(linkInfo, bounds[i]);
106 linkOutput.append(getLinkOutput(linkInfo));
107 }
108 }
109 } else if (type.asClassDoc() != null) {
110 //A class type.
111 if (linkInfo.isTypeBound &&
112 linkInfo.excludeTypeBoundsLinks) {
113 //Since we are excluding type parameter links, we should not
114 //be linking to the type bound.
115 linkInfo.displayLength += type.typeName().length();
116 linkOutput.append(type.typeName());
117 linkOutput.append(getTypeParameterLinks(linkInfo));
118 return linkOutput;
119 } else {
120 linkInfo.classDoc = type.asClassDoc();
121 linkOutput = getClassLink(linkInfo);
122 if (linkInfo.includeTypeAsSepLink) {
123 linkOutput.append(getTypeParameterLinks(linkInfo, false));
124 }
125 }
126 }
128 if (linkInfo.isVarArg) {
129 if (type.dimension().length() > 2) {
130 //Javadoc returns var args as array.
131 //Strip out the first [] from the var arg.
132 linkInfo.displayLength += type.dimension().length()-2;
133 linkOutput.append(type.dimension().substring(2));
134 }
135 linkInfo.displayLength += 3;
136 linkOutput.append("...");
137 } else {
138 linkInfo.displayLength += type.dimension().length();
139 linkOutput.append(type.dimension());
140 }
141 return linkOutput;
142 } else if (linkInfo.classDoc != null) {
143 //Just a class link
144 LinkOutput linkOutput = getClassLink(linkInfo);
145 if (linkInfo.includeTypeAsSepLink) {
146 linkOutput.append(getTypeParameterLinks(linkInfo, false));
147 }
148 return linkOutput;
149 } else {
150 return null;
151 }
152 }
154 private void setBoundsLinkInfo(LinkInfo linkInfo, Type bound) {
155 linkInfo.classDoc = null;
156 linkInfo.label = null;
157 linkInfo.type = bound;
158 }
160 /**
161 * Return the link to the given class.
162 *
163 * @param linkInfo the information about the link to construct.
164 *
165 * @return the link for the given class.
166 */
167 protected abstract LinkOutput getClassLink(LinkInfo linkInfo);
169 /**
170 * Return the link to the given type parameter.
171 *
172 * @param linkInfo the information about the link to construct.
173 * @param typeParam the type parameter to link to.
174 */
175 protected abstract LinkOutput getTypeParameterLink(LinkInfo linkInfo,
176 Type typeParam);
178 /**
179 * Return the links to the type parameters.
180 *
181 * @param linkInfo the information about the link to construct.
182 * @return the links to the type parameters.
183 */
184 public LinkOutput getTypeParameterLinks(LinkInfo linkInfo) {
185 return getTypeParameterLinks(linkInfo, true);
186 }
188 /**
189 * Return the links to the type parameters.
190 *
191 * @param linkInfo the information about the link to construct.
192 * @param isClassLabel true if this is a class label. False if it is
193 * the type parameters portion of the link.
194 * @return the links to the type parameters.
195 */
196 public LinkOutput getTypeParameterLinks(LinkInfo linkInfo, boolean isClassLabel) {
197 LinkOutput output = getOutputInstance();
198 Type[] vars;
199 if (linkInfo.executableMemberDoc != null) {
200 vars = linkInfo.executableMemberDoc.typeParameters();
201 } else if (linkInfo.type != null &&
202 linkInfo.type.asParameterizedType() != null){
203 vars = linkInfo.type.asParameterizedType().typeArguments();
204 } else if (linkInfo.classDoc != null){
205 vars = linkInfo.classDoc.typeParameters();
206 } else {
207 //Nothing to document.
208 return output;
209 }
210 if (((linkInfo.includeTypeInClassLinkLabel && isClassLabel) ||
211 (linkInfo.includeTypeAsSepLink && ! isClassLabel)
212 )
213 && vars.length > 0) {
214 linkInfo.displayLength += 1;
215 output.append(getLessThanString());
216 for (int i = 0; i < vars.length; i++) {
217 if (i > 0) {
218 linkInfo.displayLength += 1;
219 output.append(",");
220 }
221 output.append(getTypeParameterLink(linkInfo, vars[i]));
222 }
223 linkInfo.displayLength += 1;
224 output.append(getGreaterThanString());
225 }
226 return output;
227 }
229 /**
230 * Return &lt;, which is used in type parameters. Override this
231 * if your doclet uses something different.
232 *
233 * @return return &lt;, which is used in type parameters.
234 */
235 protected String getLessThanString() {
236 return "<";
237 }
239 /**
240 * Return &gt;, which is used in type parameters. Override this
241 * if your doclet uses something different.
242 *
243 * @return return &gt;, which is used in type parameters.
244 */
245 protected String getGreaterThanString() {
246 return ">";
247 }
248 }