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

Wed, 27 Apr 2016 01:34:52 +0800

author
aoqi
date
Wed, 27 Apr 2016 01:34:52 +0800
changeset 0
959103a6100f
child 2525
2eb010b6cb22
permissions
-rw-r--r--

Initial load
http://hg.openjdk.java.net/jdk8u/jdk8u/langtools/
changeset: 2573:53ca196be1ae
tag: jdk8u25-b17

     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 && t.isAnnotated()) {
    67             return new AnnotatedTypeImpl(env, t);
    68         }
    70         switch (t.getTag()) {
    71         case CLASS:
    72             if (ClassDocImpl.isGeneric((ClassSymbol)t.tsym)) {
    73                 return env.getParameterizedType((ClassType)t);
    74             } else {
    75                 return env.getClassDoc((ClassSymbol)t.tsym);
    76             }
    77         case WILDCARD:
    78             Type.WildcardType a = (Type.WildcardType)t;
    79             return new WildcardTypeImpl(env, a);
    80         case TYPEVAR: return new TypeVariableImpl(env, (TypeVar)t);
    81         case ARRAY: return new ArrayTypeImpl(env, t);
    82         case BYTE: return PrimitiveType.byteType;
    83         case CHAR: return PrimitiveType.charType;
    84         case SHORT: return PrimitiveType.shortType;
    85         case INT: return PrimitiveType.intType;
    86         case LONG: return PrimitiveType.longType;
    87         case FLOAT: return PrimitiveType.floatType;
    88         case DOUBLE: return PrimitiveType.doubleType;
    89         case BOOLEAN: return PrimitiveType.booleanType;
    90         case VOID: return PrimitiveType.voidType;
    91         case ERROR:
    92             if (errToClassDoc)
    93                 return env.getClassDoc((ClassSymbol)t.tsym);
    94             // FALLTHRU
    95         default:
    96             return new PrimitiveType(t.tsym.getQualifiedName().toString());
    97         }
    98     }
   100     /**
   101      * Convert a list of javac types into an array of javadoc types.
   102      */
   103     public static com.sun.javadoc.Type[] getTypes(DocEnv env, List<Type> ts) {
   104         return getTypes(env, ts, new com.sun.javadoc.Type[ts.length()]);
   105     }
   107     /**
   108      * Like the above version, but use and return the array given.
   109      */
   110     public static com.sun.javadoc.Type[] getTypes(DocEnv env, List<Type> ts,
   111                                                   com.sun.javadoc.Type res[]) {
   112         int i = 0;
   113         for (Type t : ts) {
   114             res[i++] = getType(env, t);
   115         }
   116         return res;
   117     }
   119     public static String getTypeName(Type t, boolean full) {
   120         switch (t.getTag()) {
   121         case ARRAY:
   122             StringBuilder s = new StringBuilder();
   123             while (t.hasTag(ARRAY)) {
   124                 s.append("[]");
   125                 t = ((ArrayType)t).elemtype;
   126             }
   127             s.insert(0, getTypeName(t, full));
   128             return s.toString();
   129         case CLASS:
   130             return ClassDocImpl.getClassName((ClassSymbol)t.tsym, full);
   131         default:
   132             return t.tsym.getQualifiedName().toString();
   133         }
   134     }
   136     /**
   137      * Return the string representation of a type use.  Bounds of type
   138      * variables are not included; bounds of wildcard types are.
   139      * Class names are qualified if "full" is true.
   140      */
   141     static String getTypeString(DocEnv env, Type t, boolean full) {
   142         // TODO: should annotations be included here?
   143         if (t.isAnnotated()) {
   144             t = t.unannotatedType();
   145         }
   146         switch (t.getTag()) {
   147         case ARRAY:
   148             StringBuilder s = new StringBuilder();
   149             while (t.hasTag(ARRAY)) {
   150                 s.append("[]");
   151                 t = env.types.elemtype(t);
   152             }
   153             s.insert(0, getTypeString(env, t, full));
   154             return s.toString();
   155         case CLASS:
   156             return ParameterizedTypeImpl.
   157                         parameterizedTypeToString(env, (ClassType)t, full);
   158         case WILDCARD:
   159             Type.WildcardType a = (Type.WildcardType)t;
   160             return WildcardTypeImpl.wildcardTypeToString(env, a, full);
   161         default:
   162             return t.tsym.getQualifiedName().toString();
   163         }
   164     }
   166     /**
   167      * Return the formal type parameters of a class or method as an
   168      * angle-bracketed string.  Each parameter is a type variable with
   169      * optional bounds.  Class names are qualified if "full" is true.
   170      * Return "" if there are no type parameters or we're hiding generics.
   171      */
   172     static String typeParametersString(DocEnv env, Symbol sym, boolean full) {
   173         if (env.legacyDoclet || sym.type.getTypeArguments().isEmpty()) {
   174             return "";
   175         }
   176         StringBuilder s = new StringBuilder();
   177         for (Type t : sym.type.getTypeArguments()) {
   178             s.append(s.length() == 0 ? "<" : ", ");
   179             s.append(TypeVariableImpl.typeVarToString(env, (TypeVar)t, full));
   180         }
   181         s.append(">");
   182         return s.toString();
   183     }
   185     /**
   186      * Return the actual type arguments of a parameterized type as an
   187      * angle-bracketed string.  Class name are qualified if "full" is true.
   188      * Return "" if there are no type arguments or we're hiding generics.
   189      */
   190     static String typeArgumentsString(DocEnv env, ClassType cl, boolean full) {
   191         if (env.legacyDoclet || cl.getTypeArguments().isEmpty()) {
   192             return "";
   193         }
   194         StringBuilder s = new StringBuilder();
   195         for (Type t : cl.getTypeArguments()) {
   196             s.append(s.length() == 0 ? "<" : ", ");
   197             s.append(getTypeString(env, t, full));
   198         }
   199         s.append(">");
   200         return s.toString();
   201     }
   204     private static class ArrayTypeImpl implements com.sun.javadoc.Type {
   206         Type arrayType;
   208         DocEnv env;
   210         ArrayTypeImpl(DocEnv env, Type arrayType) {
   211             this.env = env;
   212             this.arrayType = arrayType;
   213         }
   215         private com.sun.javadoc.Type skipArraysCache = null;
   217         public com.sun.javadoc.Type getElementType() {
   218             return TypeMaker.getType(env, env.types.elemtype(arrayType));
   219         }
   221         private com.sun.javadoc.Type skipArrays() {
   222             if (skipArraysCache == null) {
   223                 Type t;
   224                 for (t = arrayType; t.hasTag(ARRAY); t = env.types.elemtype(t)) { }
   225                 skipArraysCache = TypeMaker.getType(env, t);
   226             }
   227             return skipArraysCache;
   228         }
   230         /**
   231          * Return the type's dimension information, as a string.
   232          * <p>
   233          * For example, a two dimensional array of String returns '[][]'.
   234          */
   235         public String dimension() {
   236             StringBuilder dimension = new StringBuilder();
   237             for (Type t = arrayType; t.hasTag(ARRAY); t = env.types.elemtype(t)) {
   238                 dimension.append("[]");
   239             }
   240             return dimension.toString();
   241         }
   243         /**
   244          * Return unqualified name of type excluding any dimension information.
   245          * <p>
   246          * For example, a two dimensional array of String returns 'String'.
   247          */
   248         public String typeName() {
   249             return skipArrays().typeName();
   250         }
   252         /**
   253          * Return qualified name of type excluding any dimension information.
   254          *<p>
   255          * For example, a two dimensional array of String
   256          * returns 'java.lang.String'.
   257          */
   258         public String qualifiedTypeName() {
   259             return skipArrays().qualifiedTypeName();
   260         }
   262         /**
   263          * Return the simple name of this type excluding any dimension information.
   264          */
   265         public String simpleTypeName() {
   266             return skipArrays().simpleTypeName();
   267         }
   269         /**
   270          * Return this type as a class.  Array dimensions are ignored.
   271          *
   272          * @return a ClassDocImpl if the type is a Class.
   273          * Return null if it is a primitive type..
   274          */
   275         public ClassDoc asClassDoc() {
   276             return skipArrays().asClassDoc();
   277         }
   279         /**
   280          * Return this type as a <code>ParameterizedType</code> if it
   281          * represents a parameterized type.  Array dimensions are ignored.
   282          */
   283         public ParameterizedType asParameterizedType() {
   284             return skipArrays().asParameterizedType();
   285         }
   287         /**
   288          * Return this type as a <code>TypeVariable</code> if it represents
   289          * a type variable.  Array dimensions are ignored.
   290          */
   291         public TypeVariable asTypeVariable() {
   292             return skipArrays().asTypeVariable();
   293         }
   295         /**
   296          * Return null, as there are no arrays of wildcard types.
   297          */
   298         public WildcardType asWildcardType() {
   299             return null;
   300         }
   302         /**
   303          * Return null, as there are no annotations of the type
   304          */
   305         public AnnotatedType asAnnotatedType() {
   306             return null;
   307         }
   309         /**
   310          * Return this type as an <code>AnnotationTypeDoc</code> if it
   311          * represents an annotation type.  Array dimensions are ignored.
   312          */
   313         public AnnotationTypeDoc asAnnotationTypeDoc() {
   314             return skipArrays().asAnnotationTypeDoc();
   315         }
   317         /**
   318          * Return true if this is an array of a primitive type.
   319          */
   320         public boolean isPrimitive() {
   321             return skipArrays().isPrimitive();
   322         }
   324         /**
   325          * Return a string representation of the type.
   326          *
   327          * Return name of type including any dimension information.
   328          * <p>
   329          * For example, a two dimensional array of String returns
   330          * <code>String[][]</code>.
   331          *
   332          * @return name of type including any dimension information.
   333          */
   334         @Override
   335         public String toString() {
   336             return qualifiedTypeName() + dimension();
   337         }
   338     }
   339 }

mercurial