src/share/classes/com/sun/tools/javadoc/TypeMaker.java

Thu, 04 Aug 2016 23:36:47 -0700

author
asaha
date
Thu, 04 Aug 2016 23:36:47 -0700
changeset 3270
8a30511b2ea4
parent 2906
d3a51adc115f
child 3295
859dc787b52b
permissions
-rw-r--r--

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

mercurial