author | ksrini |
Fri, 09 Feb 2018 13:58:17 -0800 | |
changeset 48840 | 5e2d2067da48 |
parent 47216 | 71c04702a3d5 |
permissions | -rw-r--r-- |
10 | 1 |
/* |
48840
5e2d2067da48
8194651: javadoc: mark the com.sun.javadoc API for removal
ksrini
parents:
47216
diff
changeset
|
2 |
* Copyright (c) 1997, 2018, 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 |
||
37938
42baa89d2156
8075703: jdk.javadoc module exports com.sun.tools.javadoc package which contains a lot of internal API.
jjg
parents:
32800
diff
changeset
|
26 |
package com.sun.tools.javadoc.main; |
10 | 27 |
|
28 |
import com.sun.javadoc.*; |
|
29 |
import com.sun.tools.javac.code.Symbol; |
|
30 |
import com.sun.tools.javac.code.Symbol.ClassSymbol; |
|
32800
8457813125e3
8135307: CompletionFailure thrown when calling FieldDoc.type, if the field's type is missing
jlahoda
parents:
25874
diff
changeset
|
31 |
import com.sun.tools.javac.code.Symbol.CompletionFailure; |
10 | 32 |
import com.sun.tools.javac.code.Type; |
14258 | 33 |
import com.sun.tools.javac.code.Type.ArrayType; |
10 | 34 |
import com.sun.tools.javac.code.Type.ClassType; |
35 |
import com.sun.tools.javac.code.Type.TypeVar; |
|
36 |
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
|
37 |
import static com.sun.tools.javac.code.TypeTag.ARRAY; |
10 | 38 |
|
14260
727a84636f12
8000665: fix "internal API" comments on javadoc files
jjg
parents:
14258
diff
changeset
|
39 |
/** |
727a84636f12
8000665: fix "internal API" comments on javadoc files
jjg
parents:
14258
diff
changeset
|
40 |
* <p><b>This is NOT part of any supported API. |
727a84636f12
8000665: fix "internal API" comments on javadoc files
jjg
parents:
14258
diff
changeset
|
41 |
* 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
|
42 |
* 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
|
43 |
* deletion without notice.</b> |
727a84636f12
8000665: fix "internal API" comments on javadoc files
jjg
parents:
14258
diff
changeset
|
44 |
*/ |
48840
5e2d2067da48
8194651: javadoc: mark the com.sun.javadoc API for removal
ksrini
parents:
47216
diff
changeset
|
45 |
@Deprecated(since="9", forRemoval=true) |
5e2d2067da48
8194651: javadoc: mark the com.sun.javadoc API for removal
ksrini
parents:
47216
diff
changeset
|
46 |
@SuppressWarnings("removal") |
10 | 47 |
public class TypeMaker { |
48 |
||
49 |
public static com.sun.javadoc.Type getType(DocEnv env, Type t) { |
|
50 |
return getType(env, t, true); |
|
51 |
} |
|
52 |
||
53 |
/** |
|
54 |
* @param errToClassDoc if true, ERROR type results in a ClassDoc; |
|
55 |
* false preserves legacy behavior |
|
56 |
*/ |
|
15385 | 57 |
public static com.sun.javadoc.Type getType(DocEnv env, Type t, |
58 |
boolean errorToClassDoc) { |
|
59 |
return getType(env, t, errorToClassDoc, true); |
|
60 |
} |
|
61 |
||
32800
8457813125e3
8135307: CompletionFailure thrown when calling FieldDoc.type, if the field's type is missing
jlahoda
parents:
25874
diff
changeset
|
62 |
public static com.sun.javadoc.Type getType(DocEnv env, Type t, |
8457813125e3
8135307: CompletionFailure thrown when calling FieldDoc.type, if the field's type is missing
jlahoda
parents:
25874
diff
changeset
|
63 |
boolean errToClassDoc, boolean considerAnnotations) { |
8457813125e3
8135307: CompletionFailure thrown when calling FieldDoc.type, if the field's type is missing
jlahoda
parents:
25874
diff
changeset
|
64 |
try { |
8457813125e3
8135307: CompletionFailure thrown when calling FieldDoc.type, if the field's type is missing
jlahoda
parents:
25874
diff
changeset
|
65 |
return getTypeImpl(env, t, errToClassDoc, considerAnnotations); |
8457813125e3
8135307: CompletionFailure thrown when calling FieldDoc.type, if the field's type is missing
jlahoda
parents:
25874
diff
changeset
|
66 |
} catch (CompletionFailure cf) { |
8457813125e3
8135307: CompletionFailure thrown when calling FieldDoc.type, if the field's type is missing
jlahoda
parents:
25874
diff
changeset
|
67 |
/* Quietly ignore completion failures and try again - the type |
8457813125e3
8135307: CompletionFailure thrown when calling FieldDoc.type, if the field's type is missing
jlahoda
parents:
25874
diff
changeset
|
68 |
* for which the CompletionFailure was thrown shouldn't be completed |
8457813125e3
8135307: CompletionFailure thrown when calling FieldDoc.type, if the field's type is missing
jlahoda
parents:
25874
diff
changeset
|
69 |
* again by the completer that threw the CompletionFailure. |
8457813125e3
8135307: CompletionFailure thrown when calling FieldDoc.type, if the field's type is missing
jlahoda
parents:
25874
diff
changeset
|
70 |
*/ |
8457813125e3
8135307: CompletionFailure thrown when calling FieldDoc.type, if the field's type is missing
jlahoda
parents:
25874
diff
changeset
|
71 |
return getType(env, t, errToClassDoc, considerAnnotations); |
8457813125e3
8135307: CompletionFailure thrown when calling FieldDoc.type, if the field's type is missing
jlahoda
parents:
25874
diff
changeset
|
72 |
} |
8457813125e3
8135307: CompletionFailure thrown when calling FieldDoc.type, if the field's type is missing
jlahoda
parents:
25874
diff
changeset
|
73 |
} |
8457813125e3
8135307: CompletionFailure thrown when calling FieldDoc.type, if the field's type is missing
jlahoda
parents:
25874
diff
changeset
|
74 |
|
1870
57a1138dffc8
6795903: fix latent build warnings in langtools repository
jjg
parents:
10
diff
changeset
|
75 |
@SuppressWarnings("fallthrough") |
32800
8457813125e3
8135307: CompletionFailure thrown when calling FieldDoc.type, if the field's type is missing
jlahoda
parents:
25874
diff
changeset
|
76 |
private static com.sun.javadoc.Type getTypeImpl(DocEnv env, Type t, |
15385 | 77 |
boolean errToClassDoc, boolean considerAnnotations) { |
10 | 78 |
if (env.legacyDoclet) { |
79 |
t = env.types.erasure(t); |
|
80 |
} |
|
15385 | 81 |
|
19906
2698aaacb4b2
8023833: Replace direct use of AnnotatedType in javadoc code
jjg
parents:
17578
diff
changeset
|
82 |
if (considerAnnotations && t.isAnnotated()) { |
2698aaacb4b2
8023833: Replace direct use of AnnotatedType in javadoc code
jjg
parents:
17578
diff
changeset
|
83 |
return new AnnotatedTypeImpl(env, t); |
15385 | 84 |
} |
85 |
||
14359
d4099818ab70
7200915: convert TypeTags from a series of small ints to an enum
jjg
parents:
14260
diff
changeset
|
86 |
switch (t.getTag()) { |
10 | 87 |
case CLASS: |
88 |
if (ClassDocImpl.isGeneric((ClassSymbol)t.tsym)) { |
|
89 |
return env.getParameterizedType((ClassType)t); |
|
90 |
} else { |
|
91 |
return env.getClassDoc((ClassSymbol)t.tsym); |
|
92 |
} |
|
93 |
case WILDCARD: |
|
94 |
Type.WildcardType a = (Type.WildcardType)t; |
|
95 |
return new WildcardTypeImpl(env, a); |
|
96 |
case TYPEVAR: return new TypeVariableImpl(env, (TypeVar)t); |
|
97 |
case ARRAY: return new ArrayTypeImpl(env, t); |
|
98 |
case BYTE: return PrimitiveType.byteType; |
|
99 |
case CHAR: return PrimitiveType.charType; |
|
100 |
case SHORT: return PrimitiveType.shortType; |
|
101 |
case INT: return PrimitiveType.intType; |
|
102 |
case LONG: return PrimitiveType.longType; |
|
103 |
case FLOAT: return PrimitiveType.floatType; |
|
104 |
case DOUBLE: return PrimitiveType.doubleType; |
|
105 |
case BOOLEAN: return PrimitiveType.booleanType; |
|
106 |
case VOID: return PrimitiveType.voidType; |
|
107 |
case ERROR: |
|
108 |
if (errToClassDoc) |
|
109 |
return env.getClassDoc((ClassSymbol)t.tsym); |
|
110 |
// FALLTHRU |
|
111 |
default: |
|
112 |
return new PrimitiveType(t.tsym.getQualifiedName().toString()); |
|
113 |
} |
|
114 |
} |
|
115 |
||
116 |
/** |
|
117 |
* Convert a list of javac types into an array of javadoc types. |
|
118 |
*/ |
|
119 |
public static com.sun.javadoc.Type[] getTypes(DocEnv env, List<Type> ts) { |
|
120 |
return getTypes(env, ts, new com.sun.javadoc.Type[ts.length()]); |
|
121 |
} |
|
122 |
||
123 |
/** |
|
124 |
* Like the above version, but use and return the array given. |
|
125 |
*/ |
|
126 |
public static com.sun.javadoc.Type[] getTypes(DocEnv env, List<Type> ts, |
|
127 |
com.sun.javadoc.Type res[]) { |
|
128 |
int i = 0; |
|
129 |
for (Type t : ts) { |
|
130 |
res[i++] = getType(env, t); |
|
131 |
} |
|
132 |
return res; |
|
133 |
} |
|
134 |
||
135 |
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
|
136 |
switch (t.getTag()) { |
10 | 137 |
case ARRAY: |
8631
664f84942e74
6866185: Util.getPackageSourcePath should use lastIndexOf not indexOf and related cleanup
jjg
parents:
5520
diff
changeset
|
138 |
StringBuilder s = new StringBuilder(); |
14359
d4099818ab70
7200915: convert TypeTags from a series of small ints to an enum
jjg
parents:
14260
diff
changeset
|
139 |
while (t.hasTag(ARRAY)) { |
8631
664f84942e74
6866185: Util.getPackageSourcePath should use lastIndexOf not indexOf and related cleanup
jjg
parents:
5520
diff
changeset
|
140 |
s.append("[]"); |
10 | 141 |
t = ((ArrayType)t).elemtype; |
142 |
} |
|
8631
664f84942e74
6866185: Util.getPackageSourcePath should use lastIndexOf not indexOf and related cleanup
jjg
parents:
5520
diff
changeset
|
143 |
s.insert(0, getTypeName(t, full)); |
664f84942e74
6866185: Util.getPackageSourcePath should use lastIndexOf not indexOf and related cleanup
jjg
parents:
5520
diff
changeset
|
144 |
return s.toString(); |
10 | 145 |
case CLASS: |
146 |
return ClassDocImpl.getClassName((ClassSymbol)t.tsym, full); |
|
147 |
default: |
|
148 |
return t.tsym.getQualifiedName().toString(); |
|
149 |
} |
|
150 |
} |
|
151 |
||
152 |
/** |
|
153 |
* Return the string representation of a type use. Bounds of type |
|
154 |
* variables are not included; bounds of wildcard types are. |
|
155 |
* Class names are qualified if "full" is true. |
|
156 |
*/ |
|
157 |
static String getTypeString(DocEnv env, Type t, boolean full) { |
|
15385 | 158 |
// TODO: should annotations be included here? |
14359
d4099818ab70
7200915: convert TypeTags from a series of small ints to an enum
jjg
parents:
14260
diff
changeset
|
159 |
switch (t.getTag()) { |
10 | 160 |
case ARRAY: |
8631
664f84942e74
6866185: Util.getPackageSourcePath should use lastIndexOf not indexOf and related cleanup
jjg
parents:
5520
diff
changeset
|
161 |
StringBuilder s = new StringBuilder(); |
14359
d4099818ab70
7200915: convert TypeTags from a series of small ints to an enum
jjg
parents:
14260
diff
changeset
|
162 |
while (t.hasTag(ARRAY)) { |
8631
664f84942e74
6866185: Util.getPackageSourcePath should use lastIndexOf not indexOf and related cleanup
jjg
parents:
5520
diff
changeset
|
163 |
s.append("[]"); |
10 | 164 |
t = env.types.elemtype(t); |
165 |
} |
|
8631
664f84942e74
6866185: Util.getPackageSourcePath should use lastIndexOf not indexOf and related cleanup
jjg
parents:
5520
diff
changeset
|
166 |
s.insert(0, getTypeString(env, t, full)); |
664f84942e74
6866185: Util.getPackageSourcePath should use lastIndexOf not indexOf and related cleanup
jjg
parents:
5520
diff
changeset
|
167 |
return s.toString(); |
10 | 168 |
case CLASS: |
169 |
return ParameterizedTypeImpl. |
|
170 |
parameterizedTypeToString(env, (ClassType)t, full); |
|
171 |
case WILDCARD: |
|
172 |
Type.WildcardType a = (Type.WildcardType)t; |
|
173 |
return WildcardTypeImpl.wildcardTypeToString(env, a, full); |
|
174 |
default: |
|
175 |
return t.tsym.getQualifiedName().toString(); |
|
176 |
} |
|
177 |
} |
|
178 |
||
179 |
/** |
|
180 |
* Return the formal type parameters of a class or method as an |
|
181 |
* angle-bracketed string. Each parameter is a type variable with |
|
182 |
* optional bounds. Class names are qualified if "full" is true. |
|
183 |
* Return "" if there are no type parameters or we're hiding generics. |
|
184 |
*/ |
|
185 |
static String typeParametersString(DocEnv env, Symbol sym, boolean full) { |
|
186 |
if (env.legacyDoclet || sym.type.getTypeArguments().isEmpty()) { |
|
187 |
return ""; |
|
188 |
} |
|
8631
664f84942e74
6866185: Util.getPackageSourcePath should use lastIndexOf not indexOf and related cleanup
jjg
parents:
5520
diff
changeset
|
189 |
StringBuilder s = new StringBuilder(); |
10 | 190 |
for (Type t : sym.type.getTypeArguments()) { |
191 |
s.append(s.length() == 0 ? "<" : ", "); |
|
192 |
s.append(TypeVariableImpl.typeVarToString(env, (TypeVar)t, full)); |
|
193 |
} |
|
194 |
s.append(">"); |
|
195 |
return s.toString(); |
|
196 |
} |
|
197 |
||
198 |
/** |
|
199 |
* Return the actual type arguments of a parameterized type as an |
|
200 |
* angle-bracketed string. Class name are qualified if "full" is true. |
|
201 |
* Return "" if there are no type arguments or we're hiding generics. |
|
202 |
*/ |
|
203 |
static String typeArgumentsString(DocEnv env, ClassType cl, boolean full) { |
|
204 |
if (env.legacyDoclet || cl.getTypeArguments().isEmpty()) { |
|
205 |
return ""; |
|
206 |
} |
|
8631
664f84942e74
6866185: Util.getPackageSourcePath should use lastIndexOf not indexOf and related cleanup
jjg
parents:
5520
diff
changeset
|
207 |
StringBuilder s = new StringBuilder(); |
10 | 208 |
for (Type t : cl.getTypeArguments()) { |
209 |
s.append(s.length() == 0 ? "<" : ", "); |
|
210 |
s.append(getTypeString(env, t, full)); |
|
211 |
} |
|
212 |
s.append(">"); |
|
213 |
return s.toString(); |
|
214 |
} |
|
215 |
||
216 |
||
217 |
private static class ArrayTypeImpl implements com.sun.javadoc.Type { |
|
218 |
||
219 |
Type arrayType; |
|
220 |
||
221 |
DocEnv env; |
|
222 |
||
223 |
ArrayTypeImpl(DocEnv env, Type arrayType) { |
|
224 |
this.env = env; |
|
225 |
this.arrayType = arrayType; |
|
226 |
} |
|
227 |
||
228 |
private com.sun.javadoc.Type skipArraysCache = null; |
|
229 |
||
16969
b58d8a70d921
8009686: Generated javadoc documentation should be able to display type annotation on an array
bpatel
parents:
16556
diff
changeset
|
230 |
public com.sun.javadoc.Type getElementType() { |
b58d8a70d921
8009686: Generated javadoc documentation should be able to display type annotation on an array
bpatel
parents:
16556
diff
changeset
|
231 |
return TypeMaker.getType(env, env.types.elemtype(arrayType)); |
b58d8a70d921
8009686: Generated javadoc documentation should be able to display type annotation on an array
bpatel
parents:
16556
diff
changeset
|
232 |
} |
b58d8a70d921
8009686: Generated javadoc documentation should be able to display type annotation on an array
bpatel
parents:
16556
diff
changeset
|
233 |
|
10 | 234 |
private com.sun.javadoc.Type skipArrays() { |
235 |
if (skipArraysCache == null) { |
|
236 |
Type t; |
|
14359
d4099818ab70
7200915: convert TypeTags from a series of small ints to an enum
jjg
parents:
14260
diff
changeset
|
237 |
for (t = arrayType; t.hasTag(ARRAY); t = env.types.elemtype(t)) { } |
10 | 238 |
skipArraysCache = TypeMaker.getType(env, t); |
239 |
} |
|
240 |
return skipArraysCache; |
|
241 |
} |
|
242 |
||
243 |
/** |
|
244 |
* Return the type's dimension information, as a string. |
|
245 |
* <p> |
|
246 |
* For example, a two dimensional array of String returns '[][]'. |
|
247 |
*/ |
|
248 |
public String dimension() { |
|
8631
664f84942e74
6866185: Util.getPackageSourcePath should use lastIndexOf not indexOf and related cleanup
jjg
parents:
5520
diff
changeset
|
249 |
StringBuilder dimension = new StringBuilder(); |
14359
d4099818ab70
7200915: convert TypeTags from a series of small ints to an enum
jjg
parents:
14260
diff
changeset
|
250 |
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
|
251 |
dimension.append("[]"); |
10 | 252 |
} |
253 |
return dimension.toString(); |
|
254 |
} |
|
255 |
||
256 |
/** |
|
257 |
* Return unqualified name of type excluding any dimension information. |
|
258 |
* <p> |
|
259 |
* For example, a two dimensional array of String returns 'String'. |
|
260 |
*/ |
|
261 |
public String typeName() { |
|
262 |
return skipArrays().typeName(); |
|
263 |
} |
|
264 |
||
265 |
/** |
|
266 |
* Return qualified name of type excluding any dimension information. |
|
267 |
*<p> |
|
268 |
* For example, a two dimensional array of String |
|
269 |
* returns 'java.lang.String'. |
|
270 |
*/ |
|
271 |
public String qualifiedTypeName() { |
|
272 |
return skipArrays().qualifiedTypeName(); |
|
273 |
} |
|
274 |
||
275 |
/** |
|
276 |
* Return the simple name of this type excluding any dimension information. |
|
277 |
*/ |
|
278 |
public String simpleTypeName() { |
|
279 |
return skipArrays().simpleTypeName(); |
|
280 |
} |
|
281 |
||
282 |
/** |
|
283 |
* Return this type as a class. Array dimensions are ignored. |
|
284 |
* |
|
285 |
* @return a ClassDocImpl if the type is a Class. |
|
286 |
* Return null if it is a primitive type.. |
|
287 |
*/ |
|
288 |
public ClassDoc asClassDoc() { |
|
289 |
return skipArrays().asClassDoc(); |
|
290 |
} |
|
291 |
||
292 |
/** |
|
293 |
* Return this type as a <code>ParameterizedType</code> if it |
|
294 |
* represents a parameterized type. Array dimensions are ignored. |
|
295 |
*/ |
|
296 |
public ParameterizedType asParameterizedType() { |
|
297 |
return skipArrays().asParameterizedType(); |
|
298 |
} |
|
299 |
||
300 |
/** |
|
301 |
* Return this type as a <code>TypeVariable</code> if it represents |
|
302 |
* a type variable. Array dimensions are ignored. |
|
303 |
*/ |
|
304 |
public TypeVariable asTypeVariable() { |
|
305 |
return skipArrays().asTypeVariable(); |
|
306 |
} |
|
307 |
||
308 |
/** |
|
309 |
* Return null, as there are no arrays of wildcard types. |
|
310 |
*/ |
|
311 |
public WildcardType asWildcardType() { |
|
312 |
return null; |
|
313 |
} |
|
314 |
||
315 |
/** |
|
15385 | 316 |
* Return null, as there are no annotations of the type |
317 |
*/ |
|
318 |
public AnnotatedType asAnnotatedType() { |
|
319 |
return null; |
|
320 |
} |
|
321 |
||
322 |
/** |
|
10 | 323 |
* Return this type as an <code>AnnotationTypeDoc</code> if it |
324 |
* represents an annotation type. Array dimensions are ignored. |
|
325 |
*/ |
|
326 |
public AnnotationTypeDoc asAnnotationTypeDoc() { |
|
327 |
return skipArrays().asAnnotationTypeDoc(); |
|
328 |
} |
|
329 |
||
330 |
/** |
|
331 |
* Return true if this is an array of a primitive type. |
|
332 |
*/ |
|
333 |
public boolean isPrimitive() { |
|
334 |
return skipArrays().isPrimitive(); |
|
335 |
} |
|
336 |
||
337 |
/** |
|
338 |
* Return a string representation of the type. |
|
339 |
* |
|
340 |
* Return name of type including any dimension information. |
|
341 |
* <p> |
|
342 |
* For example, a two dimensional array of String returns |
|
343 |
* <code>String[][]</code>. |
|
344 |
* |
|
345 |
* @return name of type including any dimension information. |
|
346 |
*/ |
|
8631
664f84942e74
6866185: Util.getPackageSourcePath should use lastIndexOf not indexOf and related cleanup
jjg
parents:
5520
diff
changeset
|
347 |
@Override |
10 | 348 |
public String toString() { |
349 |
return qualifiedTypeName() + dimension(); |
|
350 |
} |
|
351 |
} |
|
352 |
} |