Tue, 24 Dec 2013 09:17:37 -0800
8029230: Update copyright year to match last edit in jdk8 langtools repository for 2013
Reviewed-by: ksrini
Contributed-by: steve.sides@oracle.com
duke@1 | 1 | /* |
jjg@1521 | 2 | * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved. |
duke@1 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
duke@1 | 4 | * |
duke@1 | 5 | * This code is free software; you can redistribute it and/or modify it |
duke@1 | 6 | * under the terms of the GNU General Public License version 2 only, as |
ohair@554 | 7 | * published by the Free Software Foundation. Oracle designates this |
duke@1 | 8 | * particular file as subject to the "Classpath" exception as provided |
ohair@554 | 9 | * by Oracle in the LICENSE file that accompanied this code. |
duke@1 | 10 | * |
duke@1 | 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
duke@1 | 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
duke@1 | 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
duke@1 | 14 | * version 2 for more details (a copy is included in the LICENSE file that |
duke@1 | 15 | * accompanied this code). |
duke@1 | 16 | * |
duke@1 | 17 | * You should have received a copy of the GNU General Public License version |
duke@1 | 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
duke@1 | 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
duke@1 | 20 | * |
ohair@554 | 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
ohair@554 | 22 | * or visit www.oracle.com if you need additional information or have any |
ohair@554 | 23 | * questions. |
duke@1 | 24 | */ |
duke@1 | 25 | |
duke@1 | 26 | package com.sun.tools.javadoc; |
duke@1 | 27 | |
duke@1 | 28 | import com.sun.javadoc.*; |
duke@1 | 29 | import com.sun.tools.javac.code.Symbol; |
duke@1 | 30 | import com.sun.tools.javac.code.Symbol.ClassSymbol; |
duke@1 | 31 | import com.sun.tools.javac.code.Type; |
jjg@1357 | 32 | import com.sun.tools.javac.code.Type.ArrayType; |
duke@1 | 33 | import com.sun.tools.javac.code.Type.ClassType; |
duke@1 | 34 | import com.sun.tools.javac.code.Type.TypeVar; |
duke@1 | 35 | import com.sun.tools.javac.util.List; |
jjg@1374 | 36 | import static com.sun.tools.javac.code.TypeTag.ARRAY; |
duke@1 | 37 | |
jjg@1359 | 38 | /** |
jjg@1359 | 39 | * <p><b>This is NOT part of any supported API. |
jjg@1359 | 40 | * If you write code that depends on this, you do so at your own risk. |
jjg@1359 | 41 | * This code and its internal interfaces are subject to change or |
jjg@1359 | 42 | * deletion without notice.</b> |
jjg@1359 | 43 | */ |
duke@1 | 44 | public class TypeMaker { |
duke@1 | 45 | |
duke@1 | 46 | public static com.sun.javadoc.Type getType(DocEnv env, Type t) { |
duke@1 | 47 | return getType(env, t, true); |
duke@1 | 48 | } |
duke@1 | 49 | |
duke@1 | 50 | /** |
duke@1 | 51 | * @param errToClassDoc if true, ERROR type results in a ClassDoc; |
duke@1 | 52 | * false preserves legacy behavior |
duke@1 | 53 | */ |
jjg@1521 | 54 | public static com.sun.javadoc.Type getType(DocEnv env, Type t, |
jjg@1521 | 55 | boolean errorToClassDoc) { |
jjg@1521 | 56 | return getType(env, t, errorToClassDoc, true); |
jjg@1521 | 57 | } |
jjg@1521 | 58 | |
jjg@198 | 59 | @SuppressWarnings("fallthrough") |
duke@1 | 60 | public static com.sun.javadoc.Type getType(DocEnv env, Type t, |
jjg@1521 | 61 | boolean errToClassDoc, boolean considerAnnotations) { |
duke@1 | 62 | if (env.legacyDoclet) { |
duke@1 | 63 | t = env.types.erasure(t); |
duke@1 | 64 | } |
jjg@1521 | 65 | |
jjg@1992 | 66 | if (considerAnnotations && t.isAnnotated()) { |
jjg@1992 | 67 | return new AnnotatedTypeImpl(env, t); |
jjg@1521 | 68 | } |
jjg@1521 | 69 | |
jjg@1374 | 70 | switch (t.getTag()) { |
duke@1 | 71 | case CLASS: |
duke@1 | 72 | if (ClassDocImpl.isGeneric((ClassSymbol)t.tsym)) { |
duke@1 | 73 | return env.getParameterizedType((ClassType)t); |
duke@1 | 74 | } else { |
duke@1 | 75 | return env.getClassDoc((ClassSymbol)t.tsym); |
duke@1 | 76 | } |
duke@1 | 77 | case WILDCARD: |
duke@1 | 78 | Type.WildcardType a = (Type.WildcardType)t; |
duke@1 | 79 | return new WildcardTypeImpl(env, a); |
duke@1 | 80 | case TYPEVAR: return new TypeVariableImpl(env, (TypeVar)t); |
duke@1 | 81 | case ARRAY: return new ArrayTypeImpl(env, t); |
duke@1 | 82 | case BYTE: return PrimitiveType.byteType; |
duke@1 | 83 | case CHAR: return PrimitiveType.charType; |
duke@1 | 84 | case SHORT: return PrimitiveType.shortType; |
duke@1 | 85 | case INT: return PrimitiveType.intType; |
duke@1 | 86 | case LONG: return PrimitiveType.longType; |
duke@1 | 87 | case FLOAT: return PrimitiveType.floatType; |
duke@1 | 88 | case DOUBLE: return PrimitiveType.doubleType; |
duke@1 | 89 | case BOOLEAN: return PrimitiveType.booleanType; |
duke@1 | 90 | case VOID: return PrimitiveType.voidType; |
duke@1 | 91 | case ERROR: |
duke@1 | 92 | if (errToClassDoc) |
duke@1 | 93 | return env.getClassDoc((ClassSymbol)t.tsym); |
duke@1 | 94 | // FALLTHRU |
duke@1 | 95 | default: |
duke@1 | 96 | return new PrimitiveType(t.tsym.getQualifiedName().toString()); |
duke@1 | 97 | } |
duke@1 | 98 | } |
duke@1 | 99 | |
duke@1 | 100 | /** |
duke@1 | 101 | * Convert a list of javac types into an array of javadoc types. |
duke@1 | 102 | */ |
duke@1 | 103 | public static com.sun.javadoc.Type[] getTypes(DocEnv env, List<Type> ts) { |
duke@1 | 104 | return getTypes(env, ts, new com.sun.javadoc.Type[ts.length()]); |
duke@1 | 105 | } |
duke@1 | 106 | |
duke@1 | 107 | /** |
duke@1 | 108 | * Like the above version, but use and return the array given. |
duke@1 | 109 | */ |
duke@1 | 110 | public static com.sun.javadoc.Type[] getTypes(DocEnv env, List<Type> ts, |
duke@1 | 111 | com.sun.javadoc.Type res[]) { |
duke@1 | 112 | int i = 0; |
duke@1 | 113 | for (Type t : ts) { |
duke@1 | 114 | res[i++] = getType(env, t); |
duke@1 | 115 | } |
duke@1 | 116 | return res; |
duke@1 | 117 | } |
duke@1 | 118 | |
duke@1 | 119 | public static String getTypeName(Type t, boolean full) { |
jjg@1374 | 120 | switch (t.getTag()) { |
duke@1 | 121 | case ARRAY: |
jjg@910 | 122 | StringBuilder s = new StringBuilder(); |
jjg@1374 | 123 | while (t.hasTag(ARRAY)) { |
jjg@910 | 124 | s.append("[]"); |
duke@1 | 125 | t = ((ArrayType)t).elemtype; |
duke@1 | 126 | } |
jjg@910 | 127 | s.insert(0, getTypeName(t, full)); |
jjg@910 | 128 | return s.toString(); |
duke@1 | 129 | case CLASS: |
duke@1 | 130 | return ClassDocImpl.getClassName((ClassSymbol)t.tsym, full); |
duke@1 | 131 | default: |
duke@1 | 132 | return t.tsym.getQualifiedName().toString(); |
duke@1 | 133 | } |
duke@1 | 134 | } |
duke@1 | 135 | |
duke@1 | 136 | /** |
duke@1 | 137 | * Return the string representation of a type use. Bounds of type |
duke@1 | 138 | * variables are not included; bounds of wildcard types are. |
duke@1 | 139 | * Class names are qualified if "full" is true. |
duke@1 | 140 | */ |
duke@1 | 141 | static String getTypeString(DocEnv env, Type t, boolean full) { |
jjg@1521 | 142 | // TODO: should annotations be included here? |
jjg@1644 | 143 | if (t.isAnnotated()) { |
jjg@1992 | 144 | t = t.unannotatedType(); |
jjg@1521 | 145 | } |
jjg@1374 | 146 | switch (t.getTag()) { |
duke@1 | 147 | case ARRAY: |
jjg@910 | 148 | StringBuilder s = new StringBuilder(); |
jjg@1374 | 149 | while (t.hasTag(ARRAY)) { |
jjg@910 | 150 | s.append("[]"); |
duke@1 | 151 | t = env.types.elemtype(t); |
duke@1 | 152 | } |
jjg@910 | 153 | s.insert(0, getTypeString(env, t, full)); |
jjg@910 | 154 | return s.toString(); |
duke@1 | 155 | case CLASS: |
duke@1 | 156 | return ParameterizedTypeImpl. |
duke@1 | 157 | parameterizedTypeToString(env, (ClassType)t, full); |
duke@1 | 158 | case WILDCARD: |
duke@1 | 159 | Type.WildcardType a = (Type.WildcardType)t; |
duke@1 | 160 | return WildcardTypeImpl.wildcardTypeToString(env, a, full); |
duke@1 | 161 | default: |
duke@1 | 162 | return t.tsym.getQualifiedName().toString(); |
duke@1 | 163 | } |
duke@1 | 164 | } |
duke@1 | 165 | |
duke@1 | 166 | /** |
duke@1 | 167 | * Return the formal type parameters of a class or method as an |
duke@1 | 168 | * angle-bracketed string. Each parameter is a type variable with |
duke@1 | 169 | * optional bounds. Class names are qualified if "full" is true. |
duke@1 | 170 | * Return "" if there are no type parameters or we're hiding generics. |
duke@1 | 171 | */ |
duke@1 | 172 | static String typeParametersString(DocEnv env, Symbol sym, boolean full) { |
duke@1 | 173 | if (env.legacyDoclet || sym.type.getTypeArguments().isEmpty()) { |
duke@1 | 174 | return ""; |
duke@1 | 175 | } |
jjg@910 | 176 | StringBuilder s = new StringBuilder(); |
duke@1 | 177 | for (Type t : sym.type.getTypeArguments()) { |
duke@1 | 178 | s.append(s.length() == 0 ? "<" : ", "); |
duke@1 | 179 | s.append(TypeVariableImpl.typeVarToString(env, (TypeVar)t, full)); |
duke@1 | 180 | } |
duke@1 | 181 | s.append(">"); |
duke@1 | 182 | return s.toString(); |
duke@1 | 183 | } |
duke@1 | 184 | |
duke@1 | 185 | /** |
duke@1 | 186 | * Return the actual type arguments of a parameterized type as an |
duke@1 | 187 | * angle-bracketed string. Class name are qualified if "full" is true. |
duke@1 | 188 | * Return "" if there are no type arguments or we're hiding generics. |
duke@1 | 189 | */ |
duke@1 | 190 | static String typeArgumentsString(DocEnv env, ClassType cl, boolean full) { |
duke@1 | 191 | if (env.legacyDoclet || cl.getTypeArguments().isEmpty()) { |
duke@1 | 192 | return ""; |
duke@1 | 193 | } |
jjg@910 | 194 | StringBuilder s = new StringBuilder(); |
duke@1 | 195 | for (Type t : cl.getTypeArguments()) { |
duke@1 | 196 | s.append(s.length() == 0 ? "<" : ", "); |
duke@1 | 197 | s.append(getTypeString(env, t, full)); |
duke@1 | 198 | } |
duke@1 | 199 | s.append(">"); |
duke@1 | 200 | return s.toString(); |
duke@1 | 201 | } |
duke@1 | 202 | |
duke@1 | 203 | |
duke@1 | 204 | private static class ArrayTypeImpl implements com.sun.javadoc.Type { |
duke@1 | 205 | |
duke@1 | 206 | Type arrayType; |
duke@1 | 207 | |
duke@1 | 208 | DocEnv env; |
duke@1 | 209 | |
duke@1 | 210 | ArrayTypeImpl(DocEnv env, Type arrayType) { |
duke@1 | 211 | this.env = env; |
duke@1 | 212 | this.arrayType = arrayType; |
duke@1 | 213 | } |
duke@1 | 214 | |
duke@1 | 215 | private com.sun.javadoc.Type skipArraysCache = null; |
duke@1 | 216 | |
bpatel@1691 | 217 | public com.sun.javadoc.Type getElementType() { |
bpatel@1691 | 218 | return TypeMaker.getType(env, env.types.elemtype(arrayType)); |
bpatel@1691 | 219 | } |
bpatel@1691 | 220 | |
duke@1 | 221 | private com.sun.javadoc.Type skipArrays() { |
duke@1 | 222 | if (skipArraysCache == null) { |
duke@1 | 223 | Type t; |
jjg@1374 | 224 | for (t = arrayType; t.hasTag(ARRAY); t = env.types.elemtype(t)) { } |
duke@1 | 225 | skipArraysCache = TypeMaker.getType(env, t); |
duke@1 | 226 | } |
duke@1 | 227 | return skipArraysCache; |
duke@1 | 228 | } |
duke@1 | 229 | |
duke@1 | 230 | /** |
duke@1 | 231 | * Return the type's dimension information, as a string. |
duke@1 | 232 | * <p> |
duke@1 | 233 | * For example, a two dimensional array of String returns '[][]'. |
duke@1 | 234 | */ |
duke@1 | 235 | public String dimension() { |
jjg@910 | 236 | StringBuilder dimension = new StringBuilder(); |
jjg@1374 | 237 | for (Type t = arrayType; t.hasTag(ARRAY); t = env.types.elemtype(t)) { |
jjg@910 | 238 | dimension.append("[]"); |
duke@1 | 239 | } |
duke@1 | 240 | return dimension.toString(); |
duke@1 | 241 | } |
duke@1 | 242 | |
duke@1 | 243 | /** |
duke@1 | 244 | * Return unqualified name of type excluding any dimension information. |
duke@1 | 245 | * <p> |
duke@1 | 246 | * For example, a two dimensional array of String returns 'String'. |
duke@1 | 247 | */ |
duke@1 | 248 | public String typeName() { |
duke@1 | 249 | return skipArrays().typeName(); |
duke@1 | 250 | } |
duke@1 | 251 | |
duke@1 | 252 | /** |
duke@1 | 253 | * Return qualified name of type excluding any dimension information. |
duke@1 | 254 | *<p> |
duke@1 | 255 | * For example, a two dimensional array of String |
duke@1 | 256 | * returns 'java.lang.String'. |
duke@1 | 257 | */ |
duke@1 | 258 | public String qualifiedTypeName() { |
duke@1 | 259 | return skipArrays().qualifiedTypeName(); |
duke@1 | 260 | } |
duke@1 | 261 | |
duke@1 | 262 | /** |
duke@1 | 263 | * Return the simple name of this type excluding any dimension information. |
duke@1 | 264 | */ |
duke@1 | 265 | public String simpleTypeName() { |
duke@1 | 266 | return skipArrays().simpleTypeName(); |
duke@1 | 267 | } |
duke@1 | 268 | |
duke@1 | 269 | /** |
duke@1 | 270 | * Return this type as a class. Array dimensions are ignored. |
duke@1 | 271 | * |
duke@1 | 272 | * @return a ClassDocImpl if the type is a Class. |
duke@1 | 273 | * Return null if it is a primitive type.. |
duke@1 | 274 | */ |
duke@1 | 275 | public ClassDoc asClassDoc() { |
duke@1 | 276 | return skipArrays().asClassDoc(); |
duke@1 | 277 | } |
duke@1 | 278 | |
duke@1 | 279 | /** |
duke@1 | 280 | * Return this type as a <code>ParameterizedType</code> if it |
duke@1 | 281 | * represents a parameterized type. Array dimensions are ignored. |
duke@1 | 282 | */ |
duke@1 | 283 | public ParameterizedType asParameterizedType() { |
duke@1 | 284 | return skipArrays().asParameterizedType(); |
duke@1 | 285 | } |
duke@1 | 286 | |
duke@1 | 287 | /** |
duke@1 | 288 | * Return this type as a <code>TypeVariable</code> if it represents |
duke@1 | 289 | * a type variable. Array dimensions are ignored. |
duke@1 | 290 | */ |
duke@1 | 291 | public TypeVariable asTypeVariable() { |
duke@1 | 292 | return skipArrays().asTypeVariable(); |
duke@1 | 293 | } |
duke@1 | 294 | |
duke@1 | 295 | /** |
duke@1 | 296 | * Return null, as there are no arrays of wildcard types. |
duke@1 | 297 | */ |
duke@1 | 298 | public WildcardType asWildcardType() { |
duke@1 | 299 | return null; |
duke@1 | 300 | } |
duke@1 | 301 | |
duke@1 | 302 | /** |
jjg@1521 | 303 | * Return null, as there are no annotations of the type |
jjg@1521 | 304 | */ |
jjg@1521 | 305 | public AnnotatedType asAnnotatedType() { |
jjg@1521 | 306 | return null; |
jjg@1521 | 307 | } |
jjg@1521 | 308 | |
jjg@1521 | 309 | /** |
duke@1 | 310 | * Return this type as an <code>AnnotationTypeDoc</code> if it |
duke@1 | 311 | * represents an annotation type. Array dimensions are ignored. |
duke@1 | 312 | */ |
duke@1 | 313 | public AnnotationTypeDoc asAnnotationTypeDoc() { |
duke@1 | 314 | return skipArrays().asAnnotationTypeDoc(); |
duke@1 | 315 | } |
duke@1 | 316 | |
duke@1 | 317 | /** |
duke@1 | 318 | * Return true if this is an array of a primitive type. |
duke@1 | 319 | */ |
duke@1 | 320 | public boolean isPrimitive() { |
duke@1 | 321 | return skipArrays().isPrimitive(); |
duke@1 | 322 | } |
duke@1 | 323 | |
duke@1 | 324 | /** |
duke@1 | 325 | * Return a string representation of the type. |
duke@1 | 326 | * |
duke@1 | 327 | * Return name of type including any dimension information. |
duke@1 | 328 | * <p> |
duke@1 | 329 | * For example, a two dimensional array of String returns |
duke@1 | 330 | * <code>String[][]</code>. |
duke@1 | 331 | * |
duke@1 | 332 | * @return name of type including any dimension information. |
duke@1 | 333 | */ |
jjg@910 | 334 | @Override |
duke@1 | 335 | public String toString() { |
duke@1 | 336 | return qualifiedTypeName() + dimension(); |
duke@1 | 337 | } |
duke@1 | 338 | } |
duke@1 | 339 | } |