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