author | vasya |
Mon, 14 Dec 2015 20:18:19 +0100 | |
changeset 34752 | 9c262a013456 |
parent 30730 | d3ce7619db2c |
child 36526 | 3b41f1c69604 |
permissions | -rw-r--r-- |
27553
75321debd020
8064362: WriteableScope.dupUnshared misbehaves on shared Scopes
jlahoda
parents:
diff
changeset
|
1 |
/* |
34752
9c262a013456
8145342: Some copyright notices are inconsistently and ill formatted
vasya
parents:
30730
diff
changeset
|
2 |
* Copyright (c) 2014, 2015, Oracle and/or its affiliates. All rights reserved. |
27553
75321debd020
8064362: WriteableScope.dupUnshared misbehaves on shared Scopes
jlahoda
parents:
diff
changeset
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
75321debd020
8064362: WriteableScope.dupUnshared misbehaves on shared Scopes
jlahoda
parents:
diff
changeset
|
4 |
* |
75321debd020
8064362: WriteableScope.dupUnshared misbehaves on shared Scopes
jlahoda
parents:
diff
changeset
|
5 |
* This code is free software; you can redistribute it and/or modify it |
75321debd020
8064362: WriteableScope.dupUnshared misbehaves on shared Scopes
jlahoda
parents:
diff
changeset
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
75321debd020
8064362: WriteableScope.dupUnshared misbehaves on shared Scopes
jlahoda
parents:
diff
changeset
|
7 |
* published by the Free Software Foundation. |
75321debd020
8064362: WriteableScope.dupUnshared misbehaves on shared Scopes
jlahoda
parents:
diff
changeset
|
8 |
* |
75321debd020
8064362: WriteableScope.dupUnshared misbehaves on shared Scopes
jlahoda
parents:
diff
changeset
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
75321debd020
8064362: WriteableScope.dupUnshared misbehaves on shared Scopes
jlahoda
parents:
diff
changeset
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
75321debd020
8064362: WriteableScope.dupUnshared misbehaves on shared Scopes
jlahoda
parents:
diff
changeset
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
75321debd020
8064362: WriteableScope.dupUnshared misbehaves on shared Scopes
jlahoda
parents:
diff
changeset
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that |
75321debd020
8064362: WriteableScope.dupUnshared misbehaves on shared Scopes
jlahoda
parents:
diff
changeset
|
13 |
* accompanied this code). |
75321debd020
8064362: WriteableScope.dupUnshared misbehaves on shared Scopes
jlahoda
parents:
diff
changeset
|
14 |
* |
75321debd020
8064362: WriteableScope.dupUnshared misbehaves on shared Scopes
jlahoda
parents:
diff
changeset
|
15 |
* You should have received a copy of the GNU General Public License version |
75321debd020
8064362: WriteableScope.dupUnshared misbehaves on shared Scopes
jlahoda
parents:
diff
changeset
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation, |
75321debd020
8064362: WriteableScope.dupUnshared misbehaves on shared Scopes
jlahoda
parents:
diff
changeset
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
75321debd020
8064362: WriteableScope.dupUnshared misbehaves on shared Scopes
jlahoda
parents:
diff
changeset
|
18 |
* |
75321debd020
8064362: WriteableScope.dupUnshared misbehaves on shared Scopes
jlahoda
parents:
diff
changeset
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
75321debd020
8064362: WriteableScope.dupUnshared misbehaves on shared Scopes
jlahoda
parents:
diff
changeset
|
20 |
* or visit www.oracle.com if you need additional information or have any |
75321debd020
8064362: WriteableScope.dupUnshared misbehaves on shared Scopes
jlahoda
parents:
diff
changeset
|
21 |
* questions. |
75321debd020
8064362: WriteableScope.dupUnshared misbehaves on shared Scopes
jlahoda
parents:
diff
changeset
|
22 |
*/ |
75321debd020
8064362: WriteableScope.dupUnshared misbehaves on shared Scopes
jlahoda
parents:
diff
changeset
|
23 |
|
75321debd020
8064362: WriteableScope.dupUnshared misbehaves on shared Scopes
jlahoda
parents:
diff
changeset
|
24 |
/* |
75321debd020
8064362: WriteableScope.dupUnshared misbehaves on shared Scopes
jlahoda
parents:
diff
changeset
|
25 |
* @test |
75321debd020
8064362: WriteableScope.dupUnshared misbehaves on shared Scopes
jlahoda
parents:
diff
changeset
|
26 |
* @summary WriteableScope.dupUnshared not working properly for shared Scopes. |
30730
d3ce7619db2c
8076543: Add @modules as needed to the langtools tests
akulyakh
parents:
29776
diff
changeset
|
27 |
* @modules jdk.compiler/com.sun.tools.javac.code |
d3ce7619db2c
8076543: Add @modules as needed to the langtools tests
akulyakh
parents:
29776
diff
changeset
|
28 |
* jdk.compiler/com.sun.tools.javac.file |
d3ce7619db2c
8076543: Add @modules as needed to the langtools tests
akulyakh
parents:
29776
diff
changeset
|
29 |
* jdk.compiler/com.sun.tools.javac.util |
27553
75321debd020
8064362: WriteableScope.dupUnshared misbehaves on shared Scopes
jlahoda
parents:
diff
changeset
|
30 |
*/ |
75321debd020
8064362: WriteableScope.dupUnshared misbehaves on shared Scopes
jlahoda
parents:
diff
changeset
|
31 |
|
75321debd020
8064362: WriteableScope.dupUnshared misbehaves on shared Scopes
jlahoda
parents:
diff
changeset
|
32 |
import com.sun.tools.javac.util.*; |
75321debd020
8064362: WriteableScope.dupUnshared misbehaves on shared Scopes
jlahoda
parents:
diff
changeset
|
33 |
import com.sun.tools.javac.code.*; |
75321debd020
8064362: WriteableScope.dupUnshared misbehaves on shared Scopes
jlahoda
parents:
diff
changeset
|
34 |
import com.sun.tools.javac.code.Scope.*; |
75321debd020
8064362: WriteableScope.dupUnshared misbehaves on shared Scopes
jlahoda
parents:
diff
changeset
|
35 |
import com.sun.tools.javac.code.Symbol.*; |
75321debd020
8064362: WriteableScope.dupUnshared misbehaves on shared Scopes
jlahoda
parents:
diff
changeset
|
36 |
import com.sun.tools.javac.file.JavacFileManager; |
75321debd020
8064362: WriteableScope.dupUnshared misbehaves on shared Scopes
jlahoda
parents:
diff
changeset
|
37 |
import java.lang.reflect.Field; |
75321debd020
8064362: WriteableScope.dupUnshared misbehaves on shared Scopes
jlahoda
parents:
diff
changeset
|
38 |
import java.lang.reflect.Method; |
75321debd020
8064362: WriteableScope.dupUnshared misbehaves on shared Scopes
jlahoda
parents:
diff
changeset
|
39 |
import java.util.Collections; |
75321debd020
8064362: WriteableScope.dupUnshared misbehaves on shared Scopes
jlahoda
parents:
diff
changeset
|
40 |
import java.util.IdentityHashMap; |
75321debd020
8064362: WriteableScope.dupUnshared misbehaves on shared Scopes
jlahoda
parents:
diff
changeset
|
41 |
import java.util.Objects; |
75321debd020
8064362: WriteableScope.dupUnshared misbehaves on shared Scopes
jlahoda
parents:
diff
changeset
|
42 |
import java.util.Set; |
75321debd020
8064362: WriteableScope.dupUnshared misbehaves on shared Scopes
jlahoda
parents:
diff
changeset
|
43 |
|
75321debd020
8064362: WriteableScope.dupUnshared misbehaves on shared Scopes
jlahoda
parents:
diff
changeset
|
44 |
public class DupUnsharedTest { |
75321debd020
8064362: WriteableScope.dupUnshared misbehaves on shared Scopes
jlahoda
parents:
diff
changeset
|
45 |
public static void main(String... args) throws Exception { |
75321debd020
8064362: WriteableScope.dupUnshared misbehaves on shared Scopes
jlahoda
parents:
diff
changeset
|
46 |
new DupUnsharedTest().run(); |
75321debd020
8064362: WriteableScope.dupUnshared misbehaves on shared Scopes
jlahoda
parents:
diff
changeset
|
47 |
} |
75321debd020
8064362: WriteableScope.dupUnshared misbehaves on shared Scopes
jlahoda
parents:
diff
changeset
|
48 |
|
75321debd020
8064362: WriteableScope.dupUnshared misbehaves on shared Scopes
jlahoda
parents:
diff
changeset
|
49 |
Context context; |
75321debd020
8064362: WriteableScope.dupUnshared misbehaves on shared Scopes
jlahoda
parents:
diff
changeset
|
50 |
Names names; |
75321debd020
8064362: WriteableScope.dupUnshared misbehaves on shared Scopes
jlahoda
parents:
diff
changeset
|
51 |
Symtab symtab; |
75321debd020
8064362: WriteableScope.dupUnshared misbehaves on shared Scopes
jlahoda
parents:
diff
changeset
|
52 |
Name a; |
75321debd020
8064362: WriteableScope.dupUnshared misbehaves on shared Scopes
jlahoda
parents:
diff
changeset
|
53 |
Name b; |
75321debd020
8064362: WriteableScope.dupUnshared misbehaves on shared Scopes
jlahoda
parents:
diff
changeset
|
54 |
int errors; |
75321debd020
8064362: WriteableScope.dupUnshared misbehaves on shared Scopes
jlahoda
parents:
diff
changeset
|
55 |
|
75321debd020
8064362: WriteableScope.dupUnshared misbehaves on shared Scopes
jlahoda
parents:
diff
changeset
|
56 |
public DupUnsharedTest() { |
75321debd020
8064362: WriteableScope.dupUnshared misbehaves on shared Scopes
jlahoda
parents:
diff
changeset
|
57 |
context = new Context(); |
75321debd020
8064362: WriteableScope.dupUnshared misbehaves on shared Scopes
jlahoda
parents:
diff
changeset
|
58 |
JavacFileManager.preRegister(context); // required by ClassReader which is required by Symtab |
75321debd020
8064362: WriteableScope.dupUnshared misbehaves on shared Scopes
jlahoda
parents:
diff
changeset
|
59 |
names = Names.instance(context); |
75321debd020
8064362: WriteableScope.dupUnshared misbehaves on shared Scopes
jlahoda
parents:
diff
changeset
|
60 |
symtab = Symtab.instance(context); |
75321debd020
8064362: WriteableScope.dupUnshared misbehaves on shared Scopes
jlahoda
parents:
diff
changeset
|
61 |
a = names.fromString("a"); |
75321debd020
8064362: WriteableScope.dupUnshared misbehaves on shared Scopes
jlahoda
parents:
diff
changeset
|
62 |
b = names.fromString("b"); |
75321debd020
8064362: WriteableScope.dupUnshared misbehaves on shared Scopes
jlahoda
parents:
diff
changeset
|
63 |
} |
75321debd020
8064362: WriteableScope.dupUnshared misbehaves on shared Scopes
jlahoda
parents:
diff
changeset
|
64 |
|
75321debd020
8064362: WriteableScope.dupUnshared misbehaves on shared Scopes
jlahoda
parents:
diff
changeset
|
65 |
void run() throws Exception { |
75321debd020
8064362: WriteableScope.dupUnshared misbehaves on shared Scopes
jlahoda
parents:
diff
changeset
|
66 |
runScopeContentTest(); |
75321debd020
8064362: WriteableScope.dupUnshared misbehaves on shared Scopes
jlahoda
parents:
diff
changeset
|
67 |
runClashTest(); |
75321debd020
8064362: WriteableScope.dupUnshared misbehaves on shared Scopes
jlahoda
parents:
diff
changeset
|
68 |
|
75321debd020
8064362: WriteableScope.dupUnshared misbehaves on shared Scopes
jlahoda
parents:
diff
changeset
|
69 |
if (errors > 0) |
75321debd020
8064362: WriteableScope.dupUnshared misbehaves on shared Scopes
jlahoda
parents:
diff
changeset
|
70 |
throw new AssertionError("Errors detected (" + errors + ")."); |
75321debd020
8064362: WriteableScope.dupUnshared misbehaves on shared Scopes
jlahoda
parents:
diff
changeset
|
71 |
} |
75321debd020
8064362: WriteableScope.dupUnshared misbehaves on shared Scopes
jlahoda
parents:
diff
changeset
|
72 |
|
75321debd020
8064362: WriteableScope.dupUnshared misbehaves on shared Scopes
jlahoda
parents:
diff
changeset
|
73 |
void runScopeContentTest() throws Exception { |
29776
984a79b71cfe
8062373: Project Coin: diamond and anonymous classes
sadayapalam
parents:
27553
diff
changeset
|
74 |
Set<Symbol> expected = Collections.newSetFromMap(new IdentityHashMap<>() {}); |
27553
75321debd020
8064362: WriteableScope.dupUnshared misbehaves on shared Scopes
jlahoda
parents:
diff
changeset
|
75 |
Set<Symbol> notExpected = Collections.newSetFromMap(new IdentityHashMap<>()); |
75321debd020
8064362: WriteableScope.dupUnshared misbehaves on shared Scopes
jlahoda
parents:
diff
changeset
|
76 |
WriteableScope s1 = WriteableScope.create(symtab.rootPackage); |
75321debd020
8064362: WriteableScope.dupUnshared misbehaves on shared Scopes
jlahoda
parents:
diff
changeset
|
77 |
ClassSymbol acceptSym = symtab.arrayClass; |
75321debd020
8064362: WriteableScope.dupUnshared misbehaves on shared Scopes
jlahoda
parents:
diff
changeset
|
78 |
s1.enter(acceptSym); |
75321debd020
8064362: WriteableScope.dupUnshared misbehaves on shared Scopes
jlahoda
parents:
diff
changeset
|
79 |
expected.add(acceptSym); |
75321debd020
8064362: WriteableScope.dupUnshared misbehaves on shared Scopes
jlahoda
parents:
diff
changeset
|
80 |
WriteableScope s2 = s1.dup(); |
75321debd020
8064362: WriteableScope.dupUnshared misbehaves on shared Scopes
jlahoda
parents:
diff
changeset
|
81 |
fillScope(s2, notExpected, a); |
75321debd020
8064362: WriteableScope.dupUnshared misbehaves on shared Scopes
jlahoda
parents:
diff
changeset
|
82 |
WriteableScope s3 = s2.dup(); |
75321debd020
8064362: WriteableScope.dupUnshared misbehaves on shared Scopes
jlahoda
parents:
diff
changeset
|
83 |
fillScope(s3, notExpected, b); |
75321debd020
8064362: WriteableScope.dupUnshared misbehaves on shared Scopes
jlahoda
parents:
diff
changeset
|
84 |
WriteableScope s4 = s1.dupUnshared(); |
75321debd020
8064362: WriteableScope.dupUnshared misbehaves on shared Scopes
jlahoda
parents:
diff
changeset
|
85 |
assertEquals(toSet(s4.getSymbols()), expected); |
75321debd020
8064362: WriteableScope.dupUnshared misbehaves on shared Scopes
jlahoda
parents:
diff
changeset
|
86 |
assertEquals(toSet(s4.getSymbolsByName(a)), Collections.emptySet()); |
75321debd020
8064362: WriteableScope.dupUnshared misbehaves on shared Scopes
jlahoda
parents:
diff
changeset
|
87 |
assertEquals(toSet(s4.getSymbolsByName(b)), Collections.emptySet()); |
75321debd020
8064362: WriteableScope.dupUnshared misbehaves on shared Scopes
jlahoda
parents:
diff
changeset
|
88 |
assertEquals(toSet(s4.getSymbolsByName(acceptSym.name)), expected); |
75321debd020
8064362: WriteableScope.dupUnshared misbehaves on shared Scopes
jlahoda
parents:
diff
changeset
|
89 |
for (Symbol sym : notExpected) { |
75321debd020
8064362: WriteableScope.dupUnshared misbehaves on shared Scopes
jlahoda
parents:
diff
changeset
|
90 |
try { |
75321debd020
8064362: WriteableScope.dupUnshared misbehaves on shared Scopes
jlahoda
parents:
diff
changeset
|
91 |
s4.remove(sym); |
75321debd020
8064362: WriteableScope.dupUnshared misbehaves on shared Scopes
jlahoda
parents:
diff
changeset
|
92 |
} catch (Exception ex) { |
75321debd020
8064362: WriteableScope.dupUnshared misbehaves on shared Scopes
jlahoda
parents:
diff
changeset
|
93 |
System.err.println("s4.remove(" + sym + "); crashes with exception:"); |
75321debd020
8064362: WriteableScope.dupUnshared misbehaves on shared Scopes
jlahoda
parents:
diff
changeset
|
94 |
ex.printStackTrace(); |
75321debd020
8064362: WriteableScope.dupUnshared misbehaves on shared Scopes
jlahoda
parents:
diff
changeset
|
95 |
errors++; |
75321debd020
8064362: WriteableScope.dupUnshared misbehaves on shared Scopes
jlahoda
parents:
diff
changeset
|
96 |
} |
75321debd020
8064362: WriteableScope.dupUnshared misbehaves on shared Scopes
jlahoda
parents:
diff
changeset
|
97 |
} |
75321debd020
8064362: WriteableScope.dupUnshared misbehaves on shared Scopes
jlahoda
parents:
diff
changeset
|
98 |
} |
75321debd020
8064362: WriteableScope.dupUnshared misbehaves on shared Scopes
jlahoda
parents:
diff
changeset
|
99 |
|
75321debd020
8064362: WriteableScope.dupUnshared misbehaves on shared Scopes
jlahoda
parents:
diff
changeset
|
100 |
void fillScope(WriteableScope scope, Set<Symbol> notExpected, Name name) { |
75321debd020
8064362: WriteableScope.dupUnshared misbehaves on shared Scopes
jlahoda
parents:
diff
changeset
|
101 |
VarSymbol var1 = new VarSymbol(0, name, Type.noType, symtab.arrayClass); |
75321debd020
8064362: WriteableScope.dupUnshared misbehaves on shared Scopes
jlahoda
parents:
diff
changeset
|
102 |
VarSymbol var2 = new VarSymbol(0, name, Type.noType, symtab.autoCloseableClose.owner); |
75321debd020
8064362: WriteableScope.dupUnshared misbehaves on shared Scopes
jlahoda
parents:
diff
changeset
|
103 |
scope.enter(var1); |
75321debd020
8064362: WriteableScope.dupUnshared misbehaves on shared Scopes
jlahoda
parents:
diff
changeset
|
104 |
scope.enter(var2); |
75321debd020
8064362: WriteableScope.dupUnshared misbehaves on shared Scopes
jlahoda
parents:
diff
changeset
|
105 |
scope.remove(var1); |
75321debd020
8064362: WriteableScope.dupUnshared misbehaves on shared Scopes
jlahoda
parents:
diff
changeset
|
106 |
notExpected.add(var1); |
75321debd020
8064362: WriteableScope.dupUnshared misbehaves on shared Scopes
jlahoda
parents:
diff
changeset
|
107 |
notExpected.add(var2); |
75321debd020
8064362: WriteableScope.dupUnshared misbehaves on shared Scopes
jlahoda
parents:
diff
changeset
|
108 |
} |
75321debd020
8064362: WriteableScope.dupUnshared misbehaves on shared Scopes
jlahoda
parents:
diff
changeset
|
109 |
|
75321debd020
8064362: WriteableScope.dupUnshared misbehaves on shared Scopes
jlahoda
parents:
diff
changeset
|
110 |
Set<Symbol> toSet(Iterable<Symbol> it) { |
29776
984a79b71cfe
8062373: Project Coin: diamond and anonymous classes
sadayapalam
parents:
27553
diff
changeset
|
111 |
Set<Symbol> result = Collections.newSetFromMap(new IdentityHashMap<>() {}); |
27553
75321debd020
8064362: WriteableScope.dupUnshared misbehaves on shared Scopes
jlahoda
parents:
diff
changeset
|
112 |
|
75321debd020
8064362: WriteableScope.dupUnshared misbehaves on shared Scopes
jlahoda
parents:
diff
changeset
|
113 |
for (Symbol sym : it) { |
75321debd020
8064362: WriteableScope.dupUnshared misbehaves on shared Scopes
jlahoda
parents:
diff
changeset
|
114 |
result.add(sym); |
75321debd020
8064362: WriteableScope.dupUnshared misbehaves on shared Scopes
jlahoda
parents:
diff
changeset
|
115 |
} |
75321debd020
8064362: WriteableScope.dupUnshared misbehaves on shared Scopes
jlahoda
parents:
diff
changeset
|
116 |
|
75321debd020
8064362: WriteableScope.dupUnshared misbehaves on shared Scopes
jlahoda
parents:
diff
changeset
|
117 |
return result; |
75321debd020
8064362: WriteableScope.dupUnshared misbehaves on shared Scopes
jlahoda
parents:
diff
changeset
|
118 |
} |
75321debd020
8064362: WriteableScope.dupUnshared misbehaves on shared Scopes
jlahoda
parents:
diff
changeset
|
119 |
|
75321debd020
8064362: WriteableScope.dupUnshared misbehaves on shared Scopes
jlahoda
parents:
diff
changeset
|
120 |
void assertEquals(Set<Symbol> set1, Set<Symbol> set2) { |
75321debd020
8064362: WriteableScope.dupUnshared misbehaves on shared Scopes
jlahoda
parents:
diff
changeset
|
121 |
if (!Objects.equals(set1, set2)) { |
75321debd020
8064362: WriteableScope.dupUnshared misbehaves on shared Scopes
jlahoda
parents:
diff
changeset
|
122 |
System.err.println("Sets are not equals: s1=" + set1 + "; s2=" + set2); |
75321debd020
8064362: WriteableScope.dupUnshared misbehaves on shared Scopes
jlahoda
parents:
diff
changeset
|
123 |
errors++; |
75321debd020
8064362: WriteableScope.dupUnshared misbehaves on shared Scopes
jlahoda
parents:
diff
changeset
|
124 |
} |
75321debd020
8064362: WriteableScope.dupUnshared misbehaves on shared Scopes
jlahoda
parents:
diff
changeset
|
125 |
} |
75321debd020
8064362: WriteableScope.dupUnshared misbehaves on shared Scopes
jlahoda
parents:
diff
changeset
|
126 |
|
75321debd020
8064362: WriteableScope.dupUnshared misbehaves on shared Scopes
jlahoda
parents:
diff
changeset
|
127 |
/** |
75321debd020
8064362: WriteableScope.dupUnshared misbehaves on shared Scopes
jlahoda
parents:
diff
changeset
|
128 |
* This tests tests the following situation. |
75321debd020
8064362: WriteableScope.dupUnshared misbehaves on shared Scopes
jlahoda
parents:
diff
changeset
|
129 |
* - consider empty Scope S1 |
75321debd020
8064362: WriteableScope.dupUnshared misbehaves on shared Scopes
jlahoda
parents:
diff
changeset
|
130 |
* - a Symbol with name 'A' is added into S1 |
75321debd020
8064362: WriteableScope.dupUnshared misbehaves on shared Scopes
jlahoda
parents:
diff
changeset
|
131 |
* - S1 is dupped into S2 |
75321debd020
8064362: WriteableScope.dupUnshared misbehaves on shared Scopes
jlahoda
parents:
diff
changeset
|
132 |
* - a Symbol with name 'B', clashing with 'A', is added into S2 |
75321debd020
8064362: WriteableScope.dupUnshared misbehaves on shared Scopes
jlahoda
parents:
diff
changeset
|
133 |
* - so the table now looks like: [..., A, ..., B, ...] |
75321debd020
8064362: WriteableScope.dupUnshared misbehaves on shared Scopes
jlahoda
parents:
diff
changeset
|
134 |
* - S2 is doubled. As a consequence, the table is re-hashed, and looks like: |
75321debd020
8064362: WriteableScope.dupUnshared misbehaves on shared Scopes
jlahoda
parents:
diff
changeset
|
135 |
* [..., B, ..., A, ...] (note that re-hashing goes from the end, hence the original order). |
75321debd020
8064362: WriteableScope.dupUnshared misbehaves on shared Scopes
jlahoda
parents:
diff
changeset
|
136 |
* - B has been chosen so that it clashes in the doubled scope as well. So when looking up 'A', |
75321debd020
8064362: WriteableScope.dupUnshared misbehaves on shared Scopes
jlahoda
parents:
diff
changeset
|
137 |
* B is found (and rejected) first, and only then the A's bucket is tested. |
75321debd020
8064362: WriteableScope.dupUnshared misbehaves on shared Scopes
jlahoda
parents:
diff
changeset
|
138 |
* - S2 is dupUshared - the resulting table needs to look like: [..., /sentinel/, ..., A, ...], not |
75321debd020
8064362: WriteableScope.dupUnshared misbehaves on shared Scopes
jlahoda
parents:
diff
changeset
|
139 |
* [..., null, ..., A, ...], as in the latter case lookups would see 'null' while looking for |
75321debd020
8064362: WriteableScope.dupUnshared misbehaves on shared Scopes
jlahoda
parents:
diff
changeset
|
140 |
* 'A' and would stop the search prematurely. |
75321debd020
8064362: WriteableScope.dupUnshared misbehaves on shared Scopes
jlahoda
parents:
diff
changeset
|
141 |
*/ |
75321debd020
8064362: WriteableScope.dupUnshared misbehaves on shared Scopes
jlahoda
parents:
diff
changeset
|
142 |
void runClashTest() throws Exception { |
75321debd020
8064362: WriteableScope.dupUnshared misbehaves on shared Scopes
jlahoda
parents:
diff
changeset
|
143 |
WriteableScope emptyScope = WriteableScope.create(symtab.unnamedPackage); |
75321debd020
8064362: WriteableScope.dupUnshared misbehaves on shared Scopes
jlahoda
parents:
diff
changeset
|
144 |
Field tableField = emptyScope.getClass().getDeclaredField("table"); |
75321debd020
8064362: WriteableScope.dupUnshared misbehaves on shared Scopes
jlahoda
parents:
diff
changeset
|
145 |
tableField.setAccessible(true); |
75321debd020
8064362: WriteableScope.dupUnshared misbehaves on shared Scopes
jlahoda
parents:
diff
changeset
|
146 |
Method dble = emptyScope.getClass().getDeclaredMethod("dble"); |
75321debd020
8064362: WriteableScope.dupUnshared misbehaves on shared Scopes
jlahoda
parents:
diff
changeset
|
147 |
dble.setAccessible(true); |
75321debd020
8064362: WriteableScope.dupUnshared misbehaves on shared Scopes
jlahoda
parents:
diff
changeset
|
148 |
Method getIndex = emptyScope.getClass().getDeclaredMethod("getIndex", Name.class); |
75321debd020
8064362: WriteableScope.dupUnshared misbehaves on shared Scopes
jlahoda
parents:
diff
changeset
|
149 |
getIndex.setAccessible(true); |
75321debd020
8064362: WriteableScope.dupUnshared misbehaves on shared Scopes
jlahoda
parents:
diff
changeset
|
150 |
|
75321debd020
8064362: WriteableScope.dupUnshared misbehaves on shared Scopes
jlahoda
parents:
diff
changeset
|
151 |
int tries = 0; |
75321debd020
8064362: WriteableScope.dupUnshared misbehaves on shared Scopes
jlahoda
parents:
diff
changeset
|
152 |
|
75321debd020
8064362: WriteableScope.dupUnshared misbehaves on shared Scopes
jlahoda
parents:
diff
changeset
|
153 |
//find a name that will be in the first bucket in table (so that a conflicting name |
75321debd020
8064362: WriteableScope.dupUnshared misbehaves on shared Scopes
jlahoda
parents:
diff
changeset
|
154 |
//will be in placed in a bucket after this one). |
75321debd020
8064362: WriteableScope.dupUnshared misbehaves on shared Scopes
jlahoda
parents:
diff
changeset
|
155 |
Name first = names.fromString("a"); |
75321debd020
8064362: WriteableScope.dupUnshared misbehaves on shared Scopes
jlahoda
parents:
diff
changeset
|
156 |
while ((Integer) getIndex.invoke(emptyScope, first) != 0) { |
75321debd020
8064362: WriteableScope.dupUnshared misbehaves on shared Scopes
jlahoda
parents:
diff
changeset
|
157 |
if (tries++ > MAX_TRIES) { |
75321debd020
8064362: WriteableScope.dupUnshared misbehaves on shared Scopes
jlahoda
parents:
diff
changeset
|
158 |
System.err.println("could not find a name that would be placed in the first bucket"); |
75321debd020
8064362: WriteableScope.dupUnshared misbehaves on shared Scopes
jlahoda
parents:
diff
changeset
|
159 |
errors++; |
75321debd020
8064362: WriteableScope.dupUnshared misbehaves on shared Scopes
jlahoda
parents:
diff
changeset
|
160 |
return ; |
75321debd020
8064362: WriteableScope.dupUnshared misbehaves on shared Scopes
jlahoda
parents:
diff
changeset
|
161 |
} |
75321debd020
8064362: WriteableScope.dupUnshared misbehaves on shared Scopes
jlahoda
parents:
diff
changeset
|
162 |
first = names.fromString("a" + first.toString()); |
75321debd020
8064362: WriteableScope.dupUnshared misbehaves on shared Scopes
jlahoda
parents:
diff
changeset
|
163 |
} |
75321debd020
8064362: WriteableScope.dupUnshared misbehaves on shared Scopes
jlahoda
parents:
diff
changeset
|
164 |
|
75321debd020
8064362: WriteableScope.dupUnshared misbehaves on shared Scopes
jlahoda
parents:
diff
changeset
|
165 |
System.out.println("first name: " + first); |
75321debd020
8064362: WriteableScope.dupUnshared misbehaves on shared Scopes
jlahoda
parents:
diff
changeset
|
166 |
|
75321debd020
8064362: WriteableScope.dupUnshared misbehaves on shared Scopes
jlahoda
parents:
diff
changeset
|
167 |
//now, find another name, that will clash with the first one both in the empty and a doubled scope: |
75321debd020
8064362: WriteableScope.dupUnshared misbehaves on shared Scopes
jlahoda
parents:
diff
changeset
|
168 |
Scope doubledEmptyScope = WriteableScope.create(symtab.unnamedPackage); |
75321debd020
8064362: WriteableScope.dupUnshared misbehaves on shared Scopes
jlahoda
parents:
diff
changeset
|
169 |
dble.invoke(doubledEmptyScope); |
75321debd020
8064362: WriteableScope.dupUnshared misbehaves on shared Scopes
jlahoda
parents:
diff
changeset
|
170 |
Integer firstNameTestScopeIndex = (Integer) getIndex.invoke(emptyScope, first); |
75321debd020
8064362: WriteableScope.dupUnshared misbehaves on shared Scopes
jlahoda
parents:
diff
changeset
|
171 |
Integer firstNameDoubleScopeIndex = (Integer) getIndex.invoke(doubledEmptyScope, first); |
75321debd020
8064362: WriteableScope.dupUnshared misbehaves on shared Scopes
jlahoda
parents:
diff
changeset
|
172 |
Name other = names.fromString("b"); |
75321debd020
8064362: WriteableScope.dupUnshared misbehaves on shared Scopes
jlahoda
parents:
diff
changeset
|
173 |
while (!Objects.equals(firstNameTestScopeIndex, getIndex.invoke(emptyScope, other)) || |
75321debd020
8064362: WriteableScope.dupUnshared misbehaves on shared Scopes
jlahoda
parents:
diff
changeset
|
174 |
!Objects.equals(firstNameDoubleScopeIndex, getIndex.invoke(doubledEmptyScope, other))) { |
75321debd020
8064362: WriteableScope.dupUnshared misbehaves on shared Scopes
jlahoda
parents:
diff
changeset
|
175 |
if (tries++ > MAX_TRIES) { |
75321debd020
8064362: WriteableScope.dupUnshared misbehaves on shared Scopes
jlahoda
parents:
diff
changeset
|
176 |
System.err.println("could not find a name that would properly clash with the first chosen name"); |
75321debd020
8064362: WriteableScope.dupUnshared misbehaves on shared Scopes
jlahoda
parents:
diff
changeset
|
177 |
errors++; |
75321debd020
8064362: WriteableScope.dupUnshared misbehaves on shared Scopes
jlahoda
parents:
diff
changeset
|
178 |
return ; |
75321debd020
8064362: WriteableScope.dupUnshared misbehaves on shared Scopes
jlahoda
parents:
diff
changeset
|
179 |
} |
75321debd020
8064362: WriteableScope.dupUnshared misbehaves on shared Scopes
jlahoda
parents:
diff
changeset
|
180 |
other = names.fromString("b" + other); |
75321debd020
8064362: WriteableScope.dupUnshared misbehaves on shared Scopes
jlahoda
parents:
diff
changeset
|
181 |
} |
75321debd020
8064362: WriteableScope.dupUnshared misbehaves on shared Scopes
jlahoda
parents:
diff
changeset
|
182 |
|
75321debd020
8064362: WriteableScope.dupUnshared misbehaves on shared Scopes
jlahoda
parents:
diff
changeset
|
183 |
System.out.println("other name: " + other); |
75321debd020
8064362: WriteableScope.dupUnshared misbehaves on shared Scopes
jlahoda
parents:
diff
changeset
|
184 |
|
75321debd020
8064362: WriteableScope.dupUnshared misbehaves on shared Scopes
jlahoda
parents:
diff
changeset
|
185 |
Symbol firstSymbol = new VarSymbol(0, first, Type.noType, null); |
75321debd020
8064362: WriteableScope.dupUnshared misbehaves on shared Scopes
jlahoda
parents:
diff
changeset
|
186 |
Symbol otherSymbol = new VarSymbol(0, other, Type.noType, null); |
75321debd020
8064362: WriteableScope.dupUnshared misbehaves on shared Scopes
jlahoda
parents:
diff
changeset
|
187 |
|
75321debd020
8064362: WriteableScope.dupUnshared misbehaves on shared Scopes
jlahoda
parents:
diff
changeset
|
188 |
//test the situation described above: |
75321debd020
8064362: WriteableScope.dupUnshared misbehaves on shared Scopes
jlahoda
parents:
diff
changeset
|
189 |
WriteableScope testScope1 = WriteableScope.create(symtab.unnamedPackage); |
75321debd020
8064362: WriteableScope.dupUnshared misbehaves on shared Scopes
jlahoda
parents:
diff
changeset
|
190 |
testScope1.enter(firstSymbol); |
75321debd020
8064362: WriteableScope.dupUnshared misbehaves on shared Scopes
jlahoda
parents:
diff
changeset
|
191 |
|
75321debd020
8064362: WriteableScope.dupUnshared misbehaves on shared Scopes
jlahoda
parents:
diff
changeset
|
192 |
WriteableScope dupped1 = testScope1.dup(); |
75321debd020
8064362: WriteableScope.dupUnshared misbehaves on shared Scopes
jlahoda
parents:
diff
changeset
|
193 |
|
75321debd020
8064362: WriteableScope.dupUnshared misbehaves on shared Scopes
jlahoda
parents:
diff
changeset
|
194 |
dupped1.enter(otherSymbol); |
75321debd020
8064362: WriteableScope.dupUnshared misbehaves on shared Scopes
jlahoda
parents:
diff
changeset
|
195 |
dble.invoke(dupped1); |
75321debd020
8064362: WriteableScope.dupUnshared misbehaves on shared Scopes
jlahoda
parents:
diff
changeset
|
196 |
|
75321debd020
8064362: WriteableScope.dupUnshared misbehaves on shared Scopes
jlahoda
parents:
diff
changeset
|
197 |
if (testScope1.dupUnshared().findFirst(first) != firstSymbol) { |
75321debd020
8064362: WriteableScope.dupUnshared misbehaves on shared Scopes
jlahoda
parents:
diff
changeset
|
198 |
System.err.println("cannot find the Symbol in the dupUnshared scope (1)"); |
75321debd020
8064362: WriteableScope.dupUnshared misbehaves on shared Scopes
jlahoda
parents:
diff
changeset
|
199 |
errors++; |
75321debd020
8064362: WriteableScope.dupUnshared misbehaves on shared Scopes
jlahoda
parents:
diff
changeset
|
200 |
} |
75321debd020
8064362: WriteableScope.dupUnshared misbehaves on shared Scopes
jlahoda
parents:
diff
changeset
|
201 |
|
75321debd020
8064362: WriteableScope.dupUnshared misbehaves on shared Scopes
jlahoda
parents:
diff
changeset
|
202 |
//also check a situation where the clashing Symbol is removed from the dupped scope: |
75321debd020
8064362: WriteableScope.dupUnshared misbehaves on shared Scopes
jlahoda
parents:
diff
changeset
|
203 |
WriteableScope testScope2 = WriteableScope.create(symtab.unnamedPackage); |
75321debd020
8064362: WriteableScope.dupUnshared misbehaves on shared Scopes
jlahoda
parents:
diff
changeset
|
204 |
testScope2.enter(firstSymbol); |
75321debd020
8064362: WriteableScope.dupUnshared misbehaves on shared Scopes
jlahoda
parents:
diff
changeset
|
205 |
|
75321debd020
8064362: WriteableScope.dupUnshared misbehaves on shared Scopes
jlahoda
parents:
diff
changeset
|
206 |
WriteableScope dupped2 = testScope2.dup(); |
75321debd020
8064362: WriteableScope.dupUnshared misbehaves on shared Scopes
jlahoda
parents:
diff
changeset
|
207 |
|
75321debd020
8064362: WriteableScope.dupUnshared misbehaves on shared Scopes
jlahoda
parents:
diff
changeset
|
208 |
dupped2.enter(otherSymbol); |
75321debd020
8064362: WriteableScope.dupUnshared misbehaves on shared Scopes
jlahoda
parents:
diff
changeset
|
209 |
dble.invoke(dupped2); |
75321debd020
8064362: WriteableScope.dupUnshared misbehaves on shared Scopes
jlahoda
parents:
diff
changeset
|
210 |
dupped2.remove(otherSymbol); |
75321debd020
8064362: WriteableScope.dupUnshared misbehaves on shared Scopes
jlahoda
parents:
diff
changeset
|
211 |
|
75321debd020
8064362: WriteableScope.dupUnshared misbehaves on shared Scopes
jlahoda
parents:
diff
changeset
|
212 |
if (testScope2.dupUnshared().findFirst(first) != firstSymbol) { |
75321debd020
8064362: WriteableScope.dupUnshared misbehaves on shared Scopes
jlahoda
parents:
diff
changeset
|
213 |
System.err.println("cannot find the Symbol in the dupUnshared scope (2)"); |
75321debd020
8064362: WriteableScope.dupUnshared misbehaves on shared Scopes
jlahoda
parents:
diff
changeset
|
214 |
errors++; |
75321debd020
8064362: WriteableScope.dupUnshared misbehaves on shared Scopes
jlahoda
parents:
diff
changeset
|
215 |
} |
75321debd020
8064362: WriteableScope.dupUnshared misbehaves on shared Scopes
jlahoda
parents:
diff
changeset
|
216 |
} |
75321debd020
8064362: WriteableScope.dupUnshared misbehaves on shared Scopes
jlahoda
parents:
diff
changeset
|
217 |
|
75321debd020
8064362: WriteableScope.dupUnshared misbehaves on shared Scopes
jlahoda
parents:
diff
changeset
|
218 |
int MAX_TRIES = 100; // max tries to find a hash clash before giving up. |
75321debd020
8064362: WriteableScope.dupUnshared misbehaves on shared Scopes
jlahoda
parents:
diff
changeset
|
219 |
|
75321debd020
8064362: WriteableScope.dupUnshared misbehaves on shared Scopes
jlahoda
parents:
diff
changeset
|
220 |
} |