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