author | bobv |
Tue, 07 Nov 2017 10:30:53 -0500 | |
changeset 47801 | c7b50c23ea71 |
parent 47216 | 71c04702a3d5 |
permissions | -rw-r--r-- |
7615 | 1 |
/* |
30730
d3ce7619db2c
8076543: Add @modules as needed to the langtools tests
akulyakh
parents:
27857
diff
changeset
|
2 |
* Copyright (c) 2010, 2015, Oracle and/or its affiliates. All rights reserved. |
7615 | 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 |
|
32059
ea04f56aeacd
8131915: CompletionFailure during import listing crashes javac
jlahoda
parents:
30730
diff
changeset
|
26 |
* @bug 7004029 8131915 |
7615 | 27 |
* @summary Basher for star-import scopes |
30730
d3ce7619db2c
8076543: Add @modules as needed to the langtools tests
akulyakh
parents:
27857
diff
changeset
|
28 |
* @modules jdk.compiler/com.sun.tools.javac.code |
d3ce7619db2c
8076543: Add @modules as needed to the langtools tests
akulyakh
parents:
27857
diff
changeset
|
29 |
* jdk.compiler/com.sun.tools.javac.file |
36526 | 30 |
* jdk.compiler/com.sun.tools.javac.tree |
30730
d3ce7619db2c
8076543: Add @modules as needed to the langtools tests
akulyakh
parents:
27857
diff
changeset
|
31 |
* jdk.compiler/com.sun.tools.javac.util |
7615 | 32 |
*/ |
33 |
||
34 |
import java.util.*; |
|
35 |
import java.util.List; |
|
25443
9187d77f2c64
8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents:
9087
diff
changeset
|
36 |
|
7615 | 37 |
import com.sun.tools.javac.code.*; |
25443
9187d77f2c64
8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents:
9087
diff
changeset
|
38 |
import com.sun.tools.javac.code.Scope.ImportFilter; |
9187d77f2c64
8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents:
9087
diff
changeset
|
39 |
import com.sun.tools.javac.code.Scope.StarImportScope; |
9187d77f2c64
8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents:
9087
diff
changeset
|
40 |
import com.sun.tools.javac.code.Scope.WriteableScope; |
7615 | 41 |
import com.sun.tools.javac.code.Symbol.*; |
42 |
import com.sun.tools.javac.file.JavacFileManager; |
|
32059
ea04f56aeacd
8131915: CompletionFailure during import listing crashes javac
jlahoda
parents:
30730
diff
changeset
|
43 |
import com.sun.tools.javac.tree.TreeMaker; |
25443
9187d77f2c64
8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents:
9087
diff
changeset
|
44 |
import com.sun.tools.javac.util.*; |
9187d77f2c64
8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents:
9087
diff
changeset
|
45 |
|
27224
228abfa87080
8054457: Refactor Symbol kinds from small ints to an enum
emc
parents:
25443
diff
changeset
|
46 |
import static com.sun.tools.javac.code.Kinds.Kind.*; |
7615 | 47 |
|
48 |
public class StarImportTest { |
|
49 |
public static void main(String... args) throws Exception { |
|
50 |
new StarImportTest().run(args); |
|
51 |
} |
|
52 |
||
53 |
void run(String... args) throws Exception { |
|
54 |
int count = 1; |
|
55 |
||
56 |
for (int i = 0; i < args.length; i++) { |
|
57 |
String arg = args[i]; |
|
58 |
if (arg.equals("-seed") && (i + 1 < args.length)) |
|
59 |
seed = Long.parseLong(args[++i]); |
|
60 |
else if(arg.equals("-tests") && (i + 1 < args.length)) |
|
61 |
count = Integer.parseInt(args[++i]); |
|
62 |
else |
|
63 |
throw new Exception("unknown arg: " + arg); |
|
64 |
} |
|
65 |
||
66 |
rgen = new Random(seed); |
|
67 |
||
68 |
for (int i = 0; i < count; i++) { |
|
69 |
Test t = new Test(); |
|
70 |
t.run(); |
|
71 |
} |
|
72 |
||
73 |
if (errors > 0) |
|
74 |
throw new Exception(errors + " errors found"); |
|
75 |
} |
|
76 |
||
77 |
/** |
|
78 |
* Select a random element from an array of choices. |
|
79 |
*/ |
|
80 |
<T> T random(T... choices) { |
|
81 |
return choices[rgen.nextInt(choices.length)]; |
|
82 |
} |
|
83 |
||
84 |
/** |
|
85 |
* Write a message to stderr. |
|
86 |
*/ |
|
87 |
void log(String msg) { |
|
88 |
System.err.println(msg); |
|
89 |
} |
|
90 |
||
91 |
/** |
|
92 |
* Write a message to stderr, and dump a scope. |
|
93 |
*/ |
|
94 |
void log(String msg, Scope s) { |
|
95 |
System.err.print(msg); |
|
96 |
System.err.print(": "); |
|
97 |
String sep = "("; |
|
25443
9187d77f2c64
8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents:
9087
diff
changeset
|
98 |
for (Symbol sym : s.getSymbols()) { |
9187d77f2c64
8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents:
9087
diff
changeset
|
99 |
System.err.print(sep + sym.name + ":" + sym); |
9187d77f2c64
8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents:
9087
diff
changeset
|
100 |
sep = ","; |
7615 | 101 |
} |
102 |
System.err.println(); |
|
103 |
} |
|
104 |
||
105 |
/** |
|
106 |
* Write an error message to stderr. |
|
107 |
*/ |
|
108 |
void error(String msg) { |
|
109 |
System.err.println("Error: " + msg); |
|
110 |
errors++; |
|
111 |
} |
|
112 |
||
113 |
Random rgen; |
|
114 |
long seed = 0; |
|
115 |
||
116 |
int errors; |
|
117 |
||
118 |
enum SetupKind { NAMES, PACKAGE, CLASS }; |
|
119 |
static final int MAX_SETUP_COUNT = 50; |
|
120 |
static final int MAX_SETUP_NAME_COUNT = 20; |
|
121 |
static final int MAX_SETUP_PACKAGE_COUNT = 20; |
|
122 |
static final int MAX_SETUP_CLASS_COUNT = 20; |
|
123 |
||
124 |
/** Class to encapsulate a test run. */ |
|
125 |
class Test { |
|
126 |
/** Run the test. */ |
|
127 |
void run() throws Exception { |
|
128 |
log ("starting test"); |
|
129 |
setup(); |
|
130 |
createStarImportScope(); |
|
131 |
test(); |
|
132 |
} |
|
133 |
||
134 |
/** |
|
135 |
* Setup env by creating pseudo-random collection of names, packages and classes. |
|
136 |
*/ |
|
137 |
void setup() { |
|
138 |
log ("setup"); |
|
139 |
context = new Context(); |
|
140 |
JavacFileManager.preRegister(context); // required by ClassReader which is required by Symtab |
|
32059
ea04f56aeacd
8131915: CompletionFailure during import listing crashes javac
jlahoda
parents:
30730
diff
changeset
|
141 |
make = TreeMaker.instance(context); |
7615 | 142 |
names = Names.instance(context); // Name.Table impls tied to an instance of Names |
143 |
symtab = Symtab.instance(context); |
|
27857 | 144 |
types = Types.instance(context); |
7615 | 145 |
int setupCount = rgen.nextInt(MAX_SETUP_COUNT); |
146 |
for (int i = 0; i < setupCount; i++) { |
|
147 |
switch (random(SetupKind.values())) { |
|
148 |
case NAMES: |
|
149 |
setupNames(); |
|
150 |
break; |
|
151 |
case PACKAGE: |
|
152 |
setupPackage(); |
|
153 |
break; |
|
154 |
case CLASS: |
|
155 |
setupClass(); |
|
156 |
break; |
|
157 |
} |
|
158 |
} |
|
159 |
} |
|
160 |
||
161 |
/** |
|
162 |
* Set up a random number of names. |
|
163 |
*/ |
|
164 |
void setupNames() { |
|
165 |
int count = rgen.nextInt(MAX_SETUP_NAME_COUNT); |
|
166 |
log("setup: creating " + count + " new names"); |
|
167 |
for (int i = 0; i < count; i++) { |
|
168 |
names.fromString("n" + (++nextNameSerial)); |
|
169 |
} |
|
170 |
} |
|
171 |
||
172 |
/** |
|
173 |
* Set up a package containing a random number of member elements. |
|
174 |
*/ |
|
175 |
void setupPackage() { |
|
176 |
Name name = names.fromString("p" + (++nextPackageSerial)); |
|
177 |
int count = rgen.nextInt(MAX_SETUP_PACKAGE_COUNT); |
|
178 |
log("setup: creating package " + name + " with " + count + " entries"); |
|
179 |
PackageSymbol p = new PackageSymbol(name, symtab.rootPackage); |
|
25443
9187d77f2c64
8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents:
9087
diff
changeset
|
180 |
p.members_field = WriteableScope.create(p); |
7615 | 181 |
for (int i = 0; i < count; i++) { |
182 |
String outer = name + "c" + i; |
|
183 |
String suffix = random(null, "$Entry", "$Entry2"); |
|
184 |
ClassSymbol c1 = createClass(names.fromString(outer), p); |
|
185 |
// log("setup: created " + c1); |
|
186 |
if (suffix != null) { |
|
187 |
ClassSymbol c2 = createClass(names.fromString(outer + suffix), p); |
|
188 |
// log("setup: created " + c2); |
|
189 |
} |
|
190 |
} |
|
191 |
// log("package " + p, p.members_field); |
|
192 |
packages.add(p); |
|
193 |
imports.add(p); |
|
194 |
} |
|
195 |
||
196 |
/** |
|
197 |
* Set up a class containing a random number of member elements. |
|
198 |
*/ |
|
199 |
void setupClass() { |
|
200 |
Name name = names.fromString("c" + (++nextClassSerial)); |
|
201 |
int count = rgen.nextInt(MAX_SETUP_CLASS_COUNT); |
|
202 |
log("setup: creating class " + name + " with " + count + " entries"); |
|
36526 | 203 |
ClassSymbol c = createClass(name, symtab.unnamedModule.unnamedPackage); |
7615 | 204 |
// log("setup: created " + c); |
205 |
for (int i = 0; i < count; i++) { |
|
206 |
ClassSymbol ic = createClass(names.fromString("Entry" + i), c); |
|
207 |
// log("setup: created " + ic); |
|
208 |
} |
|
209 |
classes.add(c); |
|
210 |
imports.add(c); |
|
211 |
} |
|
212 |
||
213 |
/** |
|
27857 | 214 |
* Create a star-import scope and a model thereof, from the packages and |
7615 | 215 |
* classes created by setupPackages and setupClasses. |
216 |
* @throws Exception for fatal errors, such as from reflection |
|
217 |
*/ |
|
218 |
void createStarImportScope() throws Exception { |
|
219 |
log ("createStarImportScope"); |
|
220 |
PackageSymbol pkg = new PackageSymbol(names.fromString("pkg"), symtab.rootPackage); |
|
221 |
||
25443
9187d77f2c64
8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents:
9087
diff
changeset
|
222 |
starImportScope = new StarImportScope(pkg); |
7615 | 223 |
starImportModel = new Model(); |
224 |
||
225 |
for (Symbol imp: imports) { |
|
226 |
Scope members = imp.members(); |
|
227 |
// log("importAll", members); |
|
27857 | 228 |
starImportScope.importAll(types, members, new ImportFilter() { |
25443
9187d77f2c64
8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents:
9087
diff
changeset
|
229 |
@Override |
9187d77f2c64
8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents:
9087
diff
changeset
|
230 |
public boolean accepts(Scope origin, Symbol t) { |
9187d77f2c64
8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents:
9087
diff
changeset
|
231 |
return t.kind == TYP; |
7615 | 232 |
} |
32059
ea04f56aeacd
8131915: CompletionFailure during import listing crashes javac
jlahoda
parents:
30730
diff
changeset
|
233 |
}, make.Import(null, false), (i, cf) -> { throw new IllegalStateException(); }); |
7615 | 234 |
|
25443
9187d77f2c64
8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents:
9087
diff
changeset
|
235 |
for (Symbol sym : members.getSymbols()) { |
9187d77f2c64
8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents:
9087
diff
changeset
|
236 |
starImportModel.enter(sym); |
7615 | 237 |
} |
238 |
} |
|
239 |
||
240 |
// log("star-import scope", starImportScope); |
|
241 |
starImportModel.check(starImportScope); |
|
242 |
} |
|
243 |
||
244 |
/** |
|
245 |
* The core of the test. In a random order, move nested classes from |
|
246 |
* the package in which they created to the class which should own them. |
|
247 |
*/ |
|
248 |
void test() { |
|
249 |
log ("test"); |
|
250 |
List<ClassSymbol> nestedClasses = new LinkedList<ClassSymbol>(); |
|
251 |
for (PackageSymbol p: packages) { |
|
25443
9187d77f2c64
8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents:
9087
diff
changeset
|
252 |
for (Symbol sym : p.members_field.getSymbols()) { |
9187d77f2c64
8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents:
9087
diff
changeset
|
253 |
if (sym.name.toString().contains("$")) |
9187d77f2c64
8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents:
9087
diff
changeset
|
254 |
nestedClasses.add((ClassSymbol) sym); |
7615 | 255 |
} |
256 |
} |
|
257 |
||
258 |
for (int i = nestedClasses.size(); i > 0; i--) { |
|
259 |
// select a random nested class to move from package to class |
|
260 |
ClassSymbol sym = nestedClasses.remove(rgen.nextInt(i)); |
|
261 |
log("adjusting class " + sym); |
|
262 |
||
263 |
// remove from star import model |
|
264 |
starImportModel.remove(sym); |
|
265 |
||
266 |
String s = sym.name.toString(); |
|
267 |
int dollar = s.indexOf("$"); |
|
268 |
||
269 |
// owner should be a package |
|
270 |
assert (sym.owner.kind == PCK); |
|
271 |
||
272 |
// determine new owner |
|
273 |
Name outerName = names.fromString(s.substring(0, dollar)); |
|
274 |
// log(sym + " owner: " + sym.owner, sym.owner.members()); |
|
25443
9187d77f2c64
8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents:
9087
diff
changeset
|
275 |
ClassSymbol outer = (ClassSymbol)sym.owner.members().findFirst(outerName); |
7615 | 276 |
// log("outer: " + outerName + " " + outer); |
277 |
||
278 |
// remove from package |
|
279 |
sym.owner.members().remove(sym); |
|
280 |
||
281 |
// rename and insert into class |
|
282 |
sym.name = names.fromString(s.substring(dollar + 1)); |
|
283 |
outer.members().enter(sym); |
|
284 |
sym.owner = outer; |
|
285 |
||
286 |
// verify |
|
287 |
starImportModel.check(starImportScope); |
|
288 |
} |
|
289 |
} |
|
290 |
||
291 |
ClassSymbol createClass(Name name, Symbol owner) { |
|
292 |
ClassSymbol sym = new ClassSymbol(0, name, owner); |
|
25443
9187d77f2c64
8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents:
9087
diff
changeset
|
293 |
sym.members_field = WriteableScope.create(sym); |
36526 | 294 |
if (owner != symtab.unnamedModule.unnamedPackage) |
7615 | 295 |
owner.members().enter(sym); |
296 |
return sym; |
|
297 |
} |
|
298 |
||
299 |
Context context; |
|
300 |
Symtab symtab; |
|
32059
ea04f56aeacd
8131915: CompletionFailure during import listing crashes javac
jlahoda
parents:
30730
diff
changeset
|
301 |
TreeMaker make; |
7615 | 302 |
Names names; |
27857 | 303 |
Types types; |
7615 | 304 |
int nextNameSerial; |
305 |
List<PackageSymbol> packages = new ArrayList<PackageSymbol>(); |
|
306 |
int nextPackageSerial; |
|
307 |
List<ClassSymbol> classes = new ArrayList<ClassSymbol>(); |
|
308 |
List<Symbol> imports = new ArrayList<Symbol>(); |
|
309 |
int nextClassSerial; |
|
310 |
||
25443
9187d77f2c64
8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents:
9087
diff
changeset
|
311 |
StarImportScope starImportScope; |
7615 | 312 |
Model starImportModel; |
313 |
} |
|
314 |
||
315 |
class Model { |
|
316 |
private Map<Name, Set<Symbol>> map = new HashMap<Name, Set<Symbol>>(); |
|
317 |
private Set<Symbol> bogus = new HashSet<Symbol>(); |
|
318 |
||
319 |
void enter(Symbol sym) { |
|
320 |
Set<Symbol> syms = map.get(sym.name); |
|
321 |
if (syms == null) |
|
322 |
map.put(sym.name, syms = new LinkedHashSet<Symbol>()); |
|
323 |
syms.add(sym); |
|
324 |
} |
|
325 |
||
326 |
void remove(Symbol sym) { |
|
327 |
Set<Symbol> syms = map.get(sym.name); |
|
328 |
if (syms == null) |
|
329 |
error("no entries for " + sym.name + " found in reference model"); |
|
330 |
else { |
|
331 |
boolean ok = syms.remove(sym); |
|
332 |
if (ok) { |
|
333 |
// log(sym.name + "(" + sym + ") removed from reference model"); |
|
334 |
} else { |
|
335 |
error(sym.name + " not found in reference model"); |
|
336 |
} |
|
337 |
if (syms.isEmpty()) |
|
338 |
map.remove(sym.name); |
|
339 |
} |
|
340 |
} |
|
341 |
||
342 |
/** |
|
343 |
* Check the contents of a scope |
|
344 |
*/ |
|
345 |
void check(Scope scope) { |
|
346 |
// First, check all entries in scope are in map |
|
347 |
int bogusCount = 0; |
|
25443
9187d77f2c64
8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents:
9087
diff
changeset
|
348 |
for (Symbol sym : scope.getSymbols()) { |
9187d77f2c64
8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents:
9087
diff
changeset
|
349 |
if (sym.owner != scope.getOrigin(sym).owner) { |
7615 | 350 |
if (bogus.contains(sym)) { |
351 |
bogusCount++; |
|
352 |
} else { |
|
353 |
log("Warning: " + sym.name + ":" + sym + " appears to be bogus"); |
|
354 |
bogus.add(sym); |
|
355 |
} |
|
356 |
} else { |
|
357 |
Set<Symbol> syms = map.get(sym.name); |
|
358 |
if (syms == null) { |
|
359 |
error("check: no entries found for " + sym.name + ":" + sym + " in reference map"); |
|
360 |
} else if (!syms.contains(sym)) { |
|
361 |
error("check: symbol " + sym.name + ":" + sym + " not found in reference map"); |
|
362 |
} |
|
363 |
} |
|
364 |
} |
|
365 |
if (bogusCount > 0) { |
|
366 |
log("Warning: " + bogusCount + " other bogus entries previously reported"); |
|
367 |
} |
|
368 |
||
369 |
// Second, check all entries in map are in scope |
|
370 |
for (Map.Entry<Name,Set<Symbol>> me: map.entrySet()) { |
|
371 |
Name name = me.getKey(); |
|
25443
9187d77f2c64
8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents:
9087
diff
changeset
|
372 |
if (scope.findFirst(name) == null) { |
7615 | 373 |
error("check: no entries found for " + name + " in scope"); |
374 |
continue; |
|
375 |
} |
|
376 |
nextSym: |
|
377 |
for (Symbol sym: me.getValue()) { |
|
25443
9187d77f2c64
8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents:
9087
diff
changeset
|
378 |
for (Symbol s : scope.getSymbolsByName(name)) { |
9187d77f2c64
8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents:
9087
diff
changeset
|
379 |
if (sym == s) |
7615 | 380 |
continue nextSym; |
381 |
} |
|
382 |
error("check: symbol " + sym + " not found in scope"); |
|
383 |
} |
|
384 |
} |
|
385 |
} |
|
386 |
} |
|
387 |
} |