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

changeset 0
959103a6100f
child 2525
2eb010b6cb22
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/share/classes/com/sun/tools/javadoc/TypeMaker.java	Wed Apr 27 01:34:52 2016 +0800
     1.3 @@ -0,0 +1,339 @@
     1.4 +/*
     1.5 + * Copyright (c) 1997, 2013, Oracle and/or its affiliates. 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.  Oracle designates this
    1.11 + * particular file as subject to the "Classpath" exception as provided
    1.12 + * by Oracle 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.25 + * or visit www.oracle.com if you need additional information or have any
    1.26 + * questions.
    1.27 + */
    1.28 +
    1.29 +package com.sun.tools.javadoc;
    1.30 +
    1.31 +import com.sun.javadoc.*;
    1.32 +import com.sun.tools.javac.code.Symbol;
    1.33 +import com.sun.tools.javac.code.Symbol.ClassSymbol;
    1.34 +import com.sun.tools.javac.code.Type;
    1.35 +import com.sun.tools.javac.code.Type.ArrayType;
    1.36 +import com.sun.tools.javac.code.Type.ClassType;
    1.37 +import com.sun.tools.javac.code.Type.TypeVar;
    1.38 +import com.sun.tools.javac.util.List;
    1.39 +import static com.sun.tools.javac.code.TypeTag.ARRAY;
    1.40 +
    1.41 +/**
    1.42 + *  <p><b>This is NOT part of any supported API.
    1.43 + *  If you write code that depends on this, you do so at your own risk.
    1.44 + *  This code and its internal interfaces are subject to change or
    1.45 + *  deletion without notice.</b>
    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 errorToClassDoc) {
    1.59 +        return getType(env, t, errorToClassDoc, true);
    1.60 +    }
    1.61 +
    1.62 +    @SuppressWarnings("fallthrough")
    1.63 +    public static com.sun.javadoc.Type getType(DocEnv env, Type t,
    1.64 +            boolean errToClassDoc, boolean considerAnnotations) {
    1.65 +        if (env.legacyDoclet) {
    1.66 +            t = env.types.erasure(t);
    1.67 +        }
    1.68 +
    1.69 +        if (considerAnnotations && t.isAnnotated()) {
    1.70 +            return new AnnotatedTypeImpl(env, t);
    1.71 +        }
    1.72 +
    1.73 +        switch (t.getTag()) {
    1.74 +        case CLASS:
    1.75 +            if (ClassDocImpl.isGeneric((ClassSymbol)t.tsym)) {
    1.76 +                return env.getParameterizedType((ClassType)t);
    1.77 +            } else {
    1.78 +                return env.getClassDoc((ClassSymbol)t.tsym);
    1.79 +            }
    1.80 +        case WILDCARD:
    1.81 +            Type.WildcardType a = (Type.WildcardType)t;
    1.82 +            return new WildcardTypeImpl(env, a);
    1.83 +        case TYPEVAR: return new TypeVariableImpl(env, (TypeVar)t);
    1.84 +        case ARRAY: return new ArrayTypeImpl(env, t);
    1.85 +        case BYTE: return PrimitiveType.byteType;
    1.86 +        case CHAR: return PrimitiveType.charType;
    1.87 +        case SHORT: return PrimitiveType.shortType;
    1.88 +        case INT: return PrimitiveType.intType;
    1.89 +        case LONG: return PrimitiveType.longType;
    1.90 +        case FLOAT: return PrimitiveType.floatType;
    1.91 +        case DOUBLE: return PrimitiveType.doubleType;
    1.92 +        case BOOLEAN: return PrimitiveType.booleanType;
    1.93 +        case VOID: return PrimitiveType.voidType;
    1.94 +        case ERROR:
    1.95 +            if (errToClassDoc)
    1.96 +                return env.getClassDoc((ClassSymbol)t.tsym);
    1.97 +            // FALLTHRU
    1.98 +        default:
    1.99 +            return new PrimitiveType(t.tsym.getQualifiedName().toString());
   1.100 +        }
   1.101 +    }
   1.102 +
   1.103 +    /**
   1.104 +     * Convert a list of javac types into an array of javadoc types.
   1.105 +     */
   1.106 +    public static com.sun.javadoc.Type[] getTypes(DocEnv env, List<Type> ts) {
   1.107 +        return getTypes(env, ts, new com.sun.javadoc.Type[ts.length()]);
   1.108 +    }
   1.109 +
   1.110 +    /**
   1.111 +     * Like the above version, but use and return the array given.
   1.112 +     */
   1.113 +    public static com.sun.javadoc.Type[] getTypes(DocEnv env, List<Type> ts,
   1.114 +                                                  com.sun.javadoc.Type res[]) {
   1.115 +        int i = 0;
   1.116 +        for (Type t : ts) {
   1.117 +            res[i++] = getType(env, t);
   1.118 +        }
   1.119 +        return res;
   1.120 +    }
   1.121 +
   1.122 +    public static String getTypeName(Type t, boolean full) {
   1.123 +        switch (t.getTag()) {
   1.124 +        case ARRAY:
   1.125 +            StringBuilder s = new StringBuilder();
   1.126 +            while (t.hasTag(ARRAY)) {
   1.127 +                s.append("[]");
   1.128 +                t = ((ArrayType)t).elemtype;
   1.129 +            }
   1.130 +            s.insert(0, getTypeName(t, full));
   1.131 +            return s.toString();
   1.132 +        case CLASS:
   1.133 +            return ClassDocImpl.getClassName((ClassSymbol)t.tsym, full);
   1.134 +        default:
   1.135 +            return t.tsym.getQualifiedName().toString();
   1.136 +        }
   1.137 +    }
   1.138 +
   1.139 +    /**
   1.140 +     * Return the string representation of a type use.  Bounds of type
   1.141 +     * variables are not included; bounds of wildcard types are.
   1.142 +     * Class names are qualified if "full" is true.
   1.143 +     */
   1.144 +    static String getTypeString(DocEnv env, Type t, boolean full) {
   1.145 +        // TODO: should annotations be included here?
   1.146 +        if (t.isAnnotated()) {
   1.147 +            t = t.unannotatedType();
   1.148 +        }
   1.149 +        switch (t.getTag()) {
   1.150 +        case ARRAY:
   1.151 +            StringBuilder s = new StringBuilder();
   1.152 +            while (t.hasTag(ARRAY)) {
   1.153 +                s.append("[]");
   1.154 +                t = env.types.elemtype(t);
   1.155 +            }
   1.156 +            s.insert(0, getTypeString(env, t, full));
   1.157 +            return s.toString();
   1.158 +        case CLASS:
   1.159 +            return ParameterizedTypeImpl.
   1.160 +                        parameterizedTypeToString(env, (ClassType)t, full);
   1.161 +        case WILDCARD:
   1.162 +            Type.WildcardType a = (Type.WildcardType)t;
   1.163 +            return WildcardTypeImpl.wildcardTypeToString(env, a, full);
   1.164 +        default:
   1.165 +            return t.tsym.getQualifiedName().toString();
   1.166 +        }
   1.167 +    }
   1.168 +
   1.169 +    /**
   1.170 +     * Return the formal type parameters of a class or method as an
   1.171 +     * angle-bracketed string.  Each parameter is a type variable with
   1.172 +     * optional bounds.  Class names are qualified if "full" is true.
   1.173 +     * Return "" if there are no type parameters or we're hiding generics.
   1.174 +     */
   1.175 +    static String typeParametersString(DocEnv env, Symbol sym, boolean full) {
   1.176 +        if (env.legacyDoclet || sym.type.getTypeArguments().isEmpty()) {
   1.177 +            return "";
   1.178 +        }
   1.179 +        StringBuilder s = new StringBuilder();
   1.180 +        for (Type t : sym.type.getTypeArguments()) {
   1.181 +            s.append(s.length() == 0 ? "<" : ", ");
   1.182 +            s.append(TypeVariableImpl.typeVarToString(env, (TypeVar)t, full));
   1.183 +        }
   1.184 +        s.append(">");
   1.185 +        return s.toString();
   1.186 +    }
   1.187 +
   1.188 +    /**
   1.189 +     * Return the actual type arguments of a parameterized type as an
   1.190 +     * angle-bracketed string.  Class name are qualified if "full" is true.
   1.191 +     * Return "" if there are no type arguments or we're hiding generics.
   1.192 +     */
   1.193 +    static String typeArgumentsString(DocEnv env, ClassType cl, boolean full) {
   1.194 +        if (env.legacyDoclet || cl.getTypeArguments().isEmpty()) {
   1.195 +            return "";
   1.196 +        }
   1.197 +        StringBuilder s = new StringBuilder();
   1.198 +        for (Type t : cl.getTypeArguments()) {
   1.199 +            s.append(s.length() == 0 ? "<" : ", ");
   1.200 +            s.append(getTypeString(env, t, full));
   1.201 +        }
   1.202 +        s.append(">");
   1.203 +        return s.toString();
   1.204 +    }
   1.205 +
   1.206 +
   1.207 +    private static class ArrayTypeImpl implements com.sun.javadoc.Type {
   1.208 +
   1.209 +        Type arrayType;
   1.210 +
   1.211 +        DocEnv env;
   1.212 +
   1.213 +        ArrayTypeImpl(DocEnv env, Type arrayType) {
   1.214 +            this.env = env;
   1.215 +            this.arrayType = arrayType;
   1.216 +        }
   1.217 +
   1.218 +        private com.sun.javadoc.Type skipArraysCache = null;
   1.219 +
   1.220 +        public com.sun.javadoc.Type getElementType() {
   1.221 +            return TypeMaker.getType(env, env.types.elemtype(arrayType));
   1.222 +        }
   1.223 +
   1.224 +        private com.sun.javadoc.Type skipArrays() {
   1.225 +            if (skipArraysCache == null) {
   1.226 +                Type t;
   1.227 +                for (t = arrayType; t.hasTag(ARRAY); t = env.types.elemtype(t)) { }
   1.228 +                skipArraysCache = TypeMaker.getType(env, t);
   1.229 +            }
   1.230 +            return skipArraysCache;
   1.231 +        }
   1.232 +
   1.233 +        /**
   1.234 +         * Return the type's dimension information, as a string.
   1.235 +         * <p>
   1.236 +         * For example, a two dimensional array of String returns '[][]'.
   1.237 +         */
   1.238 +        public String dimension() {
   1.239 +            StringBuilder dimension = new StringBuilder();
   1.240 +            for (Type t = arrayType; t.hasTag(ARRAY); t = env.types.elemtype(t)) {
   1.241 +                dimension.append("[]");
   1.242 +            }
   1.243 +            return dimension.toString();
   1.244 +        }
   1.245 +
   1.246 +        /**
   1.247 +         * Return unqualified name of type excluding any dimension information.
   1.248 +         * <p>
   1.249 +         * For example, a two dimensional array of String returns 'String'.
   1.250 +         */
   1.251 +        public String typeName() {
   1.252 +            return skipArrays().typeName();
   1.253 +        }
   1.254 +
   1.255 +        /**
   1.256 +         * Return qualified name of type excluding any dimension information.
   1.257 +         *<p>
   1.258 +         * For example, a two dimensional array of String
   1.259 +         * returns 'java.lang.String'.
   1.260 +         */
   1.261 +        public String qualifiedTypeName() {
   1.262 +            return skipArrays().qualifiedTypeName();
   1.263 +        }
   1.264 +
   1.265 +        /**
   1.266 +         * Return the simple name of this type excluding any dimension information.
   1.267 +         */
   1.268 +        public String simpleTypeName() {
   1.269 +            return skipArrays().simpleTypeName();
   1.270 +        }
   1.271 +
   1.272 +        /**
   1.273 +         * Return this type as a class.  Array dimensions are ignored.
   1.274 +         *
   1.275 +         * @return a ClassDocImpl if the type is a Class.
   1.276 +         * Return null if it is a primitive type..
   1.277 +         */
   1.278 +        public ClassDoc asClassDoc() {
   1.279 +            return skipArrays().asClassDoc();
   1.280 +        }
   1.281 +
   1.282 +        /**
   1.283 +         * Return this type as a <code>ParameterizedType</code> if it
   1.284 +         * represents a parameterized type.  Array dimensions are ignored.
   1.285 +         */
   1.286 +        public ParameterizedType asParameterizedType() {
   1.287 +            return skipArrays().asParameterizedType();
   1.288 +        }
   1.289 +
   1.290 +        /**
   1.291 +         * Return this type as a <code>TypeVariable</code> if it represents
   1.292 +         * a type variable.  Array dimensions are ignored.
   1.293 +         */
   1.294 +        public TypeVariable asTypeVariable() {
   1.295 +            return skipArrays().asTypeVariable();
   1.296 +        }
   1.297 +
   1.298 +        /**
   1.299 +         * Return null, as there are no arrays of wildcard types.
   1.300 +         */
   1.301 +        public WildcardType asWildcardType() {
   1.302 +            return null;
   1.303 +        }
   1.304 +
   1.305 +        /**
   1.306 +         * Return null, as there are no annotations of the type
   1.307 +         */
   1.308 +        public AnnotatedType asAnnotatedType() {
   1.309 +            return null;
   1.310 +        }
   1.311 +
   1.312 +        /**
   1.313 +         * Return this type as an <code>AnnotationTypeDoc</code> if it
   1.314 +         * represents an annotation type.  Array dimensions are ignored.
   1.315 +         */
   1.316 +        public AnnotationTypeDoc asAnnotationTypeDoc() {
   1.317 +            return skipArrays().asAnnotationTypeDoc();
   1.318 +        }
   1.319 +
   1.320 +        /**
   1.321 +         * Return true if this is an array of a primitive type.
   1.322 +         */
   1.323 +        public boolean isPrimitive() {
   1.324 +            return skipArrays().isPrimitive();
   1.325 +        }
   1.326 +
   1.327 +        /**
   1.328 +         * Return a string representation of the type.
   1.329 +         *
   1.330 +         * Return name of type including any dimension information.
   1.331 +         * <p>
   1.332 +         * For example, a two dimensional array of String returns
   1.333 +         * <code>String[][]</code>.
   1.334 +         *
   1.335 +         * @return name of type including any dimension information.
   1.336 +         */
   1.337 +        @Override
   1.338 +        public String toString() {
   1.339 +            return qualifiedTypeName() + dimension();
   1.340 +        }
   1.341 +    }
   1.342 +}

mercurial