1 /* |
|
2 * Copyright (c) 2004, 2008, 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. |
|
8 * |
|
9 * This code is distributed in the hope that it will be useful, but WITHOUT |
|
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
12 * version 2 for more details (a copy is included in the LICENSE file that |
|
13 * accompanied this code). |
|
14 * |
|
15 * You should have received a copy of the GNU General Public License version |
|
16 * 2 along with this work; if not, write to the Free Software Foundation, |
|
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
18 * |
|
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
20 * or visit www.oracle.com if you need additional information or have any |
|
21 * questions. |
|
22 */ |
|
23 |
|
24 |
|
25 /* |
|
26 * @test |
|
27 * @bug 4853450 5055963 |
|
28 * @summary InterfaceType tests |
|
29 * @library ../../lib |
|
30 * @run main/othervm InterfaceTyp |
|
31 */ |
|
32 |
|
33 |
|
34 import java.util.*; |
|
35 import com.sun.mirror.declaration.*; |
|
36 import com.sun.mirror.type.*; |
|
37 import com.sun.mirror.util.*; |
|
38 |
|
39 |
|
40 public class InterfaceTyp<T1,T2> extends Tester { |
|
41 |
|
42 public static void main(String[] args) { |
|
43 (new InterfaceTyp()).run(); |
|
44 } |
|
45 |
|
46 |
|
47 // Declarations used by tests |
|
48 |
|
49 interface I1<S> extends Set<String> { |
|
50 interface I2<R> { |
|
51 } |
|
52 } |
|
53 |
|
54 |
|
55 // Generate some interface types to test |
|
56 private I1<T1> f0; |
|
57 private I1<String> f1; |
|
58 private I1 f2; |
|
59 private I1.I2<String> f3; |
|
60 private I1.I2 f4; |
|
61 private I1<T1> f5; |
|
62 private I3<T1> f6; |
|
63 private static final int NUMTYPES = 7; |
|
64 |
|
65 // Type mirrors corresponding to the types of the above fields |
|
66 private InterfaceType[] t = new InterfaceType[NUMTYPES]; |
|
67 |
|
68 protected void init() { |
|
69 for (int i = 0; i < t.length; i++) { |
|
70 t[i] = (InterfaceType) getField("f"+i).getType(); |
|
71 } |
|
72 } |
|
73 |
|
74 |
|
75 // TypeMirror methods |
|
76 |
|
77 @Test(result="interface") |
|
78 Collection<String> accept() { |
|
79 final Collection<String> res = new ArrayList<String>(); |
|
80 |
|
81 t[0].accept(new SimpleTypeVisitor() { |
|
82 public void visitReferenceType(ReferenceType t) { |
|
83 res.add("ref type"); |
|
84 } |
|
85 public void visitClassType(ClassType t) { |
|
86 res.add("class"); |
|
87 } |
|
88 public void visitInterfaceType(InterfaceType t) { |
|
89 res.add("interface"); |
|
90 } |
|
91 }); |
|
92 return res; |
|
93 } |
|
94 |
|
95 @Test(result="true") |
|
96 boolean equals1() { |
|
97 return t[0].equals(t[0]); |
|
98 } |
|
99 |
|
100 @Test(result="false") |
|
101 boolean equals2() { |
|
102 return t[0].equals(t[1]); |
|
103 } |
|
104 |
|
105 // Raw type is not same as type instantiated with unbounded type var. |
|
106 @Test(result="false") |
|
107 boolean equals3() { |
|
108 return t[0].equals(t[2]); |
|
109 } |
|
110 |
|
111 // I1<T1> is same type as I1<T1> |
|
112 @Test(result="true") |
|
113 boolean equals4() { |
|
114 return t[0].equals(t[5]); |
|
115 } |
|
116 |
|
117 @Test(result={ |
|
118 "InterfaceTyp.I1<T1>", |
|
119 "InterfaceTyp.I1<java.lang.String>", |
|
120 "InterfaceTyp.I1", |
|
121 "InterfaceTyp.I1.I2<java.lang.String>", |
|
122 "InterfaceTyp.I1.I2", |
|
123 "InterfaceTyp.I1<T1>", |
|
124 "I3<T1>" |
|
125 }, |
|
126 ordered=true) |
|
127 Collection<String> toStringTests() { |
|
128 Collection<String> res = new ArrayList<String>(); |
|
129 for (InterfaceType i : t) { |
|
130 res.add(i.toString()); |
|
131 } |
|
132 return res; |
|
133 } |
|
134 |
|
135 |
|
136 // DeclaredType methods |
|
137 |
|
138 @Test(result={"T1"}) |
|
139 Collection<TypeMirror> getActualTypeArguments1() { |
|
140 return t[0].getActualTypeArguments(); |
|
141 } |
|
142 |
|
143 @Test(result={}) |
|
144 Collection<TypeMirror> getActualTypeArguments2() { |
|
145 return t[2].getActualTypeArguments(); |
|
146 } |
|
147 |
|
148 @Test(result={"java.lang.String"}) |
|
149 Collection<TypeMirror> getActualTypeArguments3() { |
|
150 return t[3].getActualTypeArguments(); |
|
151 } |
|
152 |
|
153 @Test(result="InterfaceTyp") |
|
154 DeclaredType getContainingType1() { |
|
155 return t[0].getContainingType(); |
|
156 } |
|
157 |
|
158 @Test(result="InterfaceTyp.I1") |
|
159 DeclaredType getContainingType2() { |
|
160 return t[3].getContainingType(); |
|
161 } |
|
162 |
|
163 @Test(result="null") |
|
164 DeclaredType getContainingTypeTopLevel() { |
|
165 return t[6].getContainingType(); |
|
166 } |
|
167 |
|
168 @Test(result={"java.util.Set<java.lang.String>"}) |
|
169 Collection<InterfaceType> getSuperinterfaces() { |
|
170 return t[0].getSuperinterfaces(); |
|
171 } |
|
172 |
|
173 |
|
174 |
|
175 // InterfaceType method |
|
176 |
|
177 @Test(result="InterfaceTyp.I1<S>") |
|
178 InterfaceDeclaration getDeclaration1() { |
|
179 return t[0].getDeclaration(); |
|
180 } |
|
181 |
|
182 @Test(result="InterfaceTyp.I1.I2<R>") |
|
183 InterfaceDeclaration getDeclaration2a() { |
|
184 return t[3].getDeclaration(); |
|
185 } |
|
186 |
|
187 @Test(result="InterfaceTyp.I1.I2<R>") |
|
188 InterfaceDeclaration getDeclaration2b() { |
|
189 return t[4].getDeclaration(); |
|
190 } |
|
191 |
|
192 @Test(result="true") |
|
193 boolean getDeclarationCaching() { |
|
194 return t[0].getDeclaration() == t[5].getDeclaration(); |
|
195 } |
|
196 } |
|
197 |
|
198 |
|
199 // A top-level interface used by tests. |
|
200 |
|
201 interface I3<T> { |
|
202 } |
|