author | akulyakh |
Thu, 21 May 2015 11:41:04 -0700 | |
changeset 30730 | d3ce7619db2c |
parent 19654 | 560c81cdf377 |
permissions | -rw-r--r-- |
19654 | 1 |
/* |
30730
d3ce7619db2c
8076543: Add @modules as needed to the langtools tests
akulyakh
parents:
19654
diff
changeset
|
2 |
* Copyright (c) 2013, 2015, Oracle and/or its affiliates. All rights reserved. |
19654 | 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 |
* @test |
|
26 |
* @bug 7118412 |
|
27 |
* @summary Shadowing of type-variables vs. member types |
|
30730
d3ce7619db2c
8076543: Add @modules as needed to the langtools tests
akulyakh
parents:
19654
diff
changeset
|
28 |
* @modules jdk.compiler |
19654 | 29 |
*/ |
30 |
import java.io.File; |
|
31 |
import java.io.FileWriter; |
|
32 |
import java.io.IOException; |
|
33 |
import java.io.PrintWriter; |
|
34 |
import java.io.StringWriter; |
|
35 |
||
36 |
public class ShadowingTest { |
|
37 |
||
38 |
// We generate a method "test" that tries to call T.<something, |
|
39 |
// depending on the value of MethodCall>. This controls whether |
|
40 |
// "test" is static or not. |
|
41 |
private enum MethodContext { |
|
42 |
STATIC("static "), |
|
43 |
INSTANCE(""); |
|
44 |
||
45 |
public final String methodcontext; |
|
46 |
||
47 |
MethodContext(final String methodcontext) { |
|
48 |
this.methodcontext = methodcontext; |
|
49 |
} |
|
50 |
} |
|
51 |
||
52 |
// These control whether or not a type parameter, method type |
|
53 |
// parameter, or inner class get declared (and in the case of |
|
54 |
// inner classes, whether it's static or not. |
|
55 |
||
56 |
private enum MethodTypeParameterDecl { |
|
57 |
NO(""), |
|
58 |
YES("<T extends Number> "); |
|
59 |
||
60 |
public final String tyvar; |
|
61 |
||
62 |
MethodTypeParameterDecl(final String tyvar) { |
|
63 |
this.tyvar = tyvar; |
|
64 |
} |
|
65 |
} |
|
66 |
||
67 |
private enum InsideDef { |
|
68 |
NONE(""), |
|
69 |
STATIC("static class T { public void inner() {} }\n"), |
|
70 |
INSTANCE("class T { public void inner() {} }\n"); |
|
71 |
||
72 |
public final String instancedef; |
|
73 |
||
74 |
InsideDef(final String instancedef) { |
|
75 |
this.instancedef = instancedef; |
|
76 |
} |
|
77 |
} |
|
78 |
||
79 |
private enum TypeParameterDecl { |
|
80 |
NO(""), |
|
81 |
YES("<T extends Collection>"); |
|
82 |
||
83 |
public final String tyvar; |
|
84 |
||
85 |
TypeParameterDecl(final String tyvar) { |
|
86 |
this.tyvar = tyvar; |
|
87 |
} |
|
88 |
} |
|
89 |
||
90 |
// Represents what method we try to call. This is a way of |
|
91 |
// checking which T we're seeing. |
|
92 |
private enum MethodCall { |
|
93 |
// Method type variables extend Number, so we have intValue |
|
94 |
METHOD_TYPEVAR("intValue"), |
|
95 |
// The inner class declaration has a method called "inner" |
|
96 |
INNER_CLASS("inner"), |
|
97 |
// The class type variables extend Collection, so we call iterator |
|
98 |
TYPEVAR("iterator"), |
|
99 |
// The outer class declaration has a method called "outer" |
|
100 |
OUTER_CLASS("outer"); |
|
101 |
||
102 |
public final String methodcall; |
|
103 |
||
104 |
MethodCall(final String methodcall) { |
|
105 |
this.methodcall = methodcall; |
|
106 |
} |
|
107 |
||
108 |
} |
|
109 |
||
110 |
public boolean succeeds(final MethodCall call, |
|
111 |
final MethodTypeParameterDecl mtyvar, |
|
112 |
final MethodContext ctx, |
|
113 |
final InsideDef inside, |
|
114 |
final TypeParameterDecl tyvar) { |
|
115 |
switch(call) { |
|
116 |
// We want to resolve to the method type variable |
|
117 |
case METHOD_TYPEVAR: switch(mtyvar) { |
|
118 |
// If the method type variable exists, then T will |
|
119 |
// resolve to it, and we'll have intValue. |
|
120 |
case YES: return true; |
|
121 |
// Otherwise, this cannot succeed. |
|
122 |
default: return false; |
|
123 |
} |
|
124 |
// We want to resolve to the inner class |
|
125 |
case INNER_CLASS: switch(mtyvar) { |
|
126 |
// The method type parameter will shadow the inner |
|
127 |
// class, so there can't be one. |
|
128 |
case NO: switch(ctx) { |
|
129 |
// If we're not static, then either one should succeed. |
|
130 |
case INSTANCE: switch(inside) { |
|
131 |
case INSTANCE: |
|
132 |
case STATIC: |
|
133 |
return true; |
|
134 |
default: return false; |
|
135 |
} |
|
136 |
case STATIC: switch(inside) { |
|
137 |
// If we are static, and the inner class is |
|
138 |
// static, then we also succeed, because we |
|
139 |
// can't see the type variable. |
|
140 |
case STATIC: return true; |
|
141 |
case INSTANCE: switch(tyvar) { |
|
142 |
// If we're calling from a non-static |
|
143 |
// context, there can't be a class type |
|
144 |
// variable, because that will shadow the |
|
145 |
// static inner class definition. |
|
146 |
case NO: return true; |
|
147 |
default: return false; |
|
148 |
} |
|
149 |
// If the inner class isn't declared, we can't |
|
150 |
// see it. |
|
151 |
default: return false; |
|
152 |
} |
|
153 |
// Can't get here. |
|
154 |
default: return false; |
|
155 |
} |
|
156 |
default: return false; |
|
157 |
} |
|
158 |
// We want to resolve to the class type parameter |
|
159 |
case TYPEVAR: switch(mtyvar) { |
|
160 |
// We can't have a method type parameter, as that would |
|
161 |
// shadow the class type parameter |
|
162 |
case NO: switch(ctx) { |
|
163 |
case INSTANCE: switch(inside) { |
|
164 |
// We have to be in an instance context. If |
|
165 |
// we're static, we can't see the type |
|
166 |
// variable. |
|
167 |
case NONE: switch(tyvar) { |
|
168 |
// Obviously, the type parameter has to be declared. |
|
169 |
case YES: return true; |
|
170 |
default: return false; |
|
171 |
} |
|
172 |
default: return false; |
|
173 |
} |
|
174 |
default: return false; |
|
175 |
} |
|
176 |
default: return false; |
|
177 |
} |
|
178 |
// We want to resolve to the outer class |
|
179 |
case OUTER_CLASS: switch(mtyvar) { |
|
180 |
case NO: switch(inside) { |
|
181 |
case NONE: switch(tyvar) { |
|
182 |
// Basically, nothing else can be declared, or |
|
183 |
// else we can't see it. Even if our context |
|
184 |
// is static, the compiler will complain if |
|
185 |
// non-static T's exist, because they will |
|
186 |
// shadow the outer class. |
|
187 |
case NO: return true; |
|
188 |
default: return false; |
|
189 |
} |
|
190 |
default: return false; |
|
191 |
} |
|
192 |
default: return false; |
|
193 |
} |
|
194 |
} |
|
195 |
return false; |
|
196 |
} |
|
197 |
||
198 |
private static final File classesdir = new File("7118412"); |
|
199 |
||
200 |
private int errors = 0; |
|
201 |
||
202 |
private int dirnum = 0; |
|
203 |
||
204 |
private void doTest(final MethodTypeParameterDecl mtyvar, |
|
205 |
final TypeParameterDecl tyvar, |
|
206 |
final InsideDef insidedef, final MethodContext ctx, |
|
207 |
final MethodCall call) |
|
208 |
throws IOException { |
|
209 |
final String content = "import java.util.Collection;\n" + |
|
210 |
"class Test" + tyvar.tyvar + " {\n" + |
|
211 |
" " + insidedef.instancedef + |
|
212 |
" " + ctx.methodcontext + mtyvar.tyvar + "void test(T t) { t." + |
|
213 |
call.methodcall + "(); }\n" + |
|
214 |
"}\n" + |
|
215 |
"class T { void outer() {} }\n"; |
|
216 |
final File dir = new File(classesdir, "" + dirnum); |
|
217 |
final File Test_java = writeFile(dir, "Test.java", content); |
|
218 |
dirnum++; |
|
219 |
if(succeeds(call, mtyvar, ctx, insidedef, tyvar)) { |
|
220 |
if(!assert_compile_succeed(Test_java)) |
|
221 |
System.err.println("Failed file:\n" + content); |
|
222 |
} |
|
223 |
else { |
|
224 |
if(!assert_compile_fail(Test_java)) |
|
225 |
System.err.println("Failed file:\n" + content); |
|
226 |
} |
|
227 |
} |
|
228 |
||
229 |
private void run() throws Exception { |
|
230 |
classesdir.mkdir(); |
|
231 |
for(MethodTypeParameterDecl mtyvar : MethodTypeParameterDecl.values()) |
|
232 |
for(TypeParameterDecl tyvar : TypeParameterDecl.values()) |
|
233 |
for(InsideDef insidedef : InsideDef.values()) |
|
234 |
for(MethodContext ctx : MethodContext.values()) |
|
235 |
for(MethodCall methodcall : MethodCall.values()) |
|
236 |
doTest(mtyvar, tyvar, insidedef, ctx, methodcall); |
|
237 |
if (errors != 0) |
|
238 |
throw new Exception("ShadowingTest test failed with " + |
|
239 |
errors + " errors."); |
|
240 |
} |
|
241 |
||
242 |
private boolean assert_compile_fail(final File file) { |
|
243 |
final String filename = file.getPath(); |
|
244 |
final String[] args = { filename }; |
|
245 |
final StringWriter sw = new StringWriter(); |
|
246 |
final PrintWriter pw = new PrintWriter(sw); |
|
247 |
final int rc = com.sun.tools.javac.Main.compile(args, pw); |
|
248 |
pw.close(); |
|
249 |
if (rc == 0) { |
|
250 |
System.err.println("Compilation of " + file.getName() + |
|
251 |
" didn't fail as expected."); |
|
252 |
errors++; |
|
253 |
return false; |
|
254 |
} else return true; |
|
255 |
} |
|
256 |
||
257 |
private boolean assert_compile_succeed(final File file) { |
|
258 |
final String filename = file.getPath(); |
|
259 |
final String[] args = { filename }; |
|
260 |
final StringWriter sw = new StringWriter(); |
|
261 |
final PrintWriter pw = new PrintWriter(sw); |
|
262 |
final int rc = com.sun.tools.javac.Main.compile(args, pw); |
|
263 |
pw.close(); |
|
264 |
if (rc != 0) { |
|
265 |
System.err.println("Compilation of " + file.getName() + |
|
266 |
" didn't succeed as expected. Output:"); |
|
267 |
System.err.println(sw.toString()); |
|
268 |
errors++; |
|
269 |
return false; |
|
270 |
} else return true; |
|
271 |
} |
|
272 |
||
273 |
private File writeFile(final File dir, |
|
274 |
final String path, |
|
275 |
final String body) throws IOException { |
|
276 |
final File f = new File(dir, path); |
|
277 |
f.getParentFile().mkdirs(); |
|
278 |
final FileWriter out = new FileWriter(f); |
|
279 |
out.write(body); |
|
280 |
out.close(); |
|
281 |
return f; |
|
282 |
} |
|
283 |
||
284 |
public static void main(String... args) throws Exception { |
|
285 |
new ShadowingTest().run(); |
|
286 |
} |
|
287 |
||
288 |
} |