Sat, 01 Dec 2007 00:00:00 +0000
Initial load
1 /*
2 * Copyright 1997-2005 Sun Microsystems, Inc. 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. Sun designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
22 * CA 95054 USA or visit www.sun.com if you need additional information or
23 * have any questions.
24 */
26 package com.sun.tools.javadoc;
28 import com.sun.javadoc.*;
30 import static com.sun.javadoc.LanguageVersion.*;
32 import com.sun.tools.javac.code.Symbol;
33 import com.sun.tools.javac.code.Symbol.ClassSymbol;
34 import com.sun.tools.javac.code.Type;
35 import com.sun.tools.javac.code.Type.ClassType;
36 import com.sun.tools.javac.code.Type.TypeVar;
37 import com.sun.tools.javac.code.Type.ArrayType;
38 import com.sun.tools.javac.code.Types;
39 import com.sun.tools.javac.util.List;
41 import static com.sun.tools.javac.code.TypeTags.*;
44 public class TypeMaker {
46 public static com.sun.javadoc.Type getType(DocEnv env, Type t) {
47 return getType(env, t, true);
48 }
50 /**
51 * @param errToClassDoc if true, ERROR type results in a ClassDoc;
52 * false preserves legacy behavior
53 */
54 public static com.sun.javadoc.Type getType(DocEnv env, Type t,
55 boolean errToClassDoc) {
56 if (env.legacyDoclet) {
57 t = env.types.erasure(t);
58 }
59 switch (t.tag) {
60 case CLASS:
61 if (ClassDocImpl.isGeneric((ClassSymbol)t.tsym)) {
62 return env.getParameterizedType((ClassType)t);
63 } else {
64 return env.getClassDoc((ClassSymbol)t.tsym);
65 }
66 case WILDCARD:
67 Type.WildcardType a = (Type.WildcardType)t;
68 return new WildcardTypeImpl(env, a);
69 case TYPEVAR: return new TypeVariableImpl(env, (TypeVar)t);
70 case ARRAY: return new ArrayTypeImpl(env, t);
71 case BYTE: return PrimitiveType.byteType;
72 case CHAR: return PrimitiveType.charType;
73 case SHORT: return PrimitiveType.shortType;
74 case INT: return PrimitiveType.intType;
75 case LONG: return PrimitiveType.longType;
76 case FLOAT: return PrimitiveType.floatType;
77 case DOUBLE: return PrimitiveType.doubleType;
78 case BOOLEAN: return PrimitiveType.booleanType;
79 case VOID: return PrimitiveType.voidType;
80 case ERROR:
81 if (errToClassDoc)
82 return env.getClassDoc((ClassSymbol)t.tsym);
83 // FALLTHRU
84 default:
85 return new PrimitiveType(t.tsym.getQualifiedName().toString());
86 }
87 }
89 /**
90 * Convert a list of javac types into an array of javadoc types.
91 */
92 public static com.sun.javadoc.Type[] getTypes(DocEnv env, List<Type> ts) {
93 return getTypes(env, ts, new com.sun.javadoc.Type[ts.length()]);
94 }
96 /**
97 * Like the above version, but use and return the array given.
98 */
99 public static com.sun.javadoc.Type[] getTypes(DocEnv env, List<Type> ts,
100 com.sun.javadoc.Type res[]) {
101 int i = 0;
102 for (Type t : ts) {
103 res[i++] = getType(env, t);
104 }
105 return res;
106 }
108 public static String getTypeName(Type t, boolean full) {
109 switch (t.tag) {
110 case ARRAY:
111 StringBuffer dimension = new StringBuffer();
112 while (t.tag == ARRAY) {
113 dimension = dimension.append("[]");
114 t = ((ArrayType)t).elemtype;
115 }
116 return getTypeName(t, full) + dimension;
117 case CLASS:
118 return ClassDocImpl.getClassName((ClassSymbol)t.tsym, full);
119 default:
120 return t.tsym.getQualifiedName().toString();
121 }
122 }
124 /**
125 * Return the string representation of a type use. Bounds of type
126 * variables are not included; bounds of wildcard types are.
127 * Class names are qualified if "full" is true.
128 */
129 static String getTypeString(DocEnv env, Type t, boolean full) {
130 switch (t.tag) {
131 case ARRAY:
132 StringBuffer dimension = new StringBuffer();
133 while (t.tag == ARRAY) {
134 dimension = dimension.append("[]");
135 t = env.types.elemtype(t);
136 }
137 return getTypeString(env, t, full) + dimension;
138 case CLASS:
139 return ParameterizedTypeImpl.
140 parameterizedTypeToString(env, (ClassType)t, full);
141 case WILDCARD:
142 Type.WildcardType a = (Type.WildcardType)t;
143 return WildcardTypeImpl.wildcardTypeToString(env, a, full);
144 default:
145 return t.tsym.getQualifiedName().toString();
146 }
147 }
149 /**
150 * Return the formal type parameters of a class or method as an
151 * angle-bracketed string. Each parameter is a type variable with
152 * optional bounds. Class names are qualified if "full" is true.
153 * Return "" if there are no type parameters or we're hiding generics.
154 */
155 static String typeParametersString(DocEnv env, Symbol sym, boolean full) {
156 if (env.legacyDoclet || sym.type.getTypeArguments().isEmpty()) {
157 return "";
158 }
159 StringBuffer s = new StringBuffer();
160 for (Type t : sym.type.getTypeArguments()) {
161 s.append(s.length() == 0 ? "<" : ", ");
162 s.append(TypeVariableImpl.typeVarToString(env, (TypeVar)t, full));
163 }
164 s.append(">");
165 return s.toString();
166 }
168 /**
169 * Return the actual type arguments of a parameterized type as an
170 * angle-bracketed string. Class name are qualified if "full" is true.
171 * Return "" if there are no type arguments or we're hiding generics.
172 */
173 static String typeArgumentsString(DocEnv env, ClassType cl, boolean full) {
174 if (env.legacyDoclet || cl.getTypeArguments().isEmpty()) {
175 return "";
176 }
177 StringBuffer s = new StringBuffer();
178 for (Type t : cl.getTypeArguments()) {
179 s.append(s.length() == 0 ? "<" : ", ");
180 s.append(getTypeString(env, t, full));
181 }
182 s.append(">");
183 return s.toString();
184 }
187 private static class ArrayTypeImpl implements com.sun.javadoc.Type {
189 Type arrayType;
191 DocEnv env;
193 ArrayTypeImpl(DocEnv env, Type arrayType) {
194 this.env = env;
195 this.arrayType = arrayType;
196 }
198 private com.sun.javadoc.Type skipArraysCache = null;
200 private com.sun.javadoc.Type skipArrays() {
201 if (skipArraysCache == null) {
202 Type t;
203 for (t = arrayType; t.tag == ARRAY; t = env.types.elemtype(t)) { }
204 skipArraysCache = TypeMaker.getType(env, t);
205 }
206 return skipArraysCache;
207 }
209 /**
210 * Return the type's dimension information, as a string.
211 * <p>
212 * For example, a two dimensional array of String returns '[][]'.
213 */
214 public String dimension() {
215 StringBuffer dimension = new StringBuffer();
216 for (Type t = arrayType; t.tag == ARRAY; t = env.types.elemtype(t)) {
217 dimension = dimension.append("[]");
218 }
219 return dimension.toString();
220 }
222 /**
223 * Return unqualified name of type excluding any dimension information.
224 * <p>
225 * For example, a two dimensional array of String returns 'String'.
226 */
227 public String typeName() {
228 return skipArrays().typeName();
229 }
231 /**
232 * Return qualified name of type excluding any dimension information.
233 *<p>
234 * For example, a two dimensional array of String
235 * returns 'java.lang.String'.
236 */
237 public String qualifiedTypeName() {
238 return skipArrays().qualifiedTypeName();
239 }
241 /**
242 * Return the simple name of this type excluding any dimension information.
243 */
244 public String simpleTypeName() {
245 return skipArrays().simpleTypeName();
246 }
248 /**
249 * Return this type as a class. Array dimensions are ignored.
250 *
251 * @return a ClassDocImpl if the type is a Class.
252 * Return null if it is a primitive type..
253 */
254 public ClassDoc asClassDoc() {
255 return skipArrays().asClassDoc();
256 }
258 /**
259 * Return this type as a <code>ParameterizedType</code> if it
260 * represents a parameterized type. Array dimensions are ignored.
261 */
262 public ParameterizedType asParameterizedType() {
263 return skipArrays().asParameterizedType();
264 }
266 /**
267 * Return this type as a <code>TypeVariable</code> if it represents
268 * a type variable. Array dimensions are ignored.
269 */
270 public TypeVariable asTypeVariable() {
271 return skipArrays().asTypeVariable();
272 }
274 /**
275 * Return null, as there are no arrays of wildcard types.
276 */
277 public WildcardType asWildcardType() {
278 return null;
279 }
281 /**
282 * Return this type as an <code>AnnotationTypeDoc</code> if it
283 * represents an annotation type. Array dimensions are ignored.
284 */
285 public AnnotationTypeDoc asAnnotationTypeDoc() {
286 return skipArrays().asAnnotationTypeDoc();
287 }
289 /**
290 * Return true if this is an array of a primitive type.
291 */
292 public boolean isPrimitive() {
293 return skipArrays().isPrimitive();
294 }
296 /**
297 * Return a string representation of the type.
298 *
299 * Return name of type including any dimension information.
300 * <p>
301 * For example, a two dimensional array of String returns
302 * <code>String[][]</code>.
303 *
304 * @return name of type including any dimension information.
305 */
306 public String toString() {
307 return qualifiedTypeName() + dimension();
308 }
309 }
310 }