1 /*
2  * Copyright (c) 2003, 2018, 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 
26 package com.sun.tools.javadoc.main;
27 
28 import com.sun.javadoc.*;
29 
30 import com.sun.tools.javac.code.Symbol.ClassSymbol;
31 import com.sun.tools.javac.code.Type;
32 import com.sun.tools.javac.util.List;
33 
34 
35 /**
36  * Implementation of <code>WildcardType</code>, which
37  * represents a wildcard type.
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  * @author Scott Seligman
45  * @since 1.5
46  */
47 @Deprecated(since="9", forRemoval=true)
48 @SuppressWarnings("removal")
49 public class WildcardTypeImpl extends AbstractTypeImpl implements WildcardType {
50 
WildcardTypeImpl(DocEnv env, Type.WildcardType type)51     WildcardTypeImpl(DocEnv env, Type.WildcardType type) {
52         super(env, type);
53     }
54 
55     /**
56      * Return the upper bounds of this wildcard type argument
57      * as given by the <i>extends</i> clause.
58      * Return an empty array if no such bounds are explicitly given.
59      */
extendsBounds()60     public com.sun.javadoc.Type[] extendsBounds() {
61         return TypeMaker.getTypes(env, getExtendsBounds((Type.WildcardType)type));
62     }
63 
64     /**
65      * Return the lower bounds of this wildcard type argument
66      * as given by the <i>super</i> clause.
67      * Return an empty array if no such bounds are explicitly given.
68      */
superBounds()69     public com.sun.javadoc.Type[] superBounds() {
70         return TypeMaker.getTypes(env, getSuperBounds((Type.WildcardType)type));
71     }
72 
73     /**
74      * Return the ClassDoc of the erasure of this wildcard type.
75      */
76     @Override
asClassDoc()77     public ClassDoc asClassDoc() {
78         return env.getClassDoc((ClassSymbol)env.types.erasure(type).tsym);
79     }
80 
81     @Override
asWildcardType()82     public WildcardType asWildcardType() {
83         return this;
84     }
85 
86     @Override
typeName()87     public String typeName()            { return "?"; }
88     @Override
qualifiedTypeName()89     public String qualifiedTypeName()   { return "?"; }
90     @Override
simpleTypeName()91     public String simpleTypeName()      { return "?"; }
92 
93     @Override
toString()94     public String toString() {
95         return wildcardTypeToString(env, (Type.WildcardType)type, true);
96     }
97 
98 
99     /**
100      * Return the string form of a wildcard type ("?") along with any
101      * "extends" or "super" clause.  Delimiting brackets are not
102      * included.  Class names are qualified if "full" is true.
103      */
wildcardTypeToString(DocEnv env, Type.WildcardType wildThing, boolean full)104     static String wildcardTypeToString(DocEnv env,
105                                        Type.WildcardType wildThing, boolean full) {
106         if (env.legacyDoclet) {
107             return TypeMaker.getTypeName(env.types.erasure(wildThing), full);
108         }
109         StringBuilder s = new StringBuilder("?");
110         List<Type> bounds = getExtendsBounds(wildThing);
111         if (bounds.nonEmpty()) {
112             s.append(" extends ");
113         } else {
114             bounds = getSuperBounds(wildThing);
115             if (bounds.nonEmpty()) {
116                 s.append(" super ");
117             }
118         }
119         boolean first = true;   // currently only one bound is allowed
120         for (Type b : bounds) {
121             if (!first) {
122                 s.append(" & ");
123             }
124             s.append(TypeMaker.getTypeString(env, b, full));
125             first = false;
126         }
127         return s.toString();
128     }
129 
getExtendsBounds(Type.WildcardType wild)130     private static List<Type> getExtendsBounds(Type.WildcardType wild) {
131         return wild.isSuperBound()
132                 ? List.nil()
133                 : List.of(wild.type);
134     }
135 
getSuperBounds(Type.WildcardType wild)136     private static List<Type> getSuperBounds(Type.WildcardType wild) {
137         return wild.isExtendsBound()
138                 ? List.nil()
139                 : List.of(wild.type);
140     }
141 }
142