Tue, 18 Jun 2013 20:56:04 -0700
8015657: jdk8 l10n resource file translation update 3
Reviewed-by: yhuang
1 /*
2 * Copyright (c) 1997, 2013, 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.javadoc;
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;
32 import com.sun.tools.javac.code.Type.ArrayType;
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.TypeTag.ARRAY;
38 /**
39 * <p><b>This is NOT part of any supported API.
40 * If you write code that depends on this, you do so at your own risk.
41 * This code and its internal interfaces are subject to change or
42 * deletion without notice.</b>
43 */
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 errorToClassDoc) {
56 return getType(env, t, errorToClassDoc, true);
57 }
59 @SuppressWarnings("fallthrough")
60 public static com.sun.javadoc.Type getType(DocEnv env, Type t,
61 boolean errToClassDoc, boolean considerAnnotations) {
62 if (env.legacyDoclet) {
63 t = env.types.erasure(t);
64 }
66 if (considerAnnotations &&
67 t.isAnnotated()) {
68 Type.AnnotatedType at = (Type.AnnotatedType) t;
69 return new AnnotatedTypeImpl(env, at);
70 }
72 switch (t.getTag()) {
73 case CLASS:
74 if (ClassDocImpl.isGeneric((ClassSymbol)t.tsym)) {
75 return env.getParameterizedType((ClassType)t);
76 } else {
77 return env.getClassDoc((ClassSymbol)t.tsym);
78 }
79 case WILDCARD:
80 Type.WildcardType a = (Type.WildcardType)t;
81 return new WildcardTypeImpl(env, a);
82 case TYPEVAR: return new TypeVariableImpl(env, (TypeVar)t);
83 case ARRAY: return new ArrayTypeImpl(env, t);
84 case BYTE: return PrimitiveType.byteType;
85 case CHAR: return PrimitiveType.charType;
86 case SHORT: return PrimitiveType.shortType;
87 case INT: return PrimitiveType.intType;
88 case LONG: return PrimitiveType.longType;
89 case FLOAT: return PrimitiveType.floatType;
90 case DOUBLE: return PrimitiveType.doubleType;
91 case BOOLEAN: return PrimitiveType.booleanType;
92 case VOID: return PrimitiveType.voidType;
93 case ERROR:
94 if (errToClassDoc)
95 return env.getClassDoc((ClassSymbol)t.tsym);
96 // FALLTHRU
97 default:
98 return new PrimitiveType(t.tsym.getQualifiedName().toString());
99 }
100 }
102 /**
103 * Convert a list of javac types into an array of javadoc types.
104 */
105 public static com.sun.javadoc.Type[] getTypes(DocEnv env, List<Type> ts) {
106 return getTypes(env, ts, new com.sun.javadoc.Type[ts.length()]);
107 }
109 /**
110 * Like the above version, but use and return the array given.
111 */
112 public static com.sun.javadoc.Type[] getTypes(DocEnv env, List<Type> ts,
113 com.sun.javadoc.Type res[]) {
114 int i = 0;
115 for (Type t : ts) {
116 res[i++] = getType(env, t);
117 }
118 return res;
119 }
121 public static String getTypeName(Type t, boolean full) {
122 switch (t.getTag()) {
123 case ARRAY:
124 StringBuilder s = new StringBuilder();
125 while (t.hasTag(ARRAY)) {
126 s.append("[]");
127 t = ((ArrayType)t).elemtype;
128 }
129 s.insert(0, getTypeName(t, full));
130 return s.toString();
131 case CLASS:
132 return ClassDocImpl.getClassName((ClassSymbol)t.tsym, full);
133 default:
134 return t.tsym.getQualifiedName().toString();
135 }
136 }
138 /**
139 * Return the string representation of a type use. Bounds of type
140 * variables are not included; bounds of wildcard types are.
141 * Class names are qualified if "full" is true.
142 */
143 static String getTypeString(DocEnv env, Type t, boolean full) {
144 // TODO: should annotations be included here?
145 if (t.isAnnotated()) {
146 Type.AnnotatedType at = (Type.AnnotatedType)t;
147 t = at.underlyingType;
148 }
149 switch (t.getTag()) {
150 case ARRAY:
151 StringBuilder s = new StringBuilder();
152 while (t.hasTag(ARRAY)) {
153 s.append("[]");
154 t = env.types.elemtype(t);
155 }
156 s.insert(0, getTypeString(env, t, full));
157 return s.toString();
158 case CLASS:
159 return ParameterizedTypeImpl.
160 parameterizedTypeToString(env, (ClassType)t, full);
161 case WILDCARD:
162 Type.WildcardType a = (Type.WildcardType)t;
163 return WildcardTypeImpl.wildcardTypeToString(env, a, full);
164 default:
165 return t.tsym.getQualifiedName().toString();
166 }
167 }
169 /**
170 * Return the formal type parameters of a class or method as an
171 * angle-bracketed string. Each parameter is a type variable with
172 * optional bounds. Class names are qualified if "full" is true.
173 * Return "" if there are no type parameters or we're hiding generics.
174 */
175 static String typeParametersString(DocEnv env, Symbol sym, boolean full) {
176 if (env.legacyDoclet || sym.type.getTypeArguments().isEmpty()) {
177 return "";
178 }
179 StringBuilder s = new StringBuilder();
180 for (Type t : sym.type.getTypeArguments()) {
181 s.append(s.length() == 0 ? "<" : ", ");
182 s.append(TypeVariableImpl.typeVarToString(env, (TypeVar)t, full));
183 }
184 s.append(">");
185 return s.toString();
186 }
188 /**
189 * Return the actual type arguments of a parameterized type as an
190 * angle-bracketed string. Class name are qualified if "full" is true.
191 * Return "" if there are no type arguments or we're hiding generics.
192 */
193 static String typeArgumentsString(DocEnv env, ClassType cl, boolean full) {
194 if (env.legacyDoclet || cl.getTypeArguments().isEmpty()) {
195 return "";
196 }
197 StringBuilder s = new StringBuilder();
198 for (Type t : cl.getTypeArguments()) {
199 s.append(s.length() == 0 ? "<" : ", ");
200 s.append(getTypeString(env, t, full));
201 }
202 s.append(">");
203 return s.toString();
204 }
207 private static class ArrayTypeImpl implements com.sun.javadoc.Type {
209 Type arrayType;
211 DocEnv env;
213 ArrayTypeImpl(DocEnv env, Type arrayType) {
214 this.env = env;
215 this.arrayType = arrayType;
216 }
218 private com.sun.javadoc.Type skipArraysCache = null;
220 public com.sun.javadoc.Type getElementType() {
221 return TypeMaker.getType(env, env.types.elemtype(arrayType));
222 }
224 private com.sun.javadoc.Type skipArrays() {
225 if (skipArraysCache == null) {
226 Type t;
227 for (t = arrayType; t.hasTag(ARRAY); t = env.types.elemtype(t)) { }
228 skipArraysCache = TypeMaker.getType(env, t);
229 }
230 return skipArraysCache;
231 }
233 /**
234 * Return the type's dimension information, as a string.
235 * <p>
236 * For example, a two dimensional array of String returns '[][]'.
237 */
238 public String dimension() {
239 StringBuilder dimension = new StringBuilder();
240 for (Type t = arrayType; t.hasTag(ARRAY); t = env.types.elemtype(t)) {
241 dimension.append("[]");
242 }
243 return dimension.toString();
244 }
246 /**
247 * Return unqualified name of type excluding any dimension information.
248 * <p>
249 * For example, a two dimensional array of String returns 'String'.
250 */
251 public String typeName() {
252 return skipArrays().typeName();
253 }
255 /**
256 * Return qualified name of type excluding any dimension information.
257 *<p>
258 * For example, a two dimensional array of String
259 * returns 'java.lang.String'.
260 */
261 public String qualifiedTypeName() {
262 return skipArrays().qualifiedTypeName();
263 }
265 /**
266 * Return the simple name of this type excluding any dimension information.
267 */
268 public String simpleTypeName() {
269 return skipArrays().simpleTypeName();
270 }
272 /**
273 * Return this type as a class. Array dimensions are ignored.
274 *
275 * @return a ClassDocImpl if the type is a Class.
276 * Return null if it is a primitive type..
277 */
278 public ClassDoc asClassDoc() {
279 return skipArrays().asClassDoc();
280 }
282 /**
283 * Return this type as a <code>ParameterizedType</code> if it
284 * represents a parameterized type. Array dimensions are ignored.
285 */
286 public ParameterizedType asParameterizedType() {
287 return skipArrays().asParameterizedType();
288 }
290 /**
291 * Return this type as a <code>TypeVariable</code> if it represents
292 * a type variable. Array dimensions are ignored.
293 */
294 public TypeVariable asTypeVariable() {
295 return skipArrays().asTypeVariable();
296 }
298 /**
299 * Return null, as there are no arrays of wildcard types.
300 */
301 public WildcardType asWildcardType() {
302 return null;
303 }
305 /**
306 * Return null, as there are no annotations of the type
307 */
308 public AnnotatedType asAnnotatedType() {
309 return null;
310 }
312 /**
313 * Return this type as an <code>AnnotationTypeDoc</code> if it
314 * represents an annotation type. Array dimensions are ignored.
315 */
316 public AnnotationTypeDoc asAnnotationTypeDoc() {
317 return skipArrays().asAnnotationTypeDoc();
318 }
320 /**
321 * Return true if this is an array of a primitive type.
322 */
323 public boolean isPrimitive() {
324 return skipArrays().isPrimitive();
325 }
327 /**
328 * Return a string representation of the type.
329 *
330 * Return name of type including any dimension information.
331 * <p>
332 * For example, a two dimensional array of String returns
333 * <code>String[][]</code>.
334 *
335 * @return name of type including any dimension information.
336 */
337 @Override
338 public String toString() {
339 return qualifiedTypeName() + dimension();
340 }
341 }
342 }