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