aoqi@0: /* aoqi@0: * Copyright (c) 2010, 2011, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: */ aoqi@0: aoqi@0: /* aoqi@0: * @test aoqi@0: * @bug 7004029 aoqi@0: * @summary Basher for star-import scopes aoqi@0: */ aoqi@0: aoqi@0: import java.lang.reflect.*; aoqi@0: import java.util.*; aoqi@0: import java.util.List; aoqi@0: import com.sun.tools.javac.util.*; aoqi@0: import com.sun.tools.javac.code.*; aoqi@0: import com.sun.tools.javac.code.Scope.*; aoqi@0: import com.sun.tools.javac.code.Symbol.*; aoqi@0: import com.sun.tools.javac.file.JavacFileManager; aoqi@0: import static com.sun.tools.javac.code.Kinds.*; aoqi@0: aoqi@0: public class StarImportTest { aoqi@0: public static void main(String... args) throws Exception { aoqi@0: new StarImportTest().run(args); aoqi@0: } aoqi@0: aoqi@0: void run(String... args) throws Exception { aoqi@0: int count = 1; aoqi@0: aoqi@0: for (int i = 0; i < args.length; i++) { aoqi@0: String arg = args[i]; aoqi@0: if (arg.equals("-seed") && (i + 1 < args.length)) aoqi@0: seed = Long.parseLong(args[++i]); aoqi@0: else if(arg.equals("-tests") && (i + 1 < args.length)) aoqi@0: count = Integer.parseInt(args[++i]); aoqi@0: else aoqi@0: throw new Exception("unknown arg: " + arg); aoqi@0: } aoqi@0: aoqi@0: rgen = new Random(seed); aoqi@0: aoqi@0: for (int i = 0; i < count; i++) { aoqi@0: Test t = new Test(); aoqi@0: t.run(); aoqi@0: } aoqi@0: aoqi@0: if (errors > 0) aoqi@0: throw new Exception(errors + " errors found"); aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Select a random element from an array of choices. aoqi@0: */ aoqi@0: T random(T... choices) { aoqi@0: return choices[rgen.nextInt(choices.length)]; aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Write a message to stderr. aoqi@0: */ aoqi@0: void log(String msg) { aoqi@0: System.err.println(msg); aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Write a message to stderr, and dump a scope. aoqi@0: */ aoqi@0: void log(String msg, Scope s) { aoqi@0: System.err.print(msg); aoqi@0: System.err.print(": "); aoqi@0: String sep = "("; aoqi@0: for (Scope.Entry se = s.elems; se != null; se = se.sibling) { aoqi@0: for (Scope.Entry e = se; e.sym != null; e = e.next()) { aoqi@0: System.err.print(sep + e.sym.name + ":" + e.sym); aoqi@0: sep = ","; aoqi@0: } aoqi@0: System.err.print(")"); aoqi@0: sep = ", ("; aoqi@0: } aoqi@0: System.err.println(); aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Write an error message to stderr. aoqi@0: */ aoqi@0: void error(String msg) { aoqi@0: System.err.println("Error: " + msg); aoqi@0: errors++; aoqi@0: } aoqi@0: aoqi@0: Random rgen; aoqi@0: long seed = 0; aoqi@0: aoqi@0: int errors; aoqi@0: aoqi@0: enum SetupKind { NAMES, PACKAGE, CLASS }; aoqi@0: static final int MAX_SETUP_COUNT = 50; aoqi@0: static final int MAX_SETUP_NAME_COUNT = 20; aoqi@0: static final int MAX_SETUP_PACKAGE_COUNT = 20; aoqi@0: static final int MAX_SETUP_CLASS_COUNT = 20; aoqi@0: aoqi@0: /** Class to encapsulate a test run. */ aoqi@0: class Test { aoqi@0: /** Run the test. */ aoqi@0: void run() throws Exception { aoqi@0: log ("starting test"); aoqi@0: setup(); aoqi@0: createStarImportScope(); aoqi@0: test(); aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Setup env by creating pseudo-random collection of names, packages and classes. aoqi@0: */ aoqi@0: void setup() { aoqi@0: log ("setup"); aoqi@0: context = new Context(); aoqi@0: JavacFileManager.preRegister(context); // required by ClassReader which is required by Symtab aoqi@0: names = Names.instance(context); // Name.Table impls tied to an instance of Names aoqi@0: symtab = Symtab.instance(context); aoqi@0: int setupCount = rgen.nextInt(MAX_SETUP_COUNT); aoqi@0: for (int i = 0; i < setupCount; i++) { aoqi@0: switch (random(SetupKind.values())) { aoqi@0: case NAMES: aoqi@0: setupNames(); aoqi@0: break; aoqi@0: case PACKAGE: aoqi@0: setupPackage(); aoqi@0: break; aoqi@0: case CLASS: aoqi@0: setupClass(); aoqi@0: break; aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Set up a random number of names. aoqi@0: */ aoqi@0: void setupNames() { aoqi@0: int count = rgen.nextInt(MAX_SETUP_NAME_COUNT); aoqi@0: log("setup: creating " + count + " new names"); aoqi@0: for (int i = 0; i < count; i++) { aoqi@0: names.fromString("n" + (++nextNameSerial)); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Set up a package containing a random number of member elements. aoqi@0: */ aoqi@0: void setupPackage() { aoqi@0: Name name = names.fromString("p" + (++nextPackageSerial)); aoqi@0: int count = rgen.nextInt(MAX_SETUP_PACKAGE_COUNT); aoqi@0: log("setup: creating package " + name + " with " + count + " entries"); aoqi@0: PackageSymbol p = new PackageSymbol(name, symtab.rootPackage); aoqi@0: p.members_field = new Scope(p); aoqi@0: for (int i = 0; i < count; i++) { aoqi@0: String outer = name + "c" + i; aoqi@0: String suffix = random(null, "$Entry", "$Entry2"); aoqi@0: ClassSymbol c1 = createClass(names.fromString(outer), p); aoqi@0: // log("setup: created " + c1); aoqi@0: if (suffix != null) { aoqi@0: ClassSymbol c2 = createClass(names.fromString(outer + suffix), p); aoqi@0: // log("setup: created " + c2); aoqi@0: } aoqi@0: } aoqi@0: // log("package " + p, p.members_field); aoqi@0: packages.add(p); aoqi@0: imports.add(p); aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Set up a class containing a random number of member elements. aoqi@0: */ aoqi@0: void setupClass() { aoqi@0: Name name = names.fromString("c" + (++nextClassSerial)); aoqi@0: int count = rgen.nextInt(MAX_SETUP_CLASS_COUNT); aoqi@0: log("setup: creating class " + name + " with " + count + " entries"); aoqi@0: ClassSymbol c = createClass(name, symtab.unnamedPackage); aoqi@0: // log("setup: created " + c); aoqi@0: for (int i = 0; i < count; i++) { aoqi@0: ClassSymbol ic = createClass(names.fromString("Entry" + i), c); aoqi@0: // log("setup: created " + ic); aoqi@0: } aoqi@0: classes.add(c); aoqi@0: imports.add(c); aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Create a star-import scope and a model therof, from the packages and aoqi@0: * classes created by setupPackages and setupClasses. aoqi@0: * @throws Exception for fatal errors, such as from reflection aoqi@0: */ aoqi@0: void createStarImportScope() throws Exception { aoqi@0: log ("createStarImportScope"); aoqi@0: PackageSymbol pkg = new PackageSymbol(names.fromString("pkg"), symtab.rootPackage); aoqi@0: aoqi@0: // if StarImportScope exists, use it, otherwise, for testing legacy code, aoqi@0: // fall back on ImportScope aoqi@0: Method importAll; aoqi@0: try { aoqi@0: Class c = Class.forName("com.sun.tools.javac.code.Scope$StarImportScope"); aoqi@0: Constructor ctor = c.getDeclaredConstructor(new Class[] { Symbol.class }); aoqi@0: importAll = c.getDeclaredMethod("importAll", new Class[] { Scope.class }); aoqi@0: starImportScope = (Scope) ctor.newInstance(new Object[] { pkg }); aoqi@0: } catch (ClassNotFoundException e) { aoqi@0: starImportScope = new ImportScope(pkg); aoqi@0: importAll = null; aoqi@0: } aoqi@0: starImportModel = new Model(); aoqi@0: aoqi@0: for (Symbol imp: imports) { aoqi@0: Scope members = imp.members(); aoqi@0: if (importAll != null) { aoqi@0: // log("importAll", members); aoqi@0: importAll.invoke(starImportScope, members); aoqi@0: } else { aoqi@0: Scope fromScope = members; aoqi@0: Scope toScope = starImportScope; aoqi@0: // The following lines are taken from MemberEnter.importAll, aoqi@0: // before the use of StarImportScope.importAll. aoqi@0: for (Scope.Entry e = fromScope.elems; e != null; e = e.sibling) { aoqi@0: if (e.sym.kind == TYP && !toScope.includes(e.sym)) aoqi@0: toScope.enter(e.sym, fromScope); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: for (Scope.Entry e = members.elems; e != null; e = e.sibling) { aoqi@0: starImportModel.enter(e.sym); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: // log("star-import scope", starImportScope); aoqi@0: starImportModel.check(starImportScope); aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * The core of the test. In a random order, move nested classes from aoqi@0: * the package in which they created to the class which should own them. aoqi@0: */ aoqi@0: void test() { aoqi@0: log ("test"); aoqi@0: List nestedClasses = new LinkedList(); aoqi@0: for (PackageSymbol p: packages) { aoqi@0: for (Scope.Entry se = p.members_field.elems; se != null; se = se.sibling) { aoqi@0: if (se.sym.name.toString().contains("$")) aoqi@0: nestedClasses.add((ClassSymbol) se.sym); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: for (int i = nestedClasses.size(); i > 0; i--) { aoqi@0: // select a random nested class to move from package to class aoqi@0: ClassSymbol sym = nestedClasses.remove(rgen.nextInt(i)); aoqi@0: log("adjusting class " + sym); aoqi@0: aoqi@0: // remove from star import model aoqi@0: starImportModel.remove(sym); aoqi@0: aoqi@0: String s = sym.name.toString(); aoqi@0: int dollar = s.indexOf("$"); aoqi@0: aoqi@0: // owner should be a package aoqi@0: assert (sym.owner.kind == PCK); aoqi@0: aoqi@0: // determine new owner aoqi@0: Name outerName = names.fromString(s.substring(0, dollar)); aoqi@0: // log(sym + " owner: " + sym.owner, sym.owner.members()); aoqi@0: Scope.Entry outerEntry = sym.owner.members().lookup(outerName); aoqi@0: ClassSymbol outer = (ClassSymbol) outerEntry.sym; aoqi@0: // log("outer: " + outerName + " " + outer); aoqi@0: aoqi@0: // remove from package aoqi@0: sym.owner.members().remove(sym); aoqi@0: aoqi@0: // rename and insert into class aoqi@0: sym.name = names.fromString(s.substring(dollar + 1)); aoqi@0: outer.members().enter(sym); aoqi@0: sym.owner = outer; aoqi@0: aoqi@0: // verify aoqi@0: starImportModel.check(starImportScope); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: ClassSymbol createClass(Name name, Symbol owner) { aoqi@0: ClassSymbol sym = new ClassSymbol(0, name, owner); aoqi@0: sym.members_field = new Scope(sym); aoqi@0: if (owner != symtab.unnamedPackage) aoqi@0: owner.members().enter(sym); aoqi@0: return sym; aoqi@0: } aoqi@0: aoqi@0: Context context; aoqi@0: Symtab symtab; aoqi@0: Names names; aoqi@0: int nextNameSerial; aoqi@0: List packages = new ArrayList(); aoqi@0: int nextPackageSerial; aoqi@0: List classes = new ArrayList(); aoqi@0: List imports = new ArrayList(); aoqi@0: int nextClassSerial; aoqi@0: aoqi@0: Scope starImportScope; aoqi@0: Model starImportModel; aoqi@0: } aoqi@0: aoqi@0: class Model { aoqi@0: private Map> map = new HashMap>(); aoqi@0: private Set bogus = new HashSet(); aoqi@0: aoqi@0: void enter(Symbol sym) { aoqi@0: Set syms = map.get(sym.name); aoqi@0: if (syms == null) aoqi@0: map.put(sym.name, syms = new LinkedHashSet()); aoqi@0: syms.add(sym); aoqi@0: } aoqi@0: aoqi@0: void remove(Symbol sym) { aoqi@0: Set syms = map.get(sym.name); aoqi@0: if (syms == null) aoqi@0: error("no entries for " + sym.name + " found in reference model"); aoqi@0: else { aoqi@0: boolean ok = syms.remove(sym); aoqi@0: if (ok) { aoqi@0: // log(sym.name + "(" + sym + ") removed from reference model"); aoqi@0: } else { aoqi@0: error(sym.name + " not found in reference model"); aoqi@0: } aoqi@0: if (syms.isEmpty()) aoqi@0: map.remove(sym.name); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Check the contents of a scope aoqi@0: */ aoqi@0: void check(Scope scope) { aoqi@0: // First, check all entries in scope are in map aoqi@0: int bogusCount = 0; aoqi@0: for (Scope.Entry se = scope.elems; se != null; se = se.sibling) { aoqi@0: Symbol sym = se.sym; aoqi@0: if (sym.owner != se.scope.owner) { aoqi@0: if (bogus.contains(sym)) { aoqi@0: bogusCount++; aoqi@0: } else { aoqi@0: log("Warning: " + sym.name + ":" + sym + " appears to be bogus"); aoqi@0: bogus.add(sym); aoqi@0: } aoqi@0: } else { aoqi@0: Set syms = map.get(sym.name); aoqi@0: if (syms == null) { aoqi@0: error("check: no entries found for " + sym.name + ":" + sym + " in reference map"); aoqi@0: } else if (!syms.contains(sym)) { aoqi@0: error("check: symbol " + sym.name + ":" + sym + " not found in reference map"); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: if (bogusCount > 0) { aoqi@0: log("Warning: " + bogusCount + " other bogus entries previously reported"); aoqi@0: } aoqi@0: aoqi@0: // Second, check all entries in map are in scope aoqi@0: for (Map.Entry> me: map.entrySet()) { aoqi@0: Name name = me.getKey(); aoqi@0: Scope.Entry se = scope.lookup(name); aoqi@0: assert (se != null); aoqi@0: if (se.sym == null) { aoqi@0: error("check: no entries found for " + name + " in scope"); aoqi@0: continue; aoqi@0: } aoqi@0: nextSym: aoqi@0: for (Symbol sym: me.getValue()) { aoqi@0: for (Scope.Entry e = se; e.sym != null; e = e.next()) { aoqi@0: if (sym == e.sym) aoqi@0: continue nextSym; aoqi@0: } aoqi@0: error("check: symbol " + sym + " not found in scope"); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: }