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