test/tools/javac/scope/StarImportTest.java

Wed, 27 Apr 2016 01:34:52 +0800

author
aoqi
date
Wed, 27 Apr 2016 01:34:52 +0800
changeset 0
959103a6100f
child 2525
2eb010b6cb22
permissions
-rw-r--r--

Initial load
http://hg.openjdk.java.net/jdk8u/jdk8u/langtools/
changeset: 2573:53ca196be1ae
tag: jdk8u25-b17

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 2010, 2011, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation.
aoqi@0 8 *
aoqi@0 9 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 12 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 13 * accompanied this code).
aoqi@0 14 *
aoqi@0 15 * You should have received a copy of the GNU General Public License version
aoqi@0 16 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 18 *
aoqi@0 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 20 * or visit www.oracle.com if you need additional information or have any
aoqi@0 21 * questions.
aoqi@0 22 */
aoqi@0 23
aoqi@0 24 /*
aoqi@0 25 * @test
aoqi@0 26 * @bug 7004029
aoqi@0 27 * @summary Basher for star-import scopes
aoqi@0 28 */
aoqi@0 29
aoqi@0 30 import java.lang.reflect.*;
aoqi@0 31 import java.util.*;
aoqi@0 32 import java.util.List;
aoqi@0 33 import com.sun.tools.javac.util.*;
aoqi@0 34 import com.sun.tools.javac.code.*;
aoqi@0 35 import com.sun.tools.javac.code.Scope.*;
aoqi@0 36 import com.sun.tools.javac.code.Symbol.*;
aoqi@0 37 import com.sun.tools.javac.file.JavacFileManager;
aoqi@0 38 import static com.sun.tools.javac.code.Kinds.*;
aoqi@0 39
aoqi@0 40 public class StarImportTest {
aoqi@0 41 public static void main(String... args) throws Exception {
aoqi@0 42 new StarImportTest().run(args);
aoqi@0 43 }
aoqi@0 44
aoqi@0 45 void run(String... args) throws Exception {
aoqi@0 46 int count = 1;
aoqi@0 47
aoqi@0 48 for (int i = 0; i < args.length; i++) {
aoqi@0 49 String arg = args[i];
aoqi@0 50 if (arg.equals("-seed") && (i + 1 < args.length))
aoqi@0 51 seed = Long.parseLong(args[++i]);
aoqi@0 52 else if(arg.equals("-tests") && (i + 1 < args.length))
aoqi@0 53 count = Integer.parseInt(args[++i]);
aoqi@0 54 else
aoqi@0 55 throw new Exception("unknown arg: " + arg);
aoqi@0 56 }
aoqi@0 57
aoqi@0 58 rgen = new Random(seed);
aoqi@0 59
aoqi@0 60 for (int i = 0; i < count; i++) {
aoqi@0 61 Test t = new Test();
aoqi@0 62 t.run();
aoqi@0 63 }
aoqi@0 64
aoqi@0 65 if (errors > 0)
aoqi@0 66 throw new Exception(errors + " errors found");
aoqi@0 67 }
aoqi@0 68
aoqi@0 69 /**
aoqi@0 70 * Select a random element from an array of choices.
aoqi@0 71 */
aoqi@0 72 <T> T random(T... choices) {
aoqi@0 73 return choices[rgen.nextInt(choices.length)];
aoqi@0 74 }
aoqi@0 75
aoqi@0 76 /**
aoqi@0 77 * Write a message to stderr.
aoqi@0 78 */
aoqi@0 79 void log(String msg) {
aoqi@0 80 System.err.println(msg);
aoqi@0 81 }
aoqi@0 82
aoqi@0 83 /**
aoqi@0 84 * Write a message to stderr, and dump a scope.
aoqi@0 85 */
aoqi@0 86 void log(String msg, Scope s) {
aoqi@0 87 System.err.print(msg);
aoqi@0 88 System.err.print(": ");
aoqi@0 89 String sep = "(";
aoqi@0 90 for (Scope.Entry se = s.elems; se != null; se = se.sibling) {
aoqi@0 91 for (Scope.Entry e = se; e.sym != null; e = e.next()) {
aoqi@0 92 System.err.print(sep + e.sym.name + ":" + e.sym);
aoqi@0 93 sep = ",";
aoqi@0 94 }
aoqi@0 95 System.err.print(")");
aoqi@0 96 sep = ", (";
aoqi@0 97 }
aoqi@0 98 System.err.println();
aoqi@0 99 }
aoqi@0 100
aoqi@0 101 /**
aoqi@0 102 * Write an error message to stderr.
aoqi@0 103 */
aoqi@0 104 void error(String msg) {
aoqi@0 105 System.err.println("Error: " + msg);
aoqi@0 106 errors++;
aoqi@0 107 }
aoqi@0 108
aoqi@0 109 Random rgen;
aoqi@0 110 long seed = 0;
aoqi@0 111
aoqi@0 112 int errors;
aoqi@0 113
aoqi@0 114 enum SetupKind { NAMES, PACKAGE, CLASS };
aoqi@0 115 static final int MAX_SETUP_COUNT = 50;
aoqi@0 116 static final int MAX_SETUP_NAME_COUNT = 20;
aoqi@0 117 static final int MAX_SETUP_PACKAGE_COUNT = 20;
aoqi@0 118 static final int MAX_SETUP_CLASS_COUNT = 20;
aoqi@0 119
aoqi@0 120 /** Class to encapsulate a test run. */
aoqi@0 121 class Test {
aoqi@0 122 /** Run the test. */
aoqi@0 123 void run() throws Exception {
aoqi@0 124 log ("starting test");
aoqi@0 125 setup();
aoqi@0 126 createStarImportScope();
aoqi@0 127 test();
aoqi@0 128 }
aoqi@0 129
aoqi@0 130 /**
aoqi@0 131 * Setup env by creating pseudo-random collection of names, packages and classes.
aoqi@0 132 */
aoqi@0 133 void setup() {
aoqi@0 134 log ("setup");
aoqi@0 135 context = new Context();
aoqi@0 136 JavacFileManager.preRegister(context); // required by ClassReader which is required by Symtab
aoqi@0 137 names = Names.instance(context); // Name.Table impls tied to an instance of Names
aoqi@0 138 symtab = Symtab.instance(context);
aoqi@0 139 int setupCount = rgen.nextInt(MAX_SETUP_COUNT);
aoqi@0 140 for (int i = 0; i < setupCount; i++) {
aoqi@0 141 switch (random(SetupKind.values())) {
aoqi@0 142 case NAMES:
aoqi@0 143 setupNames();
aoqi@0 144 break;
aoqi@0 145 case PACKAGE:
aoqi@0 146 setupPackage();
aoqi@0 147 break;
aoqi@0 148 case CLASS:
aoqi@0 149 setupClass();
aoqi@0 150 break;
aoqi@0 151 }
aoqi@0 152 }
aoqi@0 153 }
aoqi@0 154
aoqi@0 155 /**
aoqi@0 156 * Set up a random number of names.
aoqi@0 157 */
aoqi@0 158 void setupNames() {
aoqi@0 159 int count = rgen.nextInt(MAX_SETUP_NAME_COUNT);
aoqi@0 160 log("setup: creating " + count + " new names");
aoqi@0 161 for (int i = 0; i < count; i++) {
aoqi@0 162 names.fromString("n" + (++nextNameSerial));
aoqi@0 163 }
aoqi@0 164 }
aoqi@0 165
aoqi@0 166 /**
aoqi@0 167 * Set up a package containing a random number of member elements.
aoqi@0 168 */
aoqi@0 169 void setupPackage() {
aoqi@0 170 Name name = names.fromString("p" + (++nextPackageSerial));
aoqi@0 171 int count = rgen.nextInt(MAX_SETUP_PACKAGE_COUNT);
aoqi@0 172 log("setup: creating package " + name + " with " + count + " entries");
aoqi@0 173 PackageSymbol p = new PackageSymbol(name, symtab.rootPackage);
aoqi@0 174 p.members_field = new Scope(p);
aoqi@0 175 for (int i = 0; i < count; i++) {
aoqi@0 176 String outer = name + "c" + i;
aoqi@0 177 String suffix = random(null, "$Entry", "$Entry2");
aoqi@0 178 ClassSymbol c1 = createClass(names.fromString(outer), p);
aoqi@0 179 // log("setup: created " + c1);
aoqi@0 180 if (suffix != null) {
aoqi@0 181 ClassSymbol c2 = createClass(names.fromString(outer + suffix), p);
aoqi@0 182 // log("setup: created " + c2);
aoqi@0 183 }
aoqi@0 184 }
aoqi@0 185 // log("package " + p, p.members_field);
aoqi@0 186 packages.add(p);
aoqi@0 187 imports.add(p);
aoqi@0 188 }
aoqi@0 189
aoqi@0 190 /**
aoqi@0 191 * Set up a class containing a random number of member elements.
aoqi@0 192 */
aoqi@0 193 void setupClass() {
aoqi@0 194 Name name = names.fromString("c" + (++nextClassSerial));
aoqi@0 195 int count = rgen.nextInt(MAX_SETUP_CLASS_COUNT);
aoqi@0 196 log("setup: creating class " + name + " with " + count + " entries");
aoqi@0 197 ClassSymbol c = createClass(name, symtab.unnamedPackage);
aoqi@0 198 // log("setup: created " + c);
aoqi@0 199 for (int i = 0; i < count; i++) {
aoqi@0 200 ClassSymbol ic = createClass(names.fromString("Entry" + i), c);
aoqi@0 201 // log("setup: created " + ic);
aoqi@0 202 }
aoqi@0 203 classes.add(c);
aoqi@0 204 imports.add(c);
aoqi@0 205 }
aoqi@0 206
aoqi@0 207 /**
aoqi@0 208 * Create a star-import scope and a model therof, from the packages and
aoqi@0 209 * classes created by setupPackages and setupClasses.
aoqi@0 210 * @throws Exception for fatal errors, such as from reflection
aoqi@0 211 */
aoqi@0 212 void createStarImportScope() throws Exception {
aoqi@0 213 log ("createStarImportScope");
aoqi@0 214 PackageSymbol pkg = new PackageSymbol(names.fromString("pkg"), symtab.rootPackage);
aoqi@0 215
aoqi@0 216 // if StarImportScope exists, use it, otherwise, for testing legacy code,
aoqi@0 217 // fall back on ImportScope
aoqi@0 218 Method importAll;
aoqi@0 219 try {
aoqi@0 220 Class<?> c = Class.forName("com.sun.tools.javac.code.Scope$StarImportScope");
aoqi@0 221 Constructor ctor = c.getDeclaredConstructor(new Class[] { Symbol.class });
aoqi@0 222 importAll = c.getDeclaredMethod("importAll", new Class[] { Scope.class });
aoqi@0 223 starImportScope = (Scope) ctor.newInstance(new Object[] { pkg });
aoqi@0 224 } catch (ClassNotFoundException e) {
aoqi@0 225 starImportScope = new ImportScope(pkg);
aoqi@0 226 importAll = null;
aoqi@0 227 }
aoqi@0 228 starImportModel = new Model();
aoqi@0 229
aoqi@0 230 for (Symbol imp: imports) {
aoqi@0 231 Scope members = imp.members();
aoqi@0 232 if (importAll != null) {
aoqi@0 233 // log("importAll", members);
aoqi@0 234 importAll.invoke(starImportScope, members);
aoqi@0 235 } else {
aoqi@0 236 Scope fromScope = members;
aoqi@0 237 Scope toScope = starImportScope;
aoqi@0 238 // The following lines are taken from MemberEnter.importAll,
aoqi@0 239 // before the use of StarImportScope.importAll.
aoqi@0 240 for (Scope.Entry e = fromScope.elems; e != null; e = e.sibling) {
aoqi@0 241 if (e.sym.kind == TYP && !toScope.includes(e.sym))
aoqi@0 242 toScope.enter(e.sym, fromScope);
aoqi@0 243 }
aoqi@0 244 }
aoqi@0 245
aoqi@0 246 for (Scope.Entry e = members.elems; e != null; e = e.sibling) {
aoqi@0 247 starImportModel.enter(e.sym);
aoqi@0 248 }
aoqi@0 249 }
aoqi@0 250
aoqi@0 251 // log("star-import scope", starImportScope);
aoqi@0 252 starImportModel.check(starImportScope);
aoqi@0 253 }
aoqi@0 254
aoqi@0 255 /**
aoqi@0 256 * The core of the test. In a random order, move nested classes from
aoqi@0 257 * the package in which they created to the class which should own them.
aoqi@0 258 */
aoqi@0 259 void test() {
aoqi@0 260 log ("test");
aoqi@0 261 List<ClassSymbol> nestedClasses = new LinkedList<ClassSymbol>();
aoqi@0 262 for (PackageSymbol p: packages) {
aoqi@0 263 for (Scope.Entry se = p.members_field.elems; se != null; se = se.sibling) {
aoqi@0 264 if (se.sym.name.toString().contains("$"))
aoqi@0 265 nestedClasses.add((ClassSymbol) se.sym);
aoqi@0 266 }
aoqi@0 267 }
aoqi@0 268
aoqi@0 269 for (int i = nestedClasses.size(); i > 0; i--) {
aoqi@0 270 // select a random nested class to move from package to class
aoqi@0 271 ClassSymbol sym = nestedClasses.remove(rgen.nextInt(i));
aoqi@0 272 log("adjusting class " + sym);
aoqi@0 273
aoqi@0 274 // remove from star import model
aoqi@0 275 starImportModel.remove(sym);
aoqi@0 276
aoqi@0 277 String s = sym.name.toString();
aoqi@0 278 int dollar = s.indexOf("$");
aoqi@0 279
aoqi@0 280 // owner should be a package
aoqi@0 281 assert (sym.owner.kind == PCK);
aoqi@0 282
aoqi@0 283 // determine new owner
aoqi@0 284 Name outerName = names.fromString(s.substring(0, dollar));
aoqi@0 285 // log(sym + " owner: " + sym.owner, sym.owner.members());
aoqi@0 286 Scope.Entry outerEntry = sym.owner.members().lookup(outerName);
aoqi@0 287 ClassSymbol outer = (ClassSymbol) outerEntry.sym;
aoqi@0 288 // log("outer: " + outerName + " " + outer);
aoqi@0 289
aoqi@0 290 // remove from package
aoqi@0 291 sym.owner.members().remove(sym);
aoqi@0 292
aoqi@0 293 // rename and insert into class
aoqi@0 294 sym.name = names.fromString(s.substring(dollar + 1));
aoqi@0 295 outer.members().enter(sym);
aoqi@0 296 sym.owner = outer;
aoqi@0 297
aoqi@0 298 // verify
aoqi@0 299 starImportModel.check(starImportScope);
aoqi@0 300 }
aoqi@0 301 }
aoqi@0 302
aoqi@0 303 ClassSymbol createClass(Name name, Symbol owner) {
aoqi@0 304 ClassSymbol sym = new ClassSymbol(0, name, owner);
aoqi@0 305 sym.members_field = new Scope(sym);
aoqi@0 306 if (owner != symtab.unnamedPackage)
aoqi@0 307 owner.members().enter(sym);
aoqi@0 308 return sym;
aoqi@0 309 }
aoqi@0 310
aoqi@0 311 Context context;
aoqi@0 312 Symtab symtab;
aoqi@0 313 Names names;
aoqi@0 314 int nextNameSerial;
aoqi@0 315 List<PackageSymbol> packages = new ArrayList<PackageSymbol>();
aoqi@0 316 int nextPackageSerial;
aoqi@0 317 List<ClassSymbol> classes = new ArrayList<ClassSymbol>();
aoqi@0 318 List<Symbol> imports = new ArrayList<Symbol>();
aoqi@0 319 int nextClassSerial;
aoqi@0 320
aoqi@0 321 Scope starImportScope;
aoqi@0 322 Model starImportModel;
aoqi@0 323 }
aoqi@0 324
aoqi@0 325 class Model {
aoqi@0 326 private Map<Name, Set<Symbol>> map = new HashMap<Name, Set<Symbol>>();
aoqi@0 327 private Set<Symbol> bogus = new HashSet<Symbol>();
aoqi@0 328
aoqi@0 329 void enter(Symbol sym) {
aoqi@0 330 Set<Symbol> syms = map.get(sym.name);
aoqi@0 331 if (syms == null)
aoqi@0 332 map.put(sym.name, syms = new LinkedHashSet<Symbol>());
aoqi@0 333 syms.add(sym);
aoqi@0 334 }
aoqi@0 335
aoqi@0 336 void remove(Symbol sym) {
aoqi@0 337 Set<Symbol> syms = map.get(sym.name);
aoqi@0 338 if (syms == null)
aoqi@0 339 error("no entries for " + sym.name + " found in reference model");
aoqi@0 340 else {
aoqi@0 341 boolean ok = syms.remove(sym);
aoqi@0 342 if (ok) {
aoqi@0 343 // log(sym.name + "(" + sym + ") removed from reference model");
aoqi@0 344 } else {
aoqi@0 345 error(sym.name + " not found in reference model");
aoqi@0 346 }
aoqi@0 347 if (syms.isEmpty())
aoqi@0 348 map.remove(sym.name);
aoqi@0 349 }
aoqi@0 350 }
aoqi@0 351
aoqi@0 352 /**
aoqi@0 353 * Check the contents of a scope
aoqi@0 354 */
aoqi@0 355 void check(Scope scope) {
aoqi@0 356 // First, check all entries in scope are in map
aoqi@0 357 int bogusCount = 0;
aoqi@0 358 for (Scope.Entry se = scope.elems; se != null; se = se.sibling) {
aoqi@0 359 Symbol sym = se.sym;
aoqi@0 360 if (sym.owner != se.scope.owner) {
aoqi@0 361 if (bogus.contains(sym)) {
aoqi@0 362 bogusCount++;
aoqi@0 363 } else {
aoqi@0 364 log("Warning: " + sym.name + ":" + sym + " appears to be bogus");
aoqi@0 365 bogus.add(sym);
aoqi@0 366 }
aoqi@0 367 } else {
aoqi@0 368 Set<Symbol> syms = map.get(sym.name);
aoqi@0 369 if (syms == null) {
aoqi@0 370 error("check: no entries found for " + sym.name + ":" + sym + " in reference map");
aoqi@0 371 } else if (!syms.contains(sym)) {
aoqi@0 372 error("check: symbol " + sym.name + ":" + sym + " not found in reference map");
aoqi@0 373 }
aoqi@0 374 }
aoqi@0 375 }
aoqi@0 376 if (bogusCount > 0) {
aoqi@0 377 log("Warning: " + bogusCount + " other bogus entries previously reported");
aoqi@0 378 }
aoqi@0 379
aoqi@0 380 // Second, check all entries in map are in scope
aoqi@0 381 for (Map.Entry<Name,Set<Symbol>> me: map.entrySet()) {
aoqi@0 382 Name name = me.getKey();
aoqi@0 383 Scope.Entry se = scope.lookup(name);
aoqi@0 384 assert (se != null);
aoqi@0 385 if (se.sym == null) {
aoqi@0 386 error("check: no entries found for " + name + " in scope");
aoqi@0 387 continue;
aoqi@0 388 }
aoqi@0 389 nextSym:
aoqi@0 390 for (Symbol sym: me.getValue()) {
aoqi@0 391 for (Scope.Entry e = se; e.sym != null; e = e.next()) {
aoqi@0 392 if (sym == e.sym)
aoqi@0 393 continue nextSym;
aoqi@0 394 }
aoqi@0 395 error("check: symbol " + sym + " not found in scope");
aoqi@0 396 }
aoqi@0 397 }
aoqi@0 398 }
aoqi@0 399 }
aoqi@0 400 }

mercurial