1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/share/classes/com/sun/tools/javadoc/TypeMaker.java Sat Dec 01 00:00:00 2007 +0000 1.3 @@ -0,0 +1,310 @@ 1.4 +/* 1.5 + * Copyright 1997-2005 Sun Microsystems, Inc. All Rights Reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. Sun designates this 1.11 + * particular file as subject to the "Classpath" exception as provided 1.12 + * by Sun in the LICENSE file that accompanied this code. 1.13 + * 1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.17 + * version 2 for more details (a copy is included in the LICENSE file that 1.18 + * accompanied this code). 1.19 + * 1.20 + * You should have received a copy of the GNU General Public License version 1.21 + * 2 along with this work; if not, write to the Free Software Foundation, 1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.23 + * 1.24 + * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, 1.25 + * CA 95054 USA or visit www.sun.com if you need additional information or 1.26 + * have any questions. 1.27 + */ 1.28 + 1.29 +package com.sun.tools.javadoc; 1.30 + 1.31 +import com.sun.javadoc.*; 1.32 + 1.33 +import static com.sun.javadoc.LanguageVersion.*; 1.34 + 1.35 +import com.sun.tools.javac.code.Symbol; 1.36 +import com.sun.tools.javac.code.Symbol.ClassSymbol; 1.37 +import com.sun.tools.javac.code.Type; 1.38 +import com.sun.tools.javac.code.Type.ClassType; 1.39 +import com.sun.tools.javac.code.Type.TypeVar; 1.40 +import com.sun.tools.javac.code.Type.ArrayType; 1.41 +import com.sun.tools.javac.code.Types; 1.42 +import com.sun.tools.javac.util.List; 1.43 + 1.44 +import static com.sun.tools.javac.code.TypeTags.*; 1.45 + 1.46 + 1.47 +public class TypeMaker { 1.48 + 1.49 + public static com.sun.javadoc.Type getType(DocEnv env, Type t) { 1.50 + return getType(env, t, true); 1.51 + } 1.52 + 1.53 + /** 1.54 + * @param errToClassDoc if true, ERROR type results in a ClassDoc; 1.55 + * false preserves legacy behavior 1.56 + */ 1.57 + public static com.sun.javadoc.Type getType(DocEnv env, Type t, 1.58 + boolean errToClassDoc) { 1.59 + if (env.legacyDoclet) { 1.60 + t = env.types.erasure(t); 1.61 + } 1.62 + switch (t.tag) { 1.63 + case CLASS: 1.64 + if (ClassDocImpl.isGeneric((ClassSymbol)t.tsym)) { 1.65 + return env.getParameterizedType((ClassType)t); 1.66 + } else { 1.67 + return env.getClassDoc((ClassSymbol)t.tsym); 1.68 + } 1.69 + case WILDCARD: 1.70 + Type.WildcardType a = (Type.WildcardType)t; 1.71 + return new WildcardTypeImpl(env, a); 1.72 + case TYPEVAR: return new TypeVariableImpl(env, (TypeVar)t); 1.73 + case ARRAY: return new ArrayTypeImpl(env, t); 1.74 + case BYTE: return PrimitiveType.byteType; 1.75 + case CHAR: return PrimitiveType.charType; 1.76 + case SHORT: return PrimitiveType.shortType; 1.77 + case INT: return PrimitiveType.intType; 1.78 + case LONG: return PrimitiveType.longType; 1.79 + case FLOAT: return PrimitiveType.floatType; 1.80 + case DOUBLE: return PrimitiveType.doubleType; 1.81 + case BOOLEAN: return PrimitiveType.booleanType; 1.82 + case VOID: return PrimitiveType.voidType; 1.83 + case ERROR: 1.84 + if (errToClassDoc) 1.85 + return env.getClassDoc((ClassSymbol)t.tsym); 1.86 + // FALLTHRU 1.87 + default: 1.88 + return new PrimitiveType(t.tsym.getQualifiedName().toString()); 1.89 + } 1.90 + } 1.91 + 1.92 + /** 1.93 + * Convert a list of javac types into an array of javadoc types. 1.94 + */ 1.95 + public static com.sun.javadoc.Type[] getTypes(DocEnv env, List<Type> ts) { 1.96 + return getTypes(env, ts, new com.sun.javadoc.Type[ts.length()]); 1.97 + } 1.98 + 1.99 + /** 1.100 + * Like the above version, but use and return the array given. 1.101 + */ 1.102 + public static com.sun.javadoc.Type[] getTypes(DocEnv env, List<Type> ts, 1.103 + com.sun.javadoc.Type res[]) { 1.104 + int i = 0; 1.105 + for (Type t : ts) { 1.106 + res[i++] = getType(env, t); 1.107 + } 1.108 + return res; 1.109 + } 1.110 + 1.111 + public static String getTypeName(Type t, boolean full) { 1.112 + switch (t.tag) { 1.113 + case ARRAY: 1.114 + StringBuffer dimension = new StringBuffer(); 1.115 + while (t.tag == ARRAY) { 1.116 + dimension = dimension.append("[]"); 1.117 + t = ((ArrayType)t).elemtype; 1.118 + } 1.119 + return getTypeName(t, full) + dimension; 1.120 + case CLASS: 1.121 + return ClassDocImpl.getClassName((ClassSymbol)t.tsym, full); 1.122 + default: 1.123 + return t.tsym.getQualifiedName().toString(); 1.124 + } 1.125 + } 1.126 + 1.127 + /** 1.128 + * Return the string representation of a type use. Bounds of type 1.129 + * variables are not included; bounds of wildcard types are. 1.130 + * Class names are qualified if "full" is true. 1.131 + */ 1.132 + static String getTypeString(DocEnv env, Type t, boolean full) { 1.133 + switch (t.tag) { 1.134 + case ARRAY: 1.135 + StringBuffer dimension = new StringBuffer(); 1.136 + while (t.tag == ARRAY) { 1.137 + dimension = dimension.append("[]"); 1.138 + t = env.types.elemtype(t); 1.139 + } 1.140 + return getTypeString(env, t, full) + dimension; 1.141 + case CLASS: 1.142 + return ParameterizedTypeImpl. 1.143 + parameterizedTypeToString(env, (ClassType)t, full); 1.144 + case WILDCARD: 1.145 + Type.WildcardType a = (Type.WildcardType)t; 1.146 + return WildcardTypeImpl.wildcardTypeToString(env, a, full); 1.147 + default: 1.148 + return t.tsym.getQualifiedName().toString(); 1.149 + } 1.150 + } 1.151 + 1.152 + /** 1.153 + * Return the formal type parameters of a class or method as an 1.154 + * angle-bracketed string. Each parameter is a type variable with 1.155 + * optional bounds. Class names are qualified if "full" is true. 1.156 + * Return "" if there are no type parameters or we're hiding generics. 1.157 + */ 1.158 + static String typeParametersString(DocEnv env, Symbol sym, boolean full) { 1.159 + if (env.legacyDoclet || sym.type.getTypeArguments().isEmpty()) { 1.160 + return ""; 1.161 + } 1.162 + StringBuffer s = new StringBuffer(); 1.163 + for (Type t : sym.type.getTypeArguments()) { 1.164 + s.append(s.length() == 0 ? "<" : ", "); 1.165 + s.append(TypeVariableImpl.typeVarToString(env, (TypeVar)t, full)); 1.166 + } 1.167 + s.append(">"); 1.168 + return s.toString(); 1.169 + } 1.170 + 1.171 + /** 1.172 + * Return the actual type arguments of a parameterized type as an 1.173 + * angle-bracketed string. Class name are qualified if "full" is true. 1.174 + * Return "" if there are no type arguments or we're hiding generics. 1.175 + */ 1.176 + static String typeArgumentsString(DocEnv env, ClassType cl, boolean full) { 1.177 + if (env.legacyDoclet || cl.getTypeArguments().isEmpty()) { 1.178 + return ""; 1.179 + } 1.180 + StringBuffer s = new StringBuffer(); 1.181 + for (Type t : cl.getTypeArguments()) { 1.182 + s.append(s.length() == 0 ? "<" : ", "); 1.183 + s.append(getTypeString(env, t, full)); 1.184 + } 1.185 + s.append(">"); 1.186 + return s.toString(); 1.187 + } 1.188 + 1.189 + 1.190 + private static class ArrayTypeImpl implements com.sun.javadoc.Type { 1.191 + 1.192 + Type arrayType; 1.193 + 1.194 + DocEnv env; 1.195 + 1.196 + ArrayTypeImpl(DocEnv env, Type arrayType) { 1.197 + this.env = env; 1.198 + this.arrayType = arrayType; 1.199 + } 1.200 + 1.201 + private com.sun.javadoc.Type skipArraysCache = null; 1.202 + 1.203 + private com.sun.javadoc.Type skipArrays() { 1.204 + if (skipArraysCache == null) { 1.205 + Type t; 1.206 + for (t = arrayType; t.tag == ARRAY; t = env.types.elemtype(t)) { } 1.207 + skipArraysCache = TypeMaker.getType(env, t); 1.208 + } 1.209 + return skipArraysCache; 1.210 + } 1.211 + 1.212 + /** 1.213 + * Return the type's dimension information, as a string. 1.214 + * <p> 1.215 + * For example, a two dimensional array of String returns '[][]'. 1.216 + */ 1.217 + public String dimension() { 1.218 + StringBuffer dimension = new StringBuffer(); 1.219 + for (Type t = arrayType; t.tag == ARRAY; t = env.types.elemtype(t)) { 1.220 + dimension = dimension.append("[]"); 1.221 + } 1.222 + return dimension.toString(); 1.223 + } 1.224 + 1.225 + /** 1.226 + * Return unqualified name of type excluding any dimension information. 1.227 + * <p> 1.228 + * For example, a two dimensional array of String returns 'String'. 1.229 + */ 1.230 + public String typeName() { 1.231 + return skipArrays().typeName(); 1.232 + } 1.233 + 1.234 + /** 1.235 + * Return qualified name of type excluding any dimension information. 1.236 + *<p> 1.237 + * For example, a two dimensional array of String 1.238 + * returns 'java.lang.String'. 1.239 + */ 1.240 + public String qualifiedTypeName() { 1.241 + return skipArrays().qualifiedTypeName(); 1.242 + } 1.243 + 1.244 + /** 1.245 + * Return the simple name of this type excluding any dimension information. 1.246 + */ 1.247 + public String simpleTypeName() { 1.248 + return skipArrays().simpleTypeName(); 1.249 + } 1.250 + 1.251 + /** 1.252 + * Return this type as a class. Array dimensions are ignored. 1.253 + * 1.254 + * @return a ClassDocImpl if the type is a Class. 1.255 + * Return null if it is a primitive type.. 1.256 + */ 1.257 + public ClassDoc asClassDoc() { 1.258 + return skipArrays().asClassDoc(); 1.259 + } 1.260 + 1.261 + /** 1.262 + * Return this type as a <code>ParameterizedType</code> if it 1.263 + * represents a parameterized type. Array dimensions are ignored. 1.264 + */ 1.265 + public ParameterizedType asParameterizedType() { 1.266 + return skipArrays().asParameterizedType(); 1.267 + } 1.268 + 1.269 + /** 1.270 + * Return this type as a <code>TypeVariable</code> if it represents 1.271 + * a type variable. Array dimensions are ignored. 1.272 + */ 1.273 + public TypeVariable asTypeVariable() { 1.274 + return skipArrays().asTypeVariable(); 1.275 + } 1.276 + 1.277 + /** 1.278 + * Return null, as there are no arrays of wildcard types. 1.279 + */ 1.280 + public WildcardType asWildcardType() { 1.281 + return null; 1.282 + } 1.283 + 1.284 + /** 1.285 + * Return this type as an <code>AnnotationTypeDoc</code> if it 1.286 + * represents an annotation type. Array dimensions are ignored. 1.287 + */ 1.288 + public AnnotationTypeDoc asAnnotationTypeDoc() { 1.289 + return skipArrays().asAnnotationTypeDoc(); 1.290 + } 1.291 + 1.292 + /** 1.293 + * Return true if this is an array of a primitive type. 1.294 + */ 1.295 + public boolean isPrimitive() { 1.296 + return skipArrays().isPrimitive(); 1.297 + } 1.298 + 1.299 + /** 1.300 + * Return a string representation of the type. 1.301 + * 1.302 + * Return name of type including any dimension information. 1.303 + * <p> 1.304 + * For example, a two dimensional array of String returns 1.305 + * <code>String[][]</code>. 1.306 + * 1.307 + * @return name of type including any dimension information. 1.308 + */ 1.309 + public String toString() { 1.310 + return qualifiedTypeName() + dimension(); 1.311 + } 1.312 + } 1.313 +}