1 /* |
|
2 * Copyright (c) 2003, 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 */ |
|
25 |
|
26 package com.sun.tools.javadoc; |
|
27 |
|
28 import com.sun.javadoc.*; |
|
29 |
|
30 import com.sun.tools.javac.code.Attribute; |
|
31 import com.sun.tools.javac.code.Attribute.TypeCompound; |
|
32 import com.sun.tools.javac.code.Kinds; |
|
33 import com.sun.tools.javac.code.Kinds.KindSelector; |
|
34 import com.sun.tools.javac.code.Symbol; |
|
35 import com.sun.tools.javac.code.Symbol.ClassSymbol; |
|
36 import com.sun.tools.javac.code.Symbol.MethodSymbol; |
|
37 import com.sun.tools.javac.code.Type; |
|
38 import com.sun.tools.javac.code.Type.TypeVar; |
|
39 import com.sun.tools.javac.util.List; |
|
40 import com.sun.tools.javac.util.Name; |
|
41 import com.sun.tools.javac.util.Names; |
|
42 |
|
43 /** |
|
44 * Implementation of <code>TypeVariable</code>, which |
|
45 * represents a type variable. |
|
46 * |
|
47 * <p><b>This is NOT part of any supported API. |
|
48 * If you write code that depends on this, you do so at your own risk. |
|
49 * This code and its internal interfaces are subject to change or |
|
50 * deletion without notice.</b> |
|
51 * |
|
52 * @author Scott Seligman |
|
53 * @since 1.5 |
|
54 */ |
|
55 public class TypeVariableImpl extends AbstractTypeImpl implements TypeVariable { |
|
56 |
|
57 TypeVariableImpl(DocEnv env, TypeVar type) { |
|
58 super(env, type); |
|
59 } |
|
60 |
|
61 /** |
|
62 * Return the bounds of this type variable. |
|
63 */ |
|
64 public com.sun.javadoc.Type[] bounds() { |
|
65 return TypeMaker.getTypes(env, getBounds((TypeVar)type, env)); |
|
66 } |
|
67 |
|
68 /** |
|
69 * Return the class, interface, method, or constructor within |
|
70 * which this type variable is declared. |
|
71 */ |
|
72 public ProgramElementDoc owner() { |
|
73 Symbol osym = type.tsym.owner; |
|
74 if (osym.kind.matches(KindSelector.TYP)) { |
|
75 return env.getClassDoc((ClassSymbol)osym); |
|
76 } |
|
77 Names names = osym.name.table.names; |
|
78 if (osym.name == names.init) { |
|
79 return env.getConstructorDoc((MethodSymbol)osym); |
|
80 } else { |
|
81 return env.getMethodDoc((MethodSymbol)osym); |
|
82 } |
|
83 } |
|
84 |
|
85 /** |
|
86 * Return the ClassDoc of the erasure of this type variable. |
|
87 */ |
|
88 @Override |
|
89 public ClassDoc asClassDoc() { |
|
90 return env.getClassDoc((ClassSymbol)env.types.erasure(type).tsym); |
|
91 } |
|
92 |
|
93 @Override |
|
94 public TypeVariable asTypeVariable() { |
|
95 return this; |
|
96 } |
|
97 |
|
98 @Override |
|
99 public String toString() { |
|
100 return typeVarToString(env, (TypeVar)type, true); |
|
101 } |
|
102 |
|
103 |
|
104 /** |
|
105 * Return the string form of a type variable along with any |
|
106 * "extends" clause. Class names are qualified if "full" is true. |
|
107 */ |
|
108 static String typeVarToString(DocEnv env, TypeVar v, boolean full) { |
|
109 StringBuilder s = new StringBuilder(v.toString()); |
|
110 List<Type> bounds = getBounds(v, env); |
|
111 if (bounds.nonEmpty()) { |
|
112 boolean first = true; |
|
113 for (Type b : bounds) { |
|
114 s.append(first ? " extends " : " & "); |
|
115 s.append(TypeMaker.getTypeString(env, b, full)); |
|
116 first = false; |
|
117 } |
|
118 } |
|
119 return s.toString(); |
|
120 } |
|
121 |
|
122 /** |
|
123 * Get the bounds of a type variable as listed in the "extends" clause. |
|
124 */ |
|
125 private static List<Type> getBounds(TypeVar v, DocEnv env) { |
|
126 final Type upperBound = v.getUpperBound(); |
|
127 Name boundname = upperBound.tsym.getQualifiedName(); |
|
128 if (boundname == boundname.table.names.java_lang_Object |
|
129 && !upperBound.isAnnotated()) { |
|
130 return List.nil(); |
|
131 } else { |
|
132 return env.types.getBounds(v); |
|
133 } |
|
134 } |
|
135 |
|
136 /** |
|
137 * Get the annotations of this program element. |
|
138 * Return an empty array if there are none. |
|
139 */ |
|
140 public AnnotationDesc[] annotations() { |
|
141 if (!type.isAnnotated()) { |
|
142 return new AnnotationDesc[0]; |
|
143 } |
|
144 List<? extends TypeCompound> tas = type.getAnnotationMirrors(); |
|
145 AnnotationDesc res[] = new AnnotationDesc[tas.length()]; |
|
146 int i = 0; |
|
147 for (Attribute.Compound a : tas) { |
|
148 res[i++] = new AnnotationDescImpl(env, a); |
|
149 } |
|
150 return res; |
|
151 } |
|
152 } |
|