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