author | jjg |
Thu, 25 Oct 2012 11:09:36 -0700 | |
changeset 14359 | d4099818ab70 |
parent 14260 | 727a84636f12 |
child 15385 | ee1eebe7e210 |
permissions | -rw-r--r-- |
10 | 1 |
/* |
14258 | 2 |
* Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved. |
10 | 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 |
|
5520 | 7 |
* published by the Free Software Foundation. Oracle designates this |
10 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5520 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
10 | 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 |
* |
|
5520 | 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. |
|
10 | 24 |
*/ |
25 |
||
26 |
package com.sun.tools.javadoc; |
|
27 |
||
28 |
import com.sun.javadoc.*; |
|
29 |
import com.sun.tools.javac.code.Symbol; |
|
30 |
import com.sun.tools.javac.code.Symbol.ClassSymbol; |
|
31 |
import com.sun.tools.javac.code.Type; |
|
14258 | 32 |
import com.sun.tools.javac.code.Type.ArrayType; |
10 | 33 |
import com.sun.tools.javac.code.Type.ClassType; |
34 |
import com.sun.tools.javac.code.Type.TypeVar; |
|
35 |
import com.sun.tools.javac.util.List; |
|
14359
d4099818ab70
7200915: convert TypeTags from a series of small ints to an enum
jjg
parents:
14260
diff
changeset
|
36 |
import static com.sun.tools.javac.code.TypeTag.ARRAY; |
10 | 37 |
|
14260
727a84636f12
8000665: fix "internal API" comments on javadoc files
jjg
parents:
14258
diff
changeset
|
38 |
/** |
727a84636f12
8000665: fix "internal API" comments on javadoc files
jjg
parents:
14258
diff
changeset
|
39 |
* <p><b>This is NOT part of any supported API. |
727a84636f12
8000665: fix "internal API" comments on javadoc files
jjg
parents:
14258
diff
changeset
|
40 |
* If you write code that depends on this, you do so at your own risk. |
727a84636f12
8000665: fix "internal API" comments on javadoc files
jjg
parents:
14258
diff
changeset
|
41 |
* This code and its internal interfaces are subject to change or |
727a84636f12
8000665: fix "internal API" comments on javadoc files
jjg
parents:
14258
diff
changeset
|
42 |
* deletion without notice.</b> |
727a84636f12
8000665: fix "internal API" comments on javadoc files
jjg
parents:
14258
diff
changeset
|
43 |
*/ |
10 | 44 |
public class TypeMaker { |
45 |
||
46 |
public static com.sun.javadoc.Type getType(DocEnv env, Type t) { |
|
47 |
return getType(env, t, true); |
|
48 |
} |
|
49 |
||
50 |
/** |
|
51 |
* @param errToClassDoc if true, ERROR type results in a ClassDoc; |
|
52 |
* false preserves legacy behavior |
|
53 |
*/ |
|
1870
57a1138dffc8
6795903: fix latent build warnings in langtools repository
jjg
parents:
10
diff
changeset
|
54 |
@SuppressWarnings("fallthrough") |
10 | 55 |
public static com.sun.javadoc.Type getType(DocEnv env, Type t, |
56 |
boolean errToClassDoc) { |
|
57 |
if (env.legacyDoclet) { |
|
58 |
t = env.types.erasure(t); |
|
59 |
} |
|
14359
d4099818ab70
7200915: convert TypeTags from a series of small ints to an enum
jjg
parents:
14260
diff
changeset
|
60 |
switch (t.getTag()) { |
10 | 61 |
case CLASS: |
62 |
if (ClassDocImpl.isGeneric((ClassSymbol)t.tsym)) { |
|
63 |
return env.getParameterizedType((ClassType)t); |
|
64 |
} else { |
|
65 |
return env.getClassDoc((ClassSymbol)t.tsym); |
|
66 |
} |
|
67 |
case WILDCARD: |
|
68 |
Type.WildcardType a = (Type.WildcardType)t; |
|
69 |
return new WildcardTypeImpl(env, a); |
|
70 |
case TYPEVAR: return new TypeVariableImpl(env, (TypeVar)t); |
|
71 |
case ARRAY: return new ArrayTypeImpl(env, t); |
|
72 |
case BYTE: return PrimitiveType.byteType; |
|
73 |
case CHAR: return PrimitiveType.charType; |
|
74 |
case SHORT: return PrimitiveType.shortType; |
|
75 |
case INT: return PrimitiveType.intType; |
|
76 |
case LONG: return PrimitiveType.longType; |
|
77 |
case FLOAT: return PrimitiveType.floatType; |
|
78 |
case DOUBLE: return PrimitiveType.doubleType; |
|
79 |
case BOOLEAN: return PrimitiveType.booleanType; |
|
80 |
case VOID: return PrimitiveType.voidType; |
|
81 |
case ERROR: |
|
82 |
if (errToClassDoc) |
|
83 |
return env.getClassDoc((ClassSymbol)t.tsym); |
|
84 |
// FALLTHRU |
|
85 |
default: |
|
86 |
return new PrimitiveType(t.tsym.getQualifiedName().toString()); |
|
87 |
} |
|
88 |
} |
|
89 |
||
90 |
/** |
|
91 |
* Convert a list of javac types into an array of javadoc types. |
|
92 |
*/ |
|
93 |
public static com.sun.javadoc.Type[] getTypes(DocEnv env, List<Type> ts) { |
|
94 |
return getTypes(env, ts, new com.sun.javadoc.Type[ts.length()]); |
|
95 |
} |
|
96 |
||
97 |
/** |
|
98 |
* Like the above version, but use and return the array given. |
|
99 |
*/ |
|
100 |
public static com.sun.javadoc.Type[] getTypes(DocEnv env, List<Type> ts, |
|
101 |
com.sun.javadoc.Type res[]) { |
|
102 |
int i = 0; |
|
103 |
for (Type t : ts) { |
|
104 |
res[i++] = getType(env, t); |
|
105 |
} |
|
106 |
return res; |
|
107 |
} |
|
108 |
||
109 |
public static String getTypeName(Type t, boolean full) { |
|
14359
d4099818ab70
7200915: convert TypeTags from a series of small ints to an enum
jjg
parents:
14260
diff
changeset
|
110 |
switch (t.getTag()) { |
10 | 111 |
case ARRAY: |
8631
664f84942e74
6866185: Util.getPackageSourcePath should use lastIndexOf not indexOf and related cleanup
jjg
parents:
5520
diff
changeset
|
112 |
StringBuilder s = new StringBuilder(); |
14359
d4099818ab70
7200915: convert TypeTags from a series of small ints to an enum
jjg
parents:
14260
diff
changeset
|
113 |
while (t.hasTag(ARRAY)) { |
8631
664f84942e74
6866185: Util.getPackageSourcePath should use lastIndexOf not indexOf and related cleanup
jjg
parents:
5520
diff
changeset
|
114 |
s.append("[]"); |
10 | 115 |
t = ((ArrayType)t).elemtype; |
116 |
} |
|
8631
664f84942e74
6866185: Util.getPackageSourcePath should use lastIndexOf not indexOf and related cleanup
jjg
parents:
5520
diff
changeset
|
117 |
s.insert(0, getTypeName(t, full)); |
664f84942e74
6866185: Util.getPackageSourcePath should use lastIndexOf not indexOf and related cleanup
jjg
parents:
5520
diff
changeset
|
118 |
return s.toString(); |
10 | 119 |
case CLASS: |
120 |
return ClassDocImpl.getClassName((ClassSymbol)t.tsym, full); |
|
121 |
default: |
|
122 |
return t.tsym.getQualifiedName().toString(); |
|
123 |
} |
|
124 |
} |
|
125 |
||
126 |
/** |
|
127 |
* Return the string representation of a type use. Bounds of type |
|
128 |
* variables are not included; bounds of wildcard types are. |
|
129 |
* Class names are qualified if "full" is true. |
|
130 |
*/ |
|
131 |
static String getTypeString(DocEnv env, Type t, boolean full) { |
|
14359
d4099818ab70
7200915: convert TypeTags from a series of small ints to an enum
jjg
parents:
14260
diff
changeset
|
132 |
switch (t.getTag()) { |
10 | 133 |
case ARRAY: |
8631
664f84942e74
6866185: Util.getPackageSourcePath should use lastIndexOf not indexOf and related cleanup
jjg
parents:
5520
diff
changeset
|
134 |
StringBuilder s = new StringBuilder(); |
14359
d4099818ab70
7200915: convert TypeTags from a series of small ints to an enum
jjg
parents:
14260
diff
changeset
|
135 |
while (t.hasTag(ARRAY)) { |
8631
664f84942e74
6866185: Util.getPackageSourcePath should use lastIndexOf not indexOf and related cleanup
jjg
parents:
5520
diff
changeset
|
136 |
s.append("[]"); |
10 | 137 |
t = env.types.elemtype(t); |
138 |
} |
|
8631
664f84942e74
6866185: Util.getPackageSourcePath should use lastIndexOf not indexOf and related cleanup
jjg
parents:
5520
diff
changeset
|
139 |
s.insert(0, getTypeString(env, t, full)); |
664f84942e74
6866185: Util.getPackageSourcePath should use lastIndexOf not indexOf and related cleanup
jjg
parents:
5520
diff
changeset
|
140 |
return s.toString(); |
10 | 141 |
case CLASS: |
142 |
return ParameterizedTypeImpl. |
|
143 |
parameterizedTypeToString(env, (ClassType)t, full); |
|
144 |
case WILDCARD: |
|
145 |
Type.WildcardType a = (Type.WildcardType)t; |
|
146 |
return WildcardTypeImpl.wildcardTypeToString(env, a, full); |
|
147 |
default: |
|
148 |
return t.tsym.getQualifiedName().toString(); |
|
149 |
} |
|
150 |
} |
|
151 |
||
152 |
/** |
|
153 |
* Return the formal type parameters of a class or method as an |
|
154 |
* angle-bracketed string. Each parameter is a type variable with |
|
155 |
* optional bounds. Class names are qualified if "full" is true. |
|
156 |
* Return "" if there are no type parameters or we're hiding generics. |
|
157 |
*/ |
|
158 |
static String typeParametersString(DocEnv env, Symbol sym, boolean full) { |
|
159 |
if (env.legacyDoclet || sym.type.getTypeArguments().isEmpty()) { |
|
160 |
return ""; |
|
161 |
} |
|
8631
664f84942e74
6866185: Util.getPackageSourcePath should use lastIndexOf not indexOf and related cleanup
jjg
parents:
5520
diff
changeset
|
162 |
StringBuilder s = new StringBuilder(); |
10 | 163 |
for (Type t : sym.type.getTypeArguments()) { |
164 |
s.append(s.length() == 0 ? "<" : ", "); |
|
165 |
s.append(TypeVariableImpl.typeVarToString(env, (TypeVar)t, full)); |
|
166 |
} |
|
167 |
s.append(">"); |
|
168 |
return s.toString(); |
|
169 |
} |
|
170 |
||
171 |
/** |
|
172 |
* Return the actual type arguments of a parameterized type as an |
|
173 |
* angle-bracketed string. Class name are qualified if "full" is true. |
|
174 |
* Return "" if there are no type arguments or we're hiding generics. |
|
175 |
*/ |
|
176 |
static String typeArgumentsString(DocEnv env, ClassType cl, boolean full) { |
|
177 |
if (env.legacyDoclet || cl.getTypeArguments().isEmpty()) { |
|
178 |
return ""; |
|
179 |
} |
|
8631
664f84942e74
6866185: Util.getPackageSourcePath should use lastIndexOf not indexOf and related cleanup
jjg
parents:
5520
diff
changeset
|
180 |
StringBuilder s = new StringBuilder(); |
10 | 181 |
for (Type t : cl.getTypeArguments()) { |
182 |
s.append(s.length() == 0 ? "<" : ", "); |
|
183 |
s.append(getTypeString(env, t, full)); |
|
184 |
} |
|
185 |
s.append(">"); |
|
186 |
return s.toString(); |
|
187 |
} |
|
188 |
||
189 |
||
190 |
private static class ArrayTypeImpl implements com.sun.javadoc.Type { |
|
191 |
||
192 |
Type arrayType; |
|
193 |
||
194 |
DocEnv env; |
|
195 |
||
196 |
ArrayTypeImpl(DocEnv env, Type arrayType) { |
|
197 |
this.env = env; |
|
198 |
this.arrayType = arrayType; |
|
199 |
} |
|
200 |
||
201 |
private com.sun.javadoc.Type skipArraysCache = null; |
|
202 |
||
203 |
private com.sun.javadoc.Type skipArrays() { |
|
204 |
if (skipArraysCache == null) { |
|
205 |
Type t; |
|
14359
d4099818ab70
7200915: convert TypeTags from a series of small ints to an enum
jjg
parents:
14260
diff
changeset
|
206 |
for (t = arrayType; t.hasTag(ARRAY); t = env.types.elemtype(t)) { } |
10 | 207 |
skipArraysCache = TypeMaker.getType(env, t); |
208 |
} |
|
209 |
return skipArraysCache; |
|
210 |
} |
|
211 |
||
212 |
/** |
|
213 |
* Return the type's dimension information, as a string. |
|
214 |
* <p> |
|
215 |
* For example, a two dimensional array of String returns '[][]'. |
|
216 |
*/ |
|
217 |
public String dimension() { |
|
8631
664f84942e74
6866185: Util.getPackageSourcePath should use lastIndexOf not indexOf and related cleanup
jjg
parents:
5520
diff
changeset
|
218 |
StringBuilder dimension = new StringBuilder(); |
14359
d4099818ab70
7200915: convert TypeTags from a series of small ints to an enum
jjg
parents:
14260
diff
changeset
|
219 |
for (Type t = arrayType; t.hasTag(ARRAY); t = env.types.elemtype(t)) { |
8631
664f84942e74
6866185: Util.getPackageSourcePath should use lastIndexOf not indexOf and related cleanup
jjg
parents:
5520
diff
changeset
|
220 |
dimension.append("[]"); |
10 | 221 |
} |
222 |
return dimension.toString(); |
|
223 |
} |
|
224 |
||
225 |
/** |
|
226 |
* Return unqualified name of type excluding any dimension information. |
|
227 |
* <p> |
|
228 |
* For example, a two dimensional array of String returns 'String'. |
|
229 |
*/ |
|
230 |
public String typeName() { |
|
231 |
return skipArrays().typeName(); |
|
232 |
} |
|
233 |
||
234 |
/** |
|
235 |
* Return qualified name of type excluding any dimension information. |
|
236 |
*<p> |
|
237 |
* For example, a two dimensional array of String |
|
238 |
* returns 'java.lang.String'. |
|
239 |
*/ |
|
240 |
public String qualifiedTypeName() { |
|
241 |
return skipArrays().qualifiedTypeName(); |
|
242 |
} |
|
243 |
||
244 |
/** |
|
245 |
* Return the simple name of this type excluding any dimension information. |
|
246 |
*/ |
|
247 |
public String simpleTypeName() { |
|
248 |
return skipArrays().simpleTypeName(); |
|
249 |
} |
|
250 |
||
251 |
/** |
|
252 |
* Return this type as a class. Array dimensions are ignored. |
|
253 |
* |
|
254 |
* @return a ClassDocImpl if the type is a Class. |
|
255 |
* Return null if it is a primitive type.. |
|
256 |
*/ |
|
257 |
public ClassDoc asClassDoc() { |
|
258 |
return skipArrays().asClassDoc(); |
|
259 |
} |
|
260 |
||
261 |
/** |
|
262 |
* Return this type as a <code>ParameterizedType</code> if it |
|
263 |
* represents a parameterized type. Array dimensions are ignored. |
|
264 |
*/ |
|
265 |
public ParameterizedType asParameterizedType() { |
|
266 |
return skipArrays().asParameterizedType(); |
|
267 |
} |
|
268 |
||
269 |
/** |
|
270 |
* Return this type as a <code>TypeVariable</code> if it represents |
|
271 |
* a type variable. Array dimensions are ignored. |
|
272 |
*/ |
|
273 |
public TypeVariable asTypeVariable() { |
|
274 |
return skipArrays().asTypeVariable(); |
|
275 |
} |
|
276 |
||
277 |
/** |
|
278 |
* Return null, as there are no arrays of wildcard types. |
|
279 |
*/ |
|
280 |
public WildcardType asWildcardType() { |
|
281 |
return null; |
|
282 |
} |
|
283 |
||
284 |
/** |
|
285 |
* Return this type as an <code>AnnotationTypeDoc</code> if it |
|
286 |
* represents an annotation type. Array dimensions are ignored. |
|
287 |
*/ |
|
288 |
public AnnotationTypeDoc asAnnotationTypeDoc() { |
|
289 |
return skipArrays().asAnnotationTypeDoc(); |
|
290 |
} |
|
291 |
||
292 |
/** |
|
293 |
* Return true if this is an array of a primitive type. |
|
294 |
*/ |
|
295 |
public boolean isPrimitive() { |
|
296 |
return skipArrays().isPrimitive(); |
|
297 |
} |
|
298 |
||
299 |
/** |
|
300 |
* Return a string representation of the type. |
|
301 |
* |
|
302 |
* Return name of type including any dimension information. |
|
303 |
* <p> |
|
304 |
* For example, a two dimensional array of String returns |
|
305 |
* <code>String[][]</code>. |
|
306 |
* |
|
307 |
* @return name of type including any dimension information. |
|
308 |
*/ |
|
8631
664f84942e74
6866185: Util.getPackageSourcePath should use lastIndexOf not indexOf and related cleanup
jjg
parents:
5520
diff
changeset
|
309 |
@Override |
10 | 310 |
public String toString() { |
311 |
return qualifiedTypeName() + dimension(); |
|
312 |
} |
|
313 |
} |
|
314 |
} |