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