langtools/test/tools/javac/scope/HashCollisionTest.java
author jlahoda
Mon, 03 Aug 2015 13:28:39 +0200
changeset 32059 ea04f56aeacd
parent 30730 d3ce7619db2c
child 36526 3b41f1c69604
permissions -rw-r--r--
8131915: CompletionFailure during import listing crashes javac Summary: Handling CompletionFailures during import listing properly. Reviewed-by: mcimadamore
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
7615
8bc078486f2b 7004029: intermittent failures compiling pack200
jjg
parents:
diff changeset
     1
/*
30730
d3ce7619db2c 8076543: Add @modules as needed to the langtools tests
akulyakh
parents: 27857
diff changeset
     2
 * Copyright (c) 2010, 2015, Oracle and/or its affiliates. All rights reserved.
7615
8bc078486f2b 7004029: intermittent failures compiling pack200
jjg
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8bc078486f2b 7004029: intermittent failures compiling pack200
jjg
parents:
diff changeset
     4
 *
8bc078486f2b 7004029: intermittent failures compiling pack200
jjg
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
8bc078486f2b 7004029: intermittent failures compiling pack200
jjg
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
8bc078486f2b 7004029: intermittent failures compiling pack200
jjg
parents:
diff changeset
     7
 * published by the Free Software Foundation.
8bc078486f2b 7004029: intermittent failures compiling pack200
jjg
parents:
diff changeset
     8
 *
8bc078486f2b 7004029: intermittent failures compiling pack200
jjg
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
8bc078486f2b 7004029: intermittent failures compiling pack200
jjg
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
8bc078486f2b 7004029: intermittent failures compiling pack200
jjg
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
8bc078486f2b 7004029: intermittent failures compiling pack200
jjg
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
8bc078486f2b 7004029: intermittent failures compiling pack200
jjg
parents:
diff changeset
    13
 * accompanied this code).
8bc078486f2b 7004029: intermittent failures compiling pack200
jjg
parents:
diff changeset
    14
 *
8bc078486f2b 7004029: intermittent failures compiling pack200
jjg
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
8bc078486f2b 7004029: intermittent failures compiling pack200
jjg
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
8bc078486f2b 7004029: intermittent failures compiling pack200
jjg
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
8bc078486f2b 7004029: intermittent failures compiling pack200
jjg
parents:
diff changeset
    18
 *
8bc078486f2b 7004029: intermittent failures compiling pack200
jjg
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
8bc078486f2b 7004029: intermittent failures compiling pack200
jjg
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
8bc078486f2b 7004029: intermittent failures compiling pack200
jjg
parents:
diff changeset
    21
 * questions.
8bc078486f2b 7004029: intermittent failures compiling pack200
jjg
parents:
diff changeset
    22
 */
8bc078486f2b 7004029: intermittent failures compiling pack200
jjg
parents:
diff changeset
    23
8bc078486f2b 7004029: intermittent failures compiling pack200
jjg
parents:
diff changeset
    24
/*
8bc078486f2b 7004029: intermittent failures compiling pack200
jjg
parents:
diff changeset
    25
 * @test
32059
ea04f56aeacd 8131915: CompletionFailure during import listing crashes javac
jlahoda
parents: 30730
diff changeset
    26
 * @bug 7004029 8131915
7615
8bc078486f2b 7004029: intermittent failures compiling pack200
jjg
parents:
diff changeset
    27
 * @summary Ensure Scope impl can cope with hash collisions
25443
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 9087
diff changeset
    28
 * @library /tools/javac/lib
30730
d3ce7619db2c 8076543: Add @modules as needed to the langtools tests
akulyakh
parents: 27857
diff changeset
    29
 * @modules jdk.compiler/com.sun.tools.javac.api
d3ce7619db2c 8076543: Add @modules as needed to the langtools tests
akulyakh
parents: 27857
diff changeset
    30
 *          jdk.compiler/com.sun.tools.javac.code
d3ce7619db2c 8076543: Add @modules as needed to the langtools tests
akulyakh
parents: 27857
diff changeset
    31
 *          jdk.compiler/com.sun.tools.javac.file
d3ce7619db2c 8076543: Add @modules as needed to the langtools tests
akulyakh
parents: 27857
diff changeset
    32
 *          jdk.compiler/com.sun.tools.javac.tree
d3ce7619db2c 8076543: Add @modules as needed to the langtools tests
akulyakh
parents: 27857
diff changeset
    33
 *          jdk.compiler/com.sun.tools.javac.util
25443
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 9087
diff changeset
    34
 * @build DPrinter HashCollisionTest
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 9087
diff changeset
    35
 * @run main HashCollisionTest
7615
8bc078486f2b 7004029: intermittent failures compiling pack200
jjg
parents:
diff changeset
    36
 */
8bc078486f2b 7004029: intermittent failures compiling pack200
jjg
parents:
diff changeset
    37
8bc078486f2b 7004029: intermittent failures compiling pack200
jjg
parents:
diff changeset
    38
import java.lang.reflect.*;
8bc078486f2b 7004029: intermittent failures compiling pack200
jjg
parents:
diff changeset
    39
import java.io.*;
32059
ea04f56aeacd 8131915: CompletionFailure during import listing crashes javac
jlahoda
parents: 30730
diff changeset
    40
import java.util.function.BiConsumer;
25443
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 9087
diff changeset
    41
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 9087
diff changeset
    42
import com.sun.source.util.Trees;
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 9087
diff changeset
    43
import com.sun.tools.javac.api.JavacTrees;
7615
8bc078486f2b 7004029: intermittent failures compiling pack200
jjg
parents:
diff changeset
    44
import com.sun.tools.javac.util.*;
8bc078486f2b 7004029: intermittent failures compiling pack200
jjg
parents:
diff changeset
    45
import com.sun.tools.javac.code.*;
8bc078486f2b 7004029: intermittent failures compiling pack200
jjg
parents:
diff changeset
    46
import com.sun.tools.javac.code.Scope.*;
8bc078486f2b 7004029: intermittent failures compiling pack200
jjg
parents:
diff changeset
    47
import com.sun.tools.javac.code.Symbol.*;
8bc078486f2b 7004029: intermittent failures compiling pack200
jjg
parents:
diff changeset
    48
import com.sun.tools.javac.file.JavacFileManager;
32059
ea04f56aeacd 8131915: CompletionFailure during import listing crashes javac
jlahoda
parents: 30730
diff changeset
    49
import com.sun.tools.javac.tree.JCTree.JCImport;
ea04f56aeacd 8131915: CompletionFailure during import listing crashes javac
jlahoda
parents: 30730
diff changeset
    50
import com.sun.tools.javac.tree.TreeMaker;
7615
8bc078486f2b 7004029: intermittent failures compiling pack200
jjg
parents:
diff changeset
    51
27224
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 25443
diff changeset
    52
import static com.sun.tools.javac.code.Kinds.Kind.*;
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 25443
diff changeset
    53
7615
8bc078486f2b 7004029: intermittent failures compiling pack200
jjg
parents:
diff changeset
    54
public class HashCollisionTest {
8bc078486f2b 7004029: intermittent failures compiling pack200
jjg
parents:
diff changeset
    55
    public static void main(String... args) throws Exception {
8bc078486f2b 7004029: intermittent failures compiling pack200
jjg
parents:
diff changeset
    56
        new HashCollisionTest().run();
8bc078486f2b 7004029: intermittent failures compiling pack200
jjg
parents:
diff changeset
    57
    }
8bc078486f2b 7004029: intermittent failures compiling pack200
jjg
parents:
diff changeset
    58
8bc078486f2b 7004029: intermittent failures compiling pack200
jjg
parents:
diff changeset
    59
    void run() throws Exception {
8bc078486f2b 7004029: intermittent failures compiling pack200
jjg
parents:
diff changeset
    60
        // set up basic environment for test
8bc078486f2b 7004029: intermittent failures compiling pack200
jjg
parents:
diff changeset
    61
        Context context = new Context();
8bc078486f2b 7004029: intermittent failures compiling pack200
jjg
parents:
diff changeset
    62
        JavacFileManager.preRegister(context); // required by ClassReader which is required by Symtab
32059
ea04f56aeacd 8131915: CompletionFailure during import listing crashes javac
jlahoda
parents: 30730
diff changeset
    63
        make = TreeMaker.instance(context);
7615
8bc078486f2b 7004029: intermittent failures compiling pack200
jjg
parents:
diff changeset
    64
        names = Names.instance(context);       // Name.Table impls tied to an instance of Names
8bc078486f2b 7004029: intermittent failures compiling pack200
jjg
parents:
diff changeset
    65
        symtab = Symtab.instance(context);
25443
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 9087
diff changeset
    66
        trees = JavacTrees.instance(context);
27857
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27224
diff changeset
    67
        types = Types.instance(context);
7615
8bc078486f2b 7004029: intermittent failures compiling pack200
jjg
parents:
diff changeset
    68
8bc078486f2b 7004029: intermittent failures compiling pack200
jjg
parents:
diff changeset
    69
        // determine hashMask for an empty scope
25443
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 9087
diff changeset
    70
        Scope emptyScope = WriteableScope.create(symtab.unnamedPackage); // any owner will do
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 9087
diff changeset
    71
        Field field = emptyScope.getClass().getDeclaredField("hashMask");
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 9087
diff changeset
    72
        field.setAccessible(true);
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 9087
diff changeset
    73
        scopeHashMask = field.getInt(emptyScope);
7615
8bc078486f2b 7004029: intermittent failures compiling pack200
jjg
parents:
diff changeset
    74
        log("scopeHashMask: " + scopeHashMask);
8bc078486f2b 7004029: intermittent failures compiling pack200
jjg
parents:
diff changeset
    75
8bc078486f2b 7004029: intermittent failures compiling pack200
jjg
parents:
diff changeset
    76
        // 1. determine the Name.hashCode of "Entry", and therefore the index of
8bc078486f2b 7004029: intermittent failures compiling pack200
jjg
parents:
diff changeset
    77
        // Entry in an empty scope.  i.e. name.hashCode() & Scope.hashMask
8bc078486f2b 7004029: intermittent failures compiling pack200
jjg
parents:
diff changeset
    78
        Name entry = names.fromString("Entry");
8bc078486f2b 7004029: intermittent failures compiling pack200
jjg
parents:
diff changeset
    79
8bc078486f2b 7004029: intermittent failures compiling pack200
jjg
parents:
diff changeset
    80
        // 2. create names of the form *$Entry until we find a name with a
8bc078486f2b 7004029: intermittent failures compiling pack200
jjg
parents:
diff changeset
    81
        // hashcode which yields the same index as Entry in an empty scope.
8bc078486f2b 7004029: intermittent failures compiling pack200
jjg
parents:
diff changeset
    82
        // Since Name.hashCode is a function of position (and not content) it
8bc078486f2b 7004029: intermittent failures compiling pack200
jjg
parents:
diff changeset
    83
        // should work to create successively longer names until one with the
8bc078486f2b 7004029: intermittent failures compiling pack200
jjg
parents:
diff changeset
    84
        // desired characteristics is found.
8bc078486f2b 7004029: intermittent failures compiling pack200
jjg
parents:
diff changeset
    85
        Name outerName;
8bc078486f2b 7004029: intermittent failures compiling pack200
jjg
parents:
diff changeset
    86
        Name innerName;
8bc078486f2b 7004029: intermittent failures compiling pack200
jjg
parents:
diff changeset
    87
        StringBuilder sb = new StringBuilder("C");
8bc078486f2b 7004029: intermittent failures compiling pack200
jjg
parents:
diff changeset
    88
        int i = 0;
8bc078486f2b 7004029: intermittent failures compiling pack200
jjg
parents:
diff changeset
    89
        do {
8bc078486f2b 7004029: intermittent failures compiling pack200
jjg
parents:
diff changeset
    90
            sb.append(Integer.toString(i % 10));
8bc078486f2b 7004029: intermittent failures compiling pack200
jjg
parents:
diff changeset
    91
            innerName = names.fromString(sb + "$Entry");
8bc078486f2b 7004029: intermittent failures compiling pack200
jjg
parents:
diff changeset
    92
        } while (!clash(entry, innerName) && (++i) < MAX_TRIES);
8bc078486f2b 7004029: intermittent failures compiling pack200
jjg
parents:
diff changeset
    93
8bc078486f2b 7004029: intermittent failures compiling pack200
jjg
parents:
diff changeset
    94
        if (clash(entry, innerName)) {
8bc078486f2b 7004029: intermittent failures compiling pack200
jjg
parents:
diff changeset
    95
            log("Detected expected hash collision for " + entry + " and " + innerName
8bc078486f2b 7004029: intermittent failures compiling pack200
jjg
parents:
diff changeset
    96
                    + " after " + i + " tries");
8bc078486f2b 7004029: intermittent failures compiling pack200
jjg
parents:
diff changeset
    97
        } else {
8bc078486f2b 7004029: intermittent failures compiling pack200
jjg
parents:
diff changeset
    98
            throw new Exception("No potential collision found after " + i + " tries");
8bc078486f2b 7004029: intermittent failures compiling pack200
jjg
parents:
diff changeset
    99
        }
8bc078486f2b 7004029: intermittent failures compiling pack200
jjg
parents:
diff changeset
   100
8bc078486f2b 7004029: intermittent failures compiling pack200
jjg
parents:
diff changeset
   101
        outerName = names.fromString(sb.toString());
8bc078486f2b 7004029: intermittent failures compiling pack200
jjg
parents:
diff changeset
   102
8bc078486f2b 7004029: intermittent failures compiling pack200
jjg
parents:
diff changeset
   103
        /*
8bc078486f2b 7004029: intermittent failures compiling pack200
jjg
parents:
diff changeset
   104
         * Now we can set up the scenario.
8bc078486f2b 7004029: intermittent failures compiling pack200
jjg
parents:
diff changeset
   105
         */
8bc078486f2b 7004029: intermittent failures compiling pack200
jjg
parents:
diff changeset
   106
8bc078486f2b 7004029: intermittent failures compiling pack200
jjg
parents:
diff changeset
   107
        // 3. Create a nested class named Entry
8bc078486f2b 7004029: intermittent failures compiling pack200
jjg
parents:
diff changeset
   108
        ClassSymbol cc = createClass(names.fromString("C"), symtab.unnamedPackage);
8bc078486f2b 7004029: intermittent failures compiling pack200
jjg
parents:
diff changeset
   109
        ClassSymbol ce = createClass(entry, cc);
8bc078486f2b 7004029: intermittent failures compiling pack200
jjg
parents:
diff changeset
   110
8bc078486f2b 7004029: intermittent failures compiling pack200
jjg
parents:
diff changeset
   111
        // 4. Create a package containing a nested class using the name from 2
8bc078486f2b 7004029: intermittent failures compiling pack200
jjg
parents:
diff changeset
   112
        PackageSymbol p = new PackageSymbol(names.fromString("p"), symtab.rootPackage);
25443
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 9087
diff changeset
   113
        p.members_field = WriteableScope.create(p);
7615
8bc078486f2b 7004029: intermittent failures compiling pack200
jjg
parents:
diff changeset
   114
        ClassSymbol inner = createClass(innerName, p);
8bc078486f2b 7004029: intermittent failures compiling pack200
jjg
parents:
diff changeset
   115
        // we'll need this later when we "rename" cn
8bc078486f2b 7004029: intermittent failures compiling pack200
jjg
parents:
diff changeset
   116
        ClassSymbol outer = createClass(outerName, p);
8bc078486f2b 7004029: intermittent failures compiling pack200
jjg
parents:
diff changeset
   117
8bc078486f2b 7004029: intermittent failures compiling pack200
jjg
parents:
diff changeset
   118
        // 5. Create a star-import scope
8bc078486f2b 7004029: intermittent failures compiling pack200
jjg
parents:
diff changeset
   119
        log ("createStarImportScope");
8bc078486f2b 7004029: intermittent failures compiling pack200
jjg
parents:
diff changeset
   120
8bc078486f2b 7004029: intermittent failures compiling pack200
jjg
parents:
diff changeset
   121
        PackageSymbol pkg = new PackageSymbol(names.fromString("pkg"), symtab.rootPackage);
25443
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 9087
diff changeset
   122
        StarImportScope starImportScope = new StarImportScope(pkg);
7615
8bc078486f2b 7004029: intermittent failures compiling pack200
jjg
parents:
diff changeset
   123
8bc078486f2b 7004029: intermittent failures compiling pack200
jjg
parents:
diff changeset
   124
        dump("initial", starImportScope);
8bc078486f2b 7004029: intermittent failures compiling pack200
jjg
parents:
diff changeset
   125
8bc078486f2b 7004029: intermittent failures compiling pack200
jjg
parents:
diff changeset
   126
        // 6. Insert the contents of the package from 4.
25443
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 9087
diff changeset
   127
        Scope fromScope = p.members();
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 9087
diff changeset
   128
        ImportFilter typeFilter = new ImportFilter() {
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 9087
diff changeset
   129
            @Override
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 9087
diff changeset
   130
            public boolean accepts(Scope origin, Symbol sym) {
27224
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 25443
diff changeset
   131
                return sym.kind == TYP;
7615
8bc078486f2b 7004029: intermittent failures compiling pack200
jjg
parents:
diff changeset
   132
            }
25443
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 9087
diff changeset
   133
        };
32059
ea04f56aeacd 8131915: CompletionFailure during import listing crashes javac
jlahoda
parents: 30730
diff changeset
   134
        BiConsumer<JCImport, CompletionFailure> noCompletionFailure =
ea04f56aeacd 8131915: CompletionFailure during import listing crashes javac
jlahoda
parents: 30730
diff changeset
   135
                (imp, cf) -> { throw new IllegalStateException(); };
ea04f56aeacd 8131915: CompletionFailure during import listing crashes javac
jlahoda
parents: 30730
diff changeset
   136
        starImportScope.importAll(types, fromScope, typeFilter, make.Import(null, false), noCompletionFailure);
7615
8bc078486f2b 7004029: intermittent failures compiling pack200
jjg
parents:
diff changeset
   137
8bc078486f2b 7004029: intermittent failures compiling pack200
jjg
parents:
diff changeset
   138
        dump("imported p", starImportScope);
8bc078486f2b 7004029: intermittent failures compiling pack200
jjg
parents:
diff changeset
   139
8bc078486f2b 7004029: intermittent failures compiling pack200
jjg
parents:
diff changeset
   140
        // 7. Insert the class from 3.
32059
ea04f56aeacd 8131915: CompletionFailure during import listing crashes javac
jlahoda
parents: 30730
diff changeset
   141
        starImportScope.importAll(types, cc.members_field, typeFilter, make.Import(null, false), noCompletionFailure);
7615
8bc078486f2b 7004029: intermittent failures compiling pack200
jjg
parents:
diff changeset
   142
        dump("imported ce", starImportScope);
8bc078486f2b 7004029: intermittent failures compiling pack200
jjg
parents:
diff changeset
   143
8bc078486f2b 7004029: intermittent failures compiling pack200
jjg
parents:
diff changeset
   144
        /*
8bc078486f2b 7004029: intermittent failures compiling pack200
jjg
parents:
diff changeset
   145
         * Set the trap.
8bc078486f2b 7004029: intermittent failures compiling pack200
jjg
parents:
diff changeset
   146
         */
8bc078486f2b 7004029: intermittent failures compiling pack200
jjg
parents:
diff changeset
   147
8bc078486f2b 7004029: intermittent failures compiling pack200
jjg
parents:
diff changeset
   148
        // 8. Rename the nested class to Entry. so that there is a bogus entry in the star-import scope
8bc078486f2b 7004029: intermittent failures compiling pack200
jjg
parents:
diff changeset
   149
        p.members_field.remove(inner);
8bc078486f2b 7004029: intermittent failures compiling pack200
jjg
parents:
diff changeset
   150
        inner.name = entry;
8bc078486f2b 7004029: intermittent failures compiling pack200
jjg
parents:
diff changeset
   151
        inner.owner = outer;
8bc078486f2b 7004029: intermittent failures compiling pack200
jjg
parents:
diff changeset
   152
        outer.members_field.enter(inner);
8bc078486f2b 7004029: intermittent failures compiling pack200
jjg
parents:
diff changeset
   153
8bc078486f2b 7004029: intermittent failures compiling pack200
jjg
parents:
diff changeset
   154
        // 9. Lookup Entry
25443
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 9087
diff changeset
   155
        Symbol found = starImportScope.findFirst(entry);
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 9087
diff changeset
   156
        if (found != ce)
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 9087
diff changeset
   157
            throw new Exception("correct symbol not found: " + entry + "; found=" + found);
7615
8bc078486f2b 7004029: intermittent failures compiling pack200
jjg
parents:
diff changeset
   158
25443
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 9087
diff changeset
   159
        dump("final", starImportScope);
7615
8bc078486f2b 7004029: intermittent failures compiling pack200
jjg
parents:
diff changeset
   160
    }
8bc078486f2b 7004029: intermittent failures compiling pack200
jjg
parents:
diff changeset
   161
8bc078486f2b 7004029: intermittent failures compiling pack200
jjg
parents:
diff changeset
   162
    /*
8bc078486f2b 7004029: intermittent failures compiling pack200
jjg
parents:
diff changeset
   163
     * Check for a (probable) hash collision in an empty scope.
8bc078486f2b 7004029: intermittent failures compiling pack200
jjg
parents:
diff changeset
   164
     */
8bc078486f2b 7004029: intermittent failures compiling pack200
jjg
parents:
diff changeset
   165
    boolean clash(Name n1, Name n2) {
8bc078486f2b 7004029: intermittent failures compiling pack200
jjg
parents:
diff changeset
   166
        log(n1 + " hc:" + n1.hashCode() + " v:" + (n1.hashCode() & scopeHashMask) + ", " +
8bc078486f2b 7004029: intermittent failures compiling pack200
jjg
parents:
diff changeset
   167
                n2 + " hc:" + n2.hashCode() + " v:" + (n2.hashCode() & scopeHashMask));
8bc078486f2b 7004029: intermittent failures compiling pack200
jjg
parents:
diff changeset
   168
        return (n1.hashCode() & scopeHashMask) == (n2.hashCode() & scopeHashMask);
8bc078486f2b 7004029: intermittent failures compiling pack200
jjg
parents:
diff changeset
   169
    }
8bc078486f2b 7004029: intermittent failures compiling pack200
jjg
parents:
diff changeset
   170
8bc078486f2b 7004029: intermittent failures compiling pack200
jjg
parents:
diff changeset
   171
    /**
8bc078486f2b 7004029: intermittent failures compiling pack200
jjg
parents:
diff changeset
   172
     * Create a class symbol, init the members scope, and add it to owner's scope.
8bc078486f2b 7004029: intermittent failures compiling pack200
jjg
parents:
diff changeset
   173
     */
8bc078486f2b 7004029: intermittent failures compiling pack200
jjg
parents:
diff changeset
   174
    ClassSymbol createClass(Name name, Symbol owner) {
8bc078486f2b 7004029: intermittent failures compiling pack200
jjg
parents:
diff changeset
   175
        ClassSymbol sym = new ClassSymbol(0, name, owner);
25443
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 9087
diff changeset
   176
        sym.members_field = WriteableScope.create(sym);
7615
8bc078486f2b 7004029: intermittent failures compiling pack200
jjg
parents:
diff changeset
   177
        if (owner != symtab.unnamedPackage)
8bc078486f2b 7004029: intermittent failures compiling pack200
jjg
parents:
diff changeset
   178
            owner.members().enter(sym);
8bc078486f2b 7004029: intermittent failures compiling pack200
jjg
parents:
diff changeset
   179
        return sym;
8bc078486f2b 7004029: intermittent failures compiling pack200
jjg
parents:
diff changeset
   180
    }
8bc078486f2b 7004029: intermittent failures compiling pack200
jjg
parents:
diff changeset
   181
8bc078486f2b 7004029: intermittent failures compiling pack200
jjg
parents:
diff changeset
   182
    /**
8bc078486f2b 7004029: intermittent failures compiling pack200
jjg
parents:
diff changeset
   183
     * Dump the contents of a scope to System.err.
8bc078486f2b 7004029: intermittent failures compiling pack200
jjg
parents:
diff changeset
   184
     */
8bc078486f2b 7004029: intermittent failures compiling pack200
jjg
parents:
diff changeset
   185
    void dump(String label, Scope s) throws Exception {
25443
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 9087
diff changeset
   186
        PrintWriter pw = new PrintWriter(System.err);
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 9087
diff changeset
   187
        new DPrinter(pw, trees).printScope(label, s);
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 9087
diff changeset
   188
        pw.flush();
7615
8bc078486f2b 7004029: intermittent failures compiling pack200
jjg
parents:
diff changeset
   189
    }
8bc078486f2b 7004029: intermittent failures compiling pack200
jjg
parents:
diff changeset
   190
25443
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 9087
diff changeset
   191
    Object readField(Object scope, String fieldName) throws Exception {
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 9087
diff changeset
   192
        Field field = scope.getClass().getDeclaredField(fieldName);
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 9087
diff changeset
   193
        field.setAccessible(true);
7615
8bc078486f2b 7004029: intermittent failures compiling pack200
jjg
parents:
diff changeset
   194
25443
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 9087
diff changeset
   195
        return field.get(scope);
7615
8bc078486f2b 7004029: intermittent failures compiling pack200
jjg
parents:
diff changeset
   196
    }
8bc078486f2b 7004029: intermittent failures compiling pack200
jjg
parents:
diff changeset
   197
8bc078486f2b 7004029: intermittent failures compiling pack200
jjg
parents:
diff changeset
   198
    /**
8bc078486f2b 7004029: intermittent failures compiling pack200
jjg
parents:
diff changeset
   199
     * Write a message to stderr.
8bc078486f2b 7004029: intermittent failures compiling pack200
jjg
parents:
diff changeset
   200
     */
8bc078486f2b 7004029: intermittent failures compiling pack200
jjg
parents:
diff changeset
   201
    void log(String msg) {
8bc078486f2b 7004029: intermittent failures compiling pack200
jjg
parents:
diff changeset
   202
        System.err.println(msg);
8bc078486f2b 7004029: intermittent failures compiling pack200
jjg
parents:
diff changeset
   203
    }
8bc078486f2b 7004029: intermittent failures compiling pack200
jjg
parents:
diff changeset
   204
8bc078486f2b 7004029: intermittent failures compiling pack200
jjg
parents:
diff changeset
   205
    int MAX_TRIES = 100; // max tries to find a hash clash before giving up.
8bc078486f2b 7004029: intermittent failures compiling pack200
jjg
parents:
diff changeset
   206
    int scopeHashMask;
8bc078486f2b 7004029: intermittent failures compiling pack200
jjg
parents:
diff changeset
   207
32059
ea04f56aeacd 8131915: CompletionFailure during import listing crashes javac
jlahoda
parents: 30730
diff changeset
   208
    TreeMaker make;
7615
8bc078486f2b 7004029: intermittent failures compiling pack200
jjg
parents:
diff changeset
   209
    Names names;
8bc078486f2b 7004029: intermittent failures compiling pack200
jjg
parents:
diff changeset
   210
    Symtab symtab;
25443
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 9087
diff changeset
   211
    Trees trees;
27857
7e913a535736 7101822: Compiling depends on order of imports
jlahoda
parents: 27224
diff changeset
   212
    Types types;
7615
8bc078486f2b 7004029: intermittent failures compiling pack200
jjg
parents:
diff changeset
   213
}