1/*
2 * Copyright (c) 2003, 2012, 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 */
25
26package com.sun.tools.javadoc.main;
27
28import com.sun.javadoc.*;
29
30import com.sun.tools.javac.code.Symbol.ClassSymbol;
31import com.sun.tools.javac.code.Type;
32import com.sun.tools.javac.code.Type.ClassType;
33
34import static com.sun.tools.javac.code.TypeTag.CLASS;
35
36
37/**
38 * Implementation of <code>ParameterizedType</code>, which
39 * represents an invocation of a generic class or interface.
40 *
41 *  <p><b>This is NOT part of any supported API.
42 *  If you write code that depends on this, you do so at your own risk.
43 *  This code and its internal interfaces are subject to change or
44 *  deletion without notice.</b>
45 *
46 * @author Scott Seligman
47 * @since 1.5
48 */
49@Deprecated
50public class ParameterizedTypeImpl
51        extends AbstractTypeImpl implements ParameterizedType {
52
53    ParameterizedTypeImpl(DocEnv env, Type type) {
54        super(env, type);
55    }
56
57    /**
58     * Return the generic class or interface that declared this type.
59     */
60    @Override
61    public ClassDoc asClassDoc() {
62        return env.getClassDoc((ClassSymbol)type.tsym);
63    }
64
65    /**
66     * Return the actual type arguments of this type.
67     */
68    public com.sun.javadoc.Type[] typeArguments() {
69        return TypeMaker.getTypes(env, type.getTypeArguments());
70    }
71
72    /**
73     * Return the class type that is a direct supertype of this one.
74     * Return null if this is an interface type.
75     */
76    public com.sun.javadoc.Type superclassType() {
77        if (asClassDoc().isInterface()) {
78            return null;
79        }
80        Type sup = env.types.supertype(type);
81        return TypeMaker.getType(env,
82                                 (sup != type) ? sup : env.syms.objectType);
83    }
84
85    /**
86     * Return the interface types directly implemented by or extended by this
87     * parameterized type.
88     * Return an empty array if there are no interfaces.
89     */
90    public com.sun.javadoc.Type[] interfaceTypes() {
91        return TypeMaker.getTypes(env, env.types.interfaces(type));
92    }
93
94    /**
95     * Return the type that contains this type as a member.
96     * Return null is this is a top-level type.
97     */
98    public com.sun.javadoc.Type containingType() {
99        if (type.getEnclosingType().hasTag(CLASS)) {
100            // This is the type of an inner class.
101            return TypeMaker.getType(env, type.getEnclosingType());
102        }
103        ClassSymbol enclosing = type.tsym.owner.enclClass();
104        if (enclosing != null) {
105            // Nested but not inner.  Return the ClassDoc of the enclosing
106            // class or interface.
107            // See java.lang.reflect.ParameterizedType.getOwnerType().
108            return env.getClassDoc(enclosing);
109        }
110        return null;
111    }
112
113
114    // Asking for the "name" of a parameterized type doesn't exactly make
115    // sense.  It's a type expression.  Return the name of its generic
116    // type.
117    @Override
118    public String typeName() {
119        return TypeMaker.getTypeName(type, false);
120    }
121
122    @Override
123    public ParameterizedType asParameterizedType() {
124        return this;
125    }
126
127    @Override
128    public String toString() {
129        return parameterizedTypeToString(env, (ClassType)type, true);
130    }
131
132    static String parameterizedTypeToString(DocEnv env, ClassType cl,
133                                            boolean full) {
134        if (env.legacyDoclet) {
135            return TypeMaker.getTypeName(cl, full);
136        }
137        StringBuilder s = new StringBuilder();
138        if (!(cl.getEnclosingType().hasTag(CLASS))) {               // if not an inner class...
139            s.append(TypeMaker.getTypeName(cl, full));
140        } else {
141            ClassType encl = (ClassType)cl.getEnclosingType();
142            s.append(parameterizedTypeToString(env, encl, full))
143             .append('.')
144             .append(cl.tsym.name.toString());
145        }
146        s.append(TypeMaker.typeArgumentsString(env, cl, full));
147        return s.toString();
148    }
149}
150