langtools/src/share/classes/com/sun/tools/javadoc/ParameterizedTypeImpl.java
author ohair
Tue, 25 May 2010 15:54:51 -0700
changeset 5520 86e4b9a9da40
parent 10 06bc494ca11e
child 8631 664f84942e74
permissions -rw-r--r--
6943119: Rebrand source copyright notices Reviewed-by: darcy
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     1
/*
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 10
diff changeset
     2
 * Copyright (c) 2003, 2005, Oracle and/or its affiliates. All rights reserved.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
06bc494ca11e Initial load
duke
parents:
diff changeset
     4
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
06bc494ca11e Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 10
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 10
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    10
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
06bc494ca11e Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
06bc494ca11e Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
06bc494ca11e Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
06bc494ca11e Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
06bc494ca11e Initial load
duke
parents:
diff changeset
    16
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
06bc494ca11e Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
06bc494ca11e Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
06bc494ca11e Initial load
duke
parents:
diff changeset
    20
 *
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 10
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 10
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 10
diff changeset
    23
 * questions.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    24
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    25
06bc494ca11e Initial load
duke
parents:
diff changeset
    26
package com.sun.tools.javadoc;
06bc494ca11e Initial load
duke
parents:
diff changeset
    27
06bc494ca11e Initial load
duke
parents:
diff changeset
    28
06bc494ca11e Initial load
duke
parents:
diff changeset
    29
import com.sun.javadoc.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    30
06bc494ca11e Initial load
duke
parents:
diff changeset
    31
import static com.sun.javadoc.LanguageVersion.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    32
06bc494ca11e Initial load
duke
parents:
diff changeset
    33
import com.sun.tools.javac.code.Symbol.ClassSymbol;
06bc494ca11e Initial load
duke
parents:
diff changeset
    34
import com.sun.tools.javac.code.Type;
06bc494ca11e Initial load
duke
parents:
diff changeset
    35
import com.sun.tools.javac.code.Type.ClassType;
06bc494ca11e Initial load
duke
parents:
diff changeset
    36
import com.sun.tools.javac.util.List;
06bc494ca11e Initial load
duke
parents:
diff changeset
    37
06bc494ca11e Initial load
duke
parents:
diff changeset
    38
import static com.sun.tools.javac.code.TypeTags.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    39
06bc494ca11e Initial load
duke
parents:
diff changeset
    40
06bc494ca11e Initial load
duke
parents:
diff changeset
    41
/**
06bc494ca11e Initial load
duke
parents:
diff changeset
    42
 * Implementation of <code>ParameterizedType</code>, which
06bc494ca11e Initial load
duke
parents:
diff changeset
    43
 * represents an invocation of a generic class or interface.
06bc494ca11e Initial load
duke
parents:
diff changeset
    44
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    45
 * @author Scott Seligman
06bc494ca11e Initial load
duke
parents:
diff changeset
    46
 * @since 1.5
06bc494ca11e Initial load
duke
parents:
diff changeset
    47
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    48
public class ParameterizedTypeImpl
06bc494ca11e Initial load
duke
parents:
diff changeset
    49
        extends AbstractTypeImpl implements ParameterizedType {
06bc494ca11e Initial load
duke
parents:
diff changeset
    50
06bc494ca11e Initial load
duke
parents:
diff changeset
    51
    ParameterizedTypeImpl(DocEnv env, Type type) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    52
        super(env, type);
06bc494ca11e Initial load
duke
parents:
diff changeset
    53
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    54
06bc494ca11e Initial load
duke
parents:
diff changeset
    55
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
    56
     * Return the generic class or interface that declared this type.
06bc494ca11e Initial load
duke
parents:
diff changeset
    57
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    58
    public ClassDoc asClassDoc() {
06bc494ca11e Initial load
duke
parents:
diff changeset
    59
        return env.getClassDoc((ClassSymbol)type.tsym);
06bc494ca11e Initial load
duke
parents:
diff changeset
    60
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    61
06bc494ca11e Initial load
duke
parents:
diff changeset
    62
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
    63
     * Return the actual type arguments of this type.
06bc494ca11e Initial load
duke
parents:
diff changeset
    64
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    65
    public com.sun.javadoc.Type[] typeArguments() {
06bc494ca11e Initial load
duke
parents:
diff changeset
    66
        return TypeMaker.getTypes(env, type.getTypeArguments());
06bc494ca11e Initial load
duke
parents:
diff changeset
    67
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    68
06bc494ca11e Initial load
duke
parents:
diff changeset
    69
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
    70
     * Return the class type that is a direct supertype of this one.
06bc494ca11e Initial load
duke
parents:
diff changeset
    71
     * Return null if this is an interface type.
06bc494ca11e Initial load
duke
parents:
diff changeset
    72
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    73
    public com.sun.javadoc.Type superclassType() {
06bc494ca11e Initial load
duke
parents:
diff changeset
    74
        if (asClassDoc().isInterface()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    75
            return null;
06bc494ca11e Initial load
duke
parents:
diff changeset
    76
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
    77
        Type sup = env.types.supertype(type);
06bc494ca11e Initial load
duke
parents:
diff changeset
    78
        return TypeMaker.getType(env,
06bc494ca11e Initial load
duke
parents:
diff changeset
    79
                                 (sup != type) ? sup : env.syms.objectType);
06bc494ca11e Initial load
duke
parents:
diff changeset
    80
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    81
06bc494ca11e Initial load
duke
parents:
diff changeset
    82
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
    83
     * Return the interface types directly implemented by or extended by this
06bc494ca11e Initial load
duke
parents:
diff changeset
    84
     * parameterized type.
06bc494ca11e Initial load
duke
parents:
diff changeset
    85
     * Return an empty array if there are no interfaces.
06bc494ca11e Initial load
duke
parents:
diff changeset
    86
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    87
    public com.sun.javadoc.Type[] interfaceTypes() {
06bc494ca11e Initial load
duke
parents:
diff changeset
    88
        return TypeMaker.getTypes(env, env.types.interfaces(type));
06bc494ca11e Initial load
duke
parents:
diff changeset
    89
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    90
06bc494ca11e Initial load
duke
parents:
diff changeset
    91
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
    92
     * Return the type that contains this type as a member.
06bc494ca11e Initial load
duke
parents:
diff changeset
    93
     * Return null is this is a top-level type.
06bc494ca11e Initial load
duke
parents:
diff changeset
    94
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    95
    public com.sun.javadoc.Type containingType() {
06bc494ca11e Initial load
duke
parents:
diff changeset
    96
        if (type.getEnclosingType().tag == CLASS) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    97
            // This is the type of an inner class.
06bc494ca11e Initial load
duke
parents:
diff changeset
    98
            return TypeMaker.getType(env, type.getEnclosingType());
06bc494ca11e Initial load
duke
parents:
diff changeset
    99
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   100
        ClassSymbol enclosing = type.tsym.owner.enclClass();
06bc494ca11e Initial load
duke
parents:
diff changeset
   101
        if (enclosing != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   102
            // Nested but not inner.  Return the ClassDoc of the enclosing
06bc494ca11e Initial load
duke
parents:
diff changeset
   103
            // class or interface.
06bc494ca11e Initial load
duke
parents:
diff changeset
   104
            // See java.lang.reflect.ParameterizedType.getOwnerType().
06bc494ca11e Initial load
duke
parents:
diff changeset
   105
            return env.getClassDoc(enclosing);
06bc494ca11e Initial load
duke
parents:
diff changeset
   106
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   107
        return null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   108
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   109
06bc494ca11e Initial load
duke
parents:
diff changeset
   110
06bc494ca11e Initial load
duke
parents:
diff changeset
   111
    // Asking for the "name" of a parameterized type doesn't exactly make
06bc494ca11e Initial load
duke
parents:
diff changeset
   112
    // sense.  It's a type expression.  Return the name of its generic
06bc494ca11e Initial load
duke
parents:
diff changeset
   113
    // type.
06bc494ca11e Initial load
duke
parents:
diff changeset
   114
    public String typeName() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   115
        return TypeMaker.getTypeName(type, false);
06bc494ca11e Initial load
duke
parents:
diff changeset
   116
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   117
06bc494ca11e Initial load
duke
parents:
diff changeset
   118
    public ParameterizedType asParameterizedType() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   119
        return this;
06bc494ca11e Initial load
duke
parents:
diff changeset
   120
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   121
06bc494ca11e Initial load
duke
parents:
diff changeset
   122
    public String toString() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   123
        return parameterizedTypeToString(env, (ClassType)type, true);
06bc494ca11e Initial load
duke
parents:
diff changeset
   124
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   125
06bc494ca11e Initial load
duke
parents:
diff changeset
   126
    static String parameterizedTypeToString(DocEnv env, ClassType cl,
06bc494ca11e Initial load
duke
parents:
diff changeset
   127
                                            boolean full) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   128
        if (env.legacyDoclet) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   129
            return TypeMaker.getTypeName(cl, full);
06bc494ca11e Initial load
duke
parents:
diff changeset
   130
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   131
        StringBuffer s = new StringBuffer();
06bc494ca11e Initial load
duke
parents:
diff changeset
   132
        if (cl.getEnclosingType().tag != CLASS) {               // if not an inner class...
06bc494ca11e Initial load
duke
parents:
diff changeset
   133
            s.append(TypeMaker.getTypeName(cl, full));
06bc494ca11e Initial load
duke
parents:
diff changeset
   134
        } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   135
            ClassType encl = (ClassType)cl.getEnclosingType();
06bc494ca11e Initial load
duke
parents:
diff changeset
   136
            s.append(parameterizedTypeToString(env, encl, full))
06bc494ca11e Initial load
duke
parents:
diff changeset
   137
             .append('.')
06bc494ca11e Initial load
duke
parents:
diff changeset
   138
             .append(cl.tsym.name.toString());
06bc494ca11e Initial load
duke
parents:
diff changeset
   139
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   140
        s.append(TypeMaker.typeArgumentsString(env, cl, full));
06bc494ca11e Initial load
duke
parents:
diff changeset
   141
        return s.toString();
06bc494ca11e Initial load
duke
parents:
diff changeset
   142
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   143
}