StarImportTest.java revision 3294:9adfb22ff08f
1/* 2 * Copyright (c) 2010, 2015, 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 * @test 26 * @bug 7004029 8131915 27 * @summary Basher for star-import scopes 28 * @modules jdk.compiler/com.sun.tools.javac.code 29 * jdk.compiler/com.sun.tools.javac.file 30 * jdk.compiler/com.sun.tools.javac.tree 31 * jdk.compiler/com.sun.tools.javac.util 32 */ 33 34import java.util.*; 35import java.util.List; 36 37import com.sun.tools.javac.code.*; 38import com.sun.tools.javac.code.Scope.ImportFilter; 39import com.sun.tools.javac.code.Scope.StarImportScope; 40import com.sun.tools.javac.code.Scope.WriteableScope; 41import com.sun.tools.javac.code.Symbol.*; 42import com.sun.tools.javac.file.JavacFileManager; 43import com.sun.tools.javac.tree.TreeMaker; 44import com.sun.tools.javac.util.*; 45 46import static com.sun.tools.javac.code.Kinds.Kind.*; 47 48public 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 = "("; 98 for (Symbol sym : s.getSymbols()) { 99 System.err.print(sep + sym.name + ":" + sym); 100 sep = ","; 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 141 make = TreeMaker.instance(context); 142 names = Names.instance(context); // Name.Table impls tied to an instance of Names 143 symtab = Symtab.instance(context); 144 types = Types.instance(context); 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); 180 p.members_field = WriteableScope.create(p); 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"); 203 ClassSymbol c = createClass(name, symtab.unnamedModule.unnamedPackage); 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 /** 214 * Create a star-import scope and a model thereof, from the packages and 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 222 starImportScope = new StarImportScope(pkg); 223 starImportModel = new Model(); 224 225 for (Symbol imp: imports) { 226 Scope members = imp.members(); 227// log("importAll", members); 228 starImportScope.importAll(types, members, new ImportFilter() { 229 @Override 230 public boolean accepts(Scope origin, Symbol t) { 231 return t.kind == TYP; 232 } 233 }, make.Import(null, false), (i, cf) -> { throw new IllegalStateException(); }); 234 235 for (Symbol sym : members.getSymbols()) { 236 starImportModel.enter(sym); 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) { 252 for (Symbol sym : p.members_field.getSymbols()) { 253 if (sym.name.toString().contains("$")) 254 nestedClasses.add((ClassSymbol) sym); 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()); 275 ClassSymbol outer = (ClassSymbol)sym.owner.members().findFirst(outerName); 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); 293 sym.members_field = WriteableScope.create(sym); 294 if (owner != symtab.unnamedModule.unnamedPackage) 295 owner.members().enter(sym); 296 return sym; 297 } 298 299 Context context; 300 Symtab symtab; 301 TreeMaker make; 302 Names names; 303 Types types; 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 311 StarImportScope starImportScope; 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; 348 for (Symbol sym : scope.getSymbols()) { 349 if (sym.owner != scope.getOrigin(sym).owner) { 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(); 372 if (scope.findFirst(name) == null) { 373 error("check: no entries found for " + name + " in scope"); 374 continue; 375 } 376 nextSym: 377 for (Symbol sym: me.getValue()) { 378 for (Symbol s : scope.getSymbolsByName(name)) { 379 if (sym == s) 380 continue nextSym; 381 } 382 error("check: symbol " + sym + " not found in scope"); 383 } 384 } 385 } 386 } 387} 388