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